1 /*-
2 * Copyright (c) 1998 - 2007 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/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/ata.h>
35 #include <sys/bus.h>
36 #include <sys/endian.h>
37 #include <sys/malloc.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/sema.h>
41 #include <sys/taskqueue.h>
42 #include <vm/uma.h>
43 #include <machine/stdarg.h>
44 #include <machine/resource.h>
45 #include <machine/bus.h>
46 #include <sys/rman.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/ata/ata-all.h>
50 #include <dev/ata/ata-pci.h>
51 #include <ata_if.h>
52
53 /* local prototypes */
54 /* ata-chipset.c */
55 static int ata_generic_chipinit(device_t dev);
56 static void ata_generic_intr(void *data);
57 static void ata_generic_setmode(device_t dev, int mode);
58 static void ata_sata_phy_check_events(device_t dev);
59 static void ata_sata_phy_event(void *context, int dummy);
60 static int ata_sata_phy_reset(device_t dev);
61 static int ata_sata_connect(struct ata_channel *ch);
62 static void ata_sata_setmode(device_t dev, int mode);
63 static int ata_request2fis_h2d(struct ata_request *request, u_int8_t *fis);
64 static int ata_ahci_chipinit(device_t dev);
65 static int ata_ahci_allocate(device_t dev);
66 static int ata_ahci_status(device_t dev);
67 static int ata_ahci_begin_transaction(struct ata_request *request);
68 static int ata_ahci_end_transaction(struct ata_request *request);
69 static void ata_ahci_reset(device_t dev);
70 static void ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
71 static void ata_ahci_dmainit(device_t dev);
72 static int ata_ahci_setup_fis(struct ata_ahci_cmd_tab *ctp, struct ata_request *request);
73 static int ata_acard_chipinit(device_t dev);
74 static int ata_acard_allocate(device_t dev);
75 static int ata_acard_status(device_t dev);
76 static void ata_acard_850_setmode(device_t dev, int mode);
77 static void ata_acard_86X_setmode(device_t dev, int mode);
78 static int ata_ali_chipinit(device_t dev);
79 static int ata_ali_allocate(device_t dev);
80 static int ata_ali_sata_allocate(device_t dev);
81 static void ata_ali_reset(device_t dev);
82 static void ata_ali_setmode(device_t dev, int mode);
83 static int ata_amd_chipinit(device_t dev);
84 static int ata_ati_chipinit(device_t dev);
85 static void ata_ati_setmode(device_t dev, int mode);
86 static int ata_cyrix_chipinit(device_t dev);
87 static void ata_cyrix_setmode(device_t dev, int mode);
88 static int ata_cypress_chipinit(device_t dev);
89 static void ata_cypress_setmode(device_t dev, int mode);
90 static int ata_highpoint_chipinit(device_t dev);
91 static int ata_highpoint_allocate(device_t dev);
92 static void ata_highpoint_setmode(device_t dev, int mode);
93 static int ata_highpoint_check_80pin(device_t dev, int mode);
94 static int ata_intel_chipinit(device_t dev);
95 static int ata_intel_allocate(device_t dev);
96 static void ata_intel_reset(device_t dev);
97 static void ata_intel_old_setmode(device_t dev, int mode);
98 static void ata_intel_new_setmode(device_t dev, int mode);
99 static void ata_intel_sata_setmode(device_t dev, int mode);
100 static int ata_intel_31244_allocate(device_t dev);
101 static int ata_intel_31244_status(device_t dev);
102 static void ata_intel_31244_tf_write(struct ata_request *request);
103 static void ata_intel_31244_reset(device_t dev);
104 static int ata_ite_chipinit(device_t dev);
105 static void ata_ite_setmode(device_t dev, int mode);
106 static int ata_jmicron_chipinit(device_t dev);
107 static int ata_jmicron_allocate(device_t dev);
108 static void ata_jmicron_reset(device_t dev);
109 static void ata_jmicron_dmainit(device_t dev);
110 static void ata_jmicron_setmode(device_t dev, int mode);
111 static int ata_marvell_pata_chipinit(device_t dev);
112 static int ata_marvell_pata_allocate(device_t dev);
113 static void ata_marvell_pata_setmode(device_t dev, int mode);
114 static int ata_marvell_edma_chipinit(device_t dev);
115 static int ata_marvell_edma_allocate(device_t dev);
116 static int ata_marvell_edma_status(device_t dev);
117 static int ata_marvell_edma_begin_transaction(struct ata_request *request);
118 static int ata_marvell_edma_end_transaction(struct ata_request *request);
119 static void ata_marvell_edma_reset(device_t dev);
120 static void ata_marvell_edma_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
121 static void ata_marvell_edma_dmainit(device_t dev);
122 static int ata_national_chipinit(device_t dev);
123 static void ata_national_setmode(device_t dev, int mode);
124 static int ata_netcell_chipinit(device_t dev);
125 static int ata_netcell_allocate(device_t dev);
126 static int ata_nvidia_chipinit(device_t dev);
127 static int ata_nvidia_allocate(device_t dev);
128 static int ata_nvidia_status(device_t dev);
129 static void ata_nvidia_reset(device_t dev);
130 static int ata_promise_chipinit(device_t dev);
131 static int ata_promise_allocate(device_t dev);
132 static int ata_promise_status(device_t dev);
133 static int ata_promise_dmastart(device_t dev);
134 static int ata_promise_dmastop(device_t dev);
135 static void ata_promise_dmareset(device_t dev);
136 static void ata_promise_dmainit(device_t dev);
137 static void ata_promise_setmode(device_t dev, int mode);
138 static int ata_promise_tx2_allocate(device_t dev);
139 static int ata_promise_tx2_status(device_t dev);
140 static int ata_promise_mio_allocate(device_t dev);
141 static void ata_promise_mio_intr(void *data);
142 static int ata_promise_mio_status(device_t dev);
143 static int ata_promise_mio_command(struct ata_request *request);
144 static void ata_promise_mio_reset(device_t dev);
145 static void ata_promise_mio_dmainit(device_t dev);
146 static void ata_promise_mio_setprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
147 static void ata_promise_mio_setmode(device_t dev, int mode);
148 static void ata_promise_sx4_intr(void *data);
149 static int ata_promise_sx4_command(struct ata_request *request);
150 static int ata_promise_apkt(u_int8_t *bytep, struct ata_request *request);
151 static void ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt);
152 static void ata_promise_next_hpkt(struct ata_pci_controller *ctlr);
153 static int ata_serverworks_chipinit(device_t dev);
154 static int ata_serverworks_allocate(device_t dev);
155 static void ata_serverworks_tf_read(struct ata_request *request);
156 static void ata_serverworks_tf_write(struct ata_request *request);
157 static void ata_serverworks_setmode(device_t dev, int mode);
158 static int ata_sii_chipinit(device_t dev);
159 static int ata_cmd_allocate(device_t dev);
160 static int ata_cmd_status(device_t dev);
161 static void ata_cmd_setmode(device_t dev, int mode);
162 static int ata_sii_allocate(device_t dev);
163 static int ata_sii_status(device_t dev);
164 static void ata_sii_reset(device_t dev);
165 static void ata_sii_setmode(device_t dev, int mode);
166 static int ata_siiprb_allocate(device_t dev);
167 static int ata_siiprb_status(device_t dev);
168 static int ata_siiprb_begin_transaction(struct ata_request *request);
169 static int ata_siiprb_end_transaction(struct ata_request *request);
170 static void ata_siiprb_reset(device_t dev);
171 static void ata_siiprb_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
172 static void ata_siiprb_dmainit(device_t dev);
173 static int ata_sis_chipinit(device_t dev);
174 static int ata_sis_allocate(device_t dev);
175 static void ata_sis_reset(device_t dev);
176 static void ata_sis_setmode(device_t dev, int mode);
177 static int ata_via_chipinit(device_t dev);
178 static int ata_via_allocate(device_t dev);
179 static void ata_via_reset(device_t dev);
180 static void ata_via_setmode(device_t dev, int mode);
181 static void ata_via_southbridge_fixup(device_t dev);
182 static void ata_via_family_setmode(device_t dev, int mode);
183 static void ata_set_desc(device_t dev);
184 static struct ata_chip_id *ata_match_chip(device_t dev, struct ata_chip_id *index);
185 static struct ata_chip_id *ata_find_chip(device_t dev, struct ata_chip_id *index, int slot);
186 static int ata_setup_interrupt(device_t dev);
187 static int ata_serialize(device_t dev, int flags);
188 static void ata_print_cable(device_t dev, u_int8_t *who);
189 static int ata_atapi(device_t dev);
190 static int ata_check_80pin(device_t dev, int mode);
191 static int ata_mode2idx(int mode);
192
193
194 /*
195 * generic ATA support functions
196 */
197 int
198 ata_generic_ident(device_t dev)
199 {
200 struct ata_pci_controller *ctlr = device_get_softc(dev);
201 char buffer[64];
202
203 sprintf(buffer, "%s ATA controller", ata_pcivendor2str(dev));
204 device_set_desc_copy(dev, buffer);
205 ctlr->chipinit = ata_generic_chipinit;
206 return 0;
207 }
208
209 static int
210 ata_generic_chipinit(device_t dev)
211 {
212 struct ata_pci_controller *ctlr = device_get_softc(dev);
213
214 if (ata_setup_interrupt(dev))
215 return ENXIO;
216 ctlr->setmode = ata_generic_setmode;
217 return 0;
218 }
219
220 static void
221 ata_generic_intr(void *data)
222 {
223 struct ata_pci_controller *ctlr = data;
224 struct ata_channel *ch;
225 int unit;
226
227 for (unit = 0; unit < ctlr->channels; unit++) {
228 if ((ch = ctlr->interrupt[unit].argument))
229 ctlr->interrupt[unit].function(ch);
230 }
231 }
232
233 static void
234 ata_generic_setmode(device_t dev, int mode)
235 {
236 struct ata_device *atadev = device_get_softc(dev);
237
238 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
239 mode = ata_check_80pin(dev, mode);
240 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
241 atadev->mode = mode;
242 }
243
244
245 /*
246 * SATA support functions
247 */
248 static void
249 ata_sata_phy_check_events(device_t dev)
250 {
251 struct ata_channel *ch = device_get_softc(dev);
252 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
253
254 /* clear error bits/interrupt */
255 ATA_IDX_OUTL(ch, ATA_SERROR, error);
256
257 /* do we have any events flagged ? */
258 if (error) {
259 struct ata_connect_task *tp;
260 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
261
262 /* if we have a connection event deal with it */
263 if ((error & ATA_SE_PHY_CHANGED) &&
264 (tp = (struct ata_connect_task *)
265 malloc(sizeof(struct ata_connect_task),
266 M_ATA, M_NOWAIT | M_ZERO))) {
267
268 if (((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) ||
269 ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)) {
270 if (bootverbose)
271 device_printf(ch->dev, "CONNECT requested\n");
272 tp->action = ATA_C_ATTACH;
273 }
274 else {
275 if (bootverbose)
276 device_printf(ch->dev, "DISCONNECT requested\n");
277 tp->action = ATA_C_DETACH;
278 }
279 tp->dev = ch->dev;
280 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
281 taskqueue_enqueue(taskqueue_thread, &tp->task);
282 }
283 }
284 }
285
286 static void
287 ata_sata_phy_event(void *context, int dummy)
288 {
289 struct ata_connect_task *tp = (struct ata_connect_task *)context;
290 struct ata_channel *ch = device_get_softc(tp->dev);
291 device_t *children;
292 int nchildren, i;
293
294 mtx_lock(&Giant); /* newbus suckage it needs Giant */
295 if (tp->action == ATA_C_ATTACH) {
296 if (bootverbose)
297 device_printf(tp->dev, "CONNECTED\n");
298 ATA_RESET(tp->dev);
299 ata_identify(tp->dev);
300 }
301 if (tp->action == ATA_C_DETACH) {
302 if (!device_get_children(tp->dev, &children, &nchildren)) {
303 for (i = 0; i < nchildren; i++)
304 if (children[i])
305 device_delete_child(tp->dev, children[i]);
306 free(children, M_TEMP);
307 }
308 mtx_lock(&ch->state_mtx);
309 ch->state = ATA_IDLE;
310 mtx_unlock(&ch->state_mtx);
311 if (bootverbose)
312 device_printf(tp->dev, "DISCONNECTED\n");
313 }
314 mtx_unlock(&Giant); /* suckage code dealt with, release Giant */
315 free(tp, M_ATA);
316 }
317
318 static int
319 ata_sata_phy_reset(device_t dev)
320 {
321 struct ata_channel *ch = device_get_softc(dev);
322 int loop, retry;
323
324 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE)
325 return ata_sata_connect(ch);
326
327 for (retry = 0; retry < 10; retry++) {
328 for (loop = 0; loop < 10; loop++) {
329 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_RESET);
330 ata_udelay(100);
331 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) ==
332 ATA_SC_DET_RESET)
333 break;
334 }
335 ata_udelay(5000);
336 for (loop = 0; loop < 10; loop++) {
337 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_IDLE |
338 ATA_SC_IPM_DIS_PARTIAL |
339 ATA_SC_IPM_DIS_SLUMBER);
340 ata_udelay(100);
341 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == 0)
342 return ata_sata_connect(ch);
343 }
344 }
345 return 0;
346 }
347
348 static int
349 ata_sata_connect(struct ata_channel *ch)
350 {
351 u_int32_t status;
352 int timeout;
353
354 /* wait up to 1 second for "connect well" */
355 for (timeout = 0; timeout < 100 ; timeout++) {
356 status = ATA_IDX_INL(ch, ATA_SSTATUS);
357 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1 ||
358 (status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)
359 break;
360 ata_udelay(10000);
361 }
362 if (timeout >= 100) {
363 if (bootverbose)
364 device_printf(ch->dev, "SATA connect status=%08x\n", status);
365 return 0;
366 }
367 if (bootverbose)
368 device_printf(ch->dev, "SATA connect time=%dms\n", timeout * 10);
369
370 /* clear SATA error register */
371 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
372
373 return 1;
374 }
375
376 static void
377 ata_sata_setmode(device_t dev, int mode)
378 {
379 struct ata_device *atadev = device_get_softc(dev);
380
381 /*
382 * if we detect that the device isn't a real SATA device we limit
383 * the transfer mode to UDMA5/ATA100.
384 * this works around the problems some devices has with the
385 * Marvell 88SX8030 SATA->PATA converters and UDMA6/ATA133.
386 */
387 if (atadev->param.satacapabilities != 0x0000 &&
388 atadev->param.satacapabilities != 0xffff) {
389 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
390
391 /* on some drives we need to set the transfer mode */
392 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
393 ata_limit_mode(dev, mode, ATA_UDMA6));
394
395 /* query SATA STATUS for the speed */
396 if (ch->r_io[ATA_SSTATUS].res &&
397 ((ATA_IDX_INL(ch, ATA_SSTATUS) & ATA_SS_CONWELL_MASK) ==
398 ATA_SS_CONWELL_GEN2))
399 atadev->mode = ATA_SA300;
400 else
401 atadev->mode = ATA_SA150;
402 }
403 else {
404 mode = ata_limit_mode(dev, mode, ATA_UDMA5);
405 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
406 atadev->mode = mode;
407 }
408 }
409
410 static int
411 ata_request2fis_h2d(struct ata_request *request, u_int8_t *fis)
412 {
413 struct ata_device *atadev = device_get_softc(request->dev);
414
415 if (request->flags & ATA_R_ATAPI) {
416 fis[0] = 0x27; /* host to device */
417 fis[1] = 0x80; /* command FIS (note PM goes here) */
418 fis[2] = ATA_PACKET_CMD;
419 if (request->flags & (ATA_R_READ | ATA_R_WRITE))
420 fis[3] = ATA_F_DMA;
421 else {
422 fis[5] = request->transfersize;
423 fis[6] = request->transfersize >> 8;
424 }
425 fis[7] = ATA_D_LBA | atadev->unit;
426 fis[15] = ATA_A_4BIT;
427 return 20;
428 }
429 else {
430 ata_modify_if_48bit(request);
431 fis[0] = 0x27; /* host to device */
432 fis[1] = 0x80; /* command FIS (note PM goes here) */
433 fis[2] = request->u.ata.command;
434 fis[3] = request->u.ata.feature;
435 fis[4] = request->u.ata.lba;
436 fis[5] = request->u.ata.lba >> 8;
437 fis[6] = request->u.ata.lba >> 16;
438 fis[7] = ATA_D_LBA | atadev->unit;
439 if (!(atadev->flags & ATA_D_48BIT_ACTIVE))
440 fis[7] |= (request->u.ata.lba >> 24 & 0x0f);
441 fis[8] = request->u.ata.lba >> 24;
442 fis[9] = request->u.ata.lba >> 32;
443 fis[10] = request->u.ata.lba >> 40;
444 fis[11] = request->u.ata.feature >> 8;
445 fis[12] = request->u.ata.count;
446 fis[13] = request->u.ata.count >> 8;
447 fis[15] = ATA_A_4BIT;
448 return 20;
449 }
450 return 0;
451 }
452
453
454 /*
455 * AHCI v1.x compliant SATA chipset support functions
456 */
457 int
458 ata_ahci_ident(device_t dev)
459 {
460 struct ata_pci_controller *ctlr = device_get_softc(dev);
461 char buffer[64];
462
463 /* is this PCI device flagged as an AHCI compliant chip ? */
464 if (pci_read_config(dev, PCIR_PROGIF, 1) != 0x01)
465 return ENXIO;
466
467 if (bootverbose)
468 sprintf(buffer, "%s (ID=%08x) AHCI controller",
469 ata_pcivendor2str(dev), pci_get_devid(dev));
470 else
471 sprintf(buffer, "%s AHCI controller", ata_pcivendor2str(dev));
472 device_set_desc_copy(dev, buffer);
473 ctlr->chipinit = ata_ahci_chipinit;
474 return 0;
475 }
476
477 static int
478 ata_ahci_chipinit(device_t dev)
479 {
480 struct ata_pci_controller *ctlr = device_get_softc(dev);
481 u_int32_t version;
482
483 /* if we have a memory BAR(5) we are likely on an AHCI part */
484 ctlr->r_type2 = SYS_RES_MEMORY;
485 ctlr->r_rid2 = PCIR_BAR(5);
486 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
487 &ctlr->r_rid2, RF_ACTIVE)))
488 return ENXIO;
489
490 /* setup interrupt delivery if not done allready by a vendor driver */
491 if (!ctlr->r_irq) {
492 if (ata_setup_interrupt(dev))
493 return ENXIO;
494 }
495 else
496 device_printf(dev, "AHCI called from vendor specific driver\n");
497
498 /* enable AHCI mode */
499 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC, ATA_AHCI_GHC_AE);
500
501 /* reset AHCI controller */
502 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC, ATA_AHCI_GHC_HR);
503 DELAY(1000000);
504 if (ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) & ATA_AHCI_GHC_HR) {
505 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
506 device_printf(dev, "AHCI controller reset failure\n");
507 return ENXIO;
508 }
509
510 /* reenable AHCI mode */
511 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC, ATA_AHCI_GHC_AE);
512
513 /* get the number of HW channels */
514 ctlr->channels =
515 MAX(flsl(ATA_INL(ctlr->r_res2, ATA_AHCI_PI)),
516 (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_NPMASK) + 1);
517
518 /* clear interrupts */
519 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, ATA_INL(ctlr->r_res2, ATA_AHCI_IS));
520
521 /* enable AHCI interrupts */
522 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
523 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) | ATA_AHCI_GHC_IE);
524
525 ctlr->reset = ata_ahci_reset;
526 ctlr->dmainit = ata_ahci_dmainit;
527 ctlr->allocate = ata_ahci_allocate;
528 ctlr->setmode = ata_sata_setmode;
529
530 /* enable PCI interrupt */
531 pci_write_config(dev, PCIR_COMMAND,
532 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
533
534 /* announce we support the HW */
535 version = ATA_INL(ctlr->r_res2, ATA_AHCI_VS);
536 device_printf(dev,
537 "AHCI Version %x%x.%x%x controller with %d ports detected\n",
538 (version >> 24) & 0xff, (version >> 16) & 0xff,
539 (version >> 8) & 0xff, version & 0xff,
540 (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_NPMASK) + 1);
541 return 0;
542 }
543
544 static int
545 ata_ahci_allocate(device_t dev)
546 {
547 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
548 struct ata_channel *ch = device_get_softc(dev);
549 u_int64_t work;
550 int offset = ch->unit << 7;
551
552 /* set the SATA resources */
553 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
554 ch->r_io[ATA_SSTATUS].offset = ATA_AHCI_P_SSTS + offset;
555 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
556 ch->r_io[ATA_SERROR].offset = ATA_AHCI_P_SERR + offset;
557 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
558 ch->r_io[ATA_SCONTROL].offset = ATA_AHCI_P_SCTL + offset;
559 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
560 ch->r_io[ATA_SACTIVE].offset = ATA_AHCI_P_SACT + offset;
561
562 ch->hw.status = ata_ahci_status;
563 ch->hw.begin_transaction = ata_ahci_begin_transaction;
564 ch->hw.end_transaction = ata_ahci_end_transaction;
565 ch->hw.command = NULL; /* not used here */
566
567 /* setup work areas */
568 work = ch->dma->work_bus + ATA_AHCI_CL_OFFSET;
569 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLB + offset, work & 0xffffffff);
570 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLBU + offset, work >> 32);
571
572 work = ch->dma->work_bus + ATA_AHCI_FB_OFFSET;
573 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FB + offset, work & 0xffffffff);
574 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FBU + offset, work >> 32);
575
576 /* enable wanted port interrupts */
577 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset,
578 (ATA_AHCI_P_IX_CPD | ATA_AHCI_P_IX_TFE | ATA_AHCI_P_IX_HBF |
579 ATA_AHCI_P_IX_HBD | ATA_AHCI_P_IX_IF | ATA_AHCI_P_IX_OF |
580 ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC | ATA_AHCI_P_IX_DP |
581 ATA_AHCI_P_IX_UF | ATA_AHCI_P_IX_SDB | ATA_AHCI_P_IX_DS |
582 ATA_AHCI_P_IX_PS | ATA_AHCI_P_IX_DHR));
583
584 /* start operations on this channel */
585 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
586 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
587 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
588 return 0;
589 }
590
591 static int
592 ata_ahci_status(device_t dev)
593 {
594 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
595 struct ata_channel *ch = device_get_softc(dev);
596 u_int32_t action = ATA_INL(ctlr->r_res2, ATA_AHCI_IS);
597 int offset = ch->unit << 7;
598 int tag = 0;
599
600 if (action & (1 << ch->unit)) {
601 u_int32_t istatus = ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset);
602 u_int32_t cstatus = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CI + offset);
603
604 /* clear interrupt(s) */
605 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, action & (1 << ch->unit));
606 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset, istatus);
607
608 /* do we have any PHY events ? */
609 /* XXX SOS check istatus phy bits */
610 ata_sata_phy_check_events(dev);
611
612 /* do we have a potentially hanging engine to take care of? */
613 if ((istatus & 0x78400050) && (cstatus & (1 << tag))) {
614
615 u_int32_t cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
616 int timeout = 0;
617
618 /* kill off all activity on this channel */
619 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
620 cmd & ~(ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
621
622 /* XXX SOS this is not entirely wrong */
623 do {
624 DELAY(1000);
625 if (timeout++ > 500) {
626 device_printf(dev, "stopping AHCI engine failed\n");
627 break;
628 }
629 } while (ATA_INL(ctlr->r_res2,
630 ATA_AHCI_P_CMD + offset) & ATA_AHCI_P_CMD_CR);
631
632 /* start operations on this channel */
633 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
634 cmd | (ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
635
636 return 1;
637 }
638 else
639 return (!(cstatus & (1 << tag)));
640 }
641 return 0;
642 }
643
644 /* must be called with ATA channel locked and state_mtx held */
645 static int
646 ata_ahci_begin_transaction(struct ata_request *request)
647 {
648 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
649 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
650 struct ata_ahci_cmd_tab *ctp;
651 struct ata_ahci_cmd_list *clp;
652 int offset = ch->unit << 7;
653 int tag = 0, entries = 0;
654 int fis_size;
655
656 /* get a piece of the workspace for this request */
657 ctp = (struct ata_ahci_cmd_tab *)
658 (ch->dma->work + ATA_AHCI_CT_OFFSET + (ATA_AHCI_CT_SIZE * tag));
659
660 /* setup the FIS for this request */
661 if (!(fis_size = ata_ahci_setup_fis(ctp, request))) {
662 device_printf(request->dev, "setting up SATA FIS failed\n");
663 request->result = EIO;
664 return ATA_OP_FINISHED;
665 }
666
667 /* if request moves data setup and load SG list */
668 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
669 if (ch->dma->load(ch->dev, request->data, request->bytecount,
670 request->flags & ATA_R_READ,
671 ctp->prd_tab, &entries)) {
672 device_printf(request->dev, "setting up DMA failed\n");
673 request->result = EIO;
674 return ATA_OP_FINISHED;
675 }
676 }
677
678 /* setup the command list entry */
679 clp = (struct ata_ahci_cmd_list *)
680 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
681
682 clp->prd_length = entries;
683 clp->cmd_flags = (request->flags & ATA_R_WRITE ? (1<<6) : 0) |
684 (request->flags & ATA_R_ATAPI ? ((1<<5) | (1<<7)) : 0) |
685 (fis_size / sizeof(u_int32_t));
686 clp->bytecount = 0;
687 clp->cmd_table_phys = htole64(ch->dma->work_bus + ATA_AHCI_CT_OFFSET +
688 (ATA_AHCI_CT_SIZE * tag));
689
690 /* clear eventual ACTIVE bit */
691 ATA_IDX_OUTL(ch, ATA_SACTIVE, ATA_IDX_INL(ch, ATA_SACTIVE) & (1 << tag));
692
693 /* set command type bit */
694 if (request->flags & ATA_R_ATAPI)
695 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
696 ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) |
697 ATA_AHCI_P_CMD_ATAPI);
698 else
699 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
700 ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) &
701 ~ATA_AHCI_P_CMD_ATAPI);
702
703 /* issue command to controller */
704 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CI + offset, (1 << tag));
705
706 if (!(request->flags & ATA_R_ATAPI)) {
707 /* device reset doesn't interrupt */
708 if (request->u.ata.command == ATA_DEVICE_RESET) {
709 u_int32_t tf_data;
710 int timeout = 1000000;
711
712 do {
713 DELAY(10);
714 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + (ch->unit<<7));
715 } while ((tf_data & ATA_S_BUSY) && timeout--);
716 if (bootverbose)
717 device_printf(ch->dev, "device_reset timeout=%dus\n",
718 (1000000-timeout)*10);
719 request->status = tf_data;
720 if (request->status & ATA_S_ERROR)
721 request->error = tf_data >> 8;
722 return ATA_OP_FINISHED;
723 }
724 }
725
726 /* start the timeout */
727 callout_reset(&request->callout, request->timeout * hz,
728 (timeout_t*)ata_timeout, request);
729 return ATA_OP_CONTINUES;
730 }
731
732 /* must be called with ATA channel locked and state_mtx held */
733 static int
734 ata_ahci_end_transaction(struct ata_request *request)
735 {
736 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
737 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
738 struct ata_ahci_cmd_list *clp;
739 u_int32_t tf_data;
740 int offset = ch->unit << 7;
741 int tag = 0;
742
743 /* kill the timeout */
744 callout_stop(&request->callout);
745
746 /* get status */
747 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + offset);
748 request->status = tf_data;
749
750 /* if error status get details */
751 if (request->status & ATA_S_ERROR)
752 request->error = tf_data >> 8;
753
754 /* record how much data we actually moved */
755 clp = (struct ata_ahci_cmd_list *)
756 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
757 request->donecount = clp->bytecount;
758
759 /* release SG list etc */
760 ch->dma->unload(ch->dev);
761
762 return ATA_OP_FINISHED;
763 }
764
765 static void
766 ata_ahci_reset(device_t dev)
767 {
768 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
769 struct ata_channel *ch = device_get_softc(dev);
770 u_int32_t cmd, signature;
771 int offset = ch->unit << 7;
772 int timeout;
773
774 if (!(ATA_INL(ctlr->r_res2, ATA_AHCI_PI) & (1 << ch->unit))) {
775 device_printf(dev, "port not implemented\n");
776 return;
777 }
778 ch->devices = 0;
779
780 /* kill off all activity on this channel */
781 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
782 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
783 cmd & ~(ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
784
785 /* XXX SOS this is not entirely wrong */
786 timeout = 0;
787 do {
788 DELAY(1000);
789 if (timeout++ > 500) {
790 device_printf(dev, "stopping AHCI engine failed\n");
791 break;
792 }
793 }
794 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) & ATA_AHCI_P_CMD_CR);
795
796 /* issue Command List Override if supported */
797 if (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_CAP_CLO) {
798 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
799 cmd |= ATA_AHCI_P_CMD_CLO;
800 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, cmd);
801 timeout = 0;
802 do {
803 DELAY(1000);
804 if (timeout++ > 500) {
805 device_printf(dev, "executing CLO failed\n");
806 break;
807 }
808 }
809 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD+offset)&ATA_AHCI_P_CMD_CLO);
810 }
811
812 /* reset PHY and decide what is present */
813 if (ata_sata_phy_reset(dev)) {
814
815 /* clear any interrupts pending on this channel */
816 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset,
817 ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset));
818
819 /* clear SATA error register */
820 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
821
822 /* start operations on this channel */
823 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
824 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
825 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
826
827 signature = ATA_INL(ctlr->r_res2, ATA_AHCI_P_SIG + offset);
828 if (bootverbose)
829 device_printf(dev, "SIGNATURE: %08x\n", signature);
830 switch (signature) {
831 case 0x00000101:
832 ch->devices = ATA_ATA_MASTER;
833 break;
834 case 0x96690101:
835 ch->devices = ATA_PORTMULTIPLIER;
836 device_printf(ch->dev, "Portmultipliers not supported yet\n");
837 ch->devices = 0;
838 break;
839 case 0xeb140101:
840 ch->devices = ATA_ATAPI_MASTER;
841 break;
842 default: /* SOS XXX */
843 if (bootverbose)
844 device_printf(ch->dev, "No signature, asuming disk device\n");
845 ch->devices = ATA_ATA_MASTER;
846 }
847 }
848 if (bootverbose)
849 device_printf(dev, "ahci_reset devices=0x%b\n", ch->devices,
850 "\2\4ATAPI_SLAVE\3ATAPI_MASTER\2ATA_SLAVE\1ATA_MASTER");
851 }
852
853 static void
854 ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
855 {
856 struct ata_dmasetprd_args *args = xsc;
857 struct ata_ahci_dma_prd *prd = args->dmatab;
858 int i;
859
860 if (!(args->error = error)) {
861 for (i = 0; i < nsegs; i++) {
862 prd[i].dba = htole64(segs[i].ds_addr);
863 prd[i].dbc = htole32((segs[i].ds_len - 1) & ATA_AHCI_PRD_MASK);
864 }
865 }
866 KASSERT(nsegs <= ATA_DMA_ENTRIES, ("too many DMA segment entries\n"));
867 args->nsegs = nsegs;
868 }
869
870 static void
871 ata_ahci_dmainit(device_t dev)
872 {
873 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
874 struct ata_channel *ch = device_get_softc(dev);
875
876 ata_dmainit(dev);
877 if (ch->dma) {
878 /* note start and stop are not used here */
879 ch->dma->setprd = ata_ahci_dmasetprd;
880 ch->dma->max_iosize = 8192 * DEV_BSIZE;
881 if (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_CAP_64BIT)
882 ch->dma->max_address = BUS_SPACE_MAXADDR;
883 }
884 }
885
886 static int
887 ata_ahci_setup_fis(struct ata_ahci_cmd_tab *ctp, struct ata_request *request)
888 {
889 bzero(ctp->cfis, 64);
890 if (request->flags & ATA_R_ATAPI) {
891 bzero(ctp->acmd, 32);
892 bcopy(request->u.atapi.ccb, ctp->acmd, 16);
893 }
894 return ata_request2fis_h2d(request, &ctp->cfis[0]);
895 }
896
897
898 /*
899 * Acard chipset support functions
900 */
901 int
902 ata_acard_ident(device_t dev)
903 {
904 struct ata_pci_controller *ctlr = device_get_softc(dev);
905 static struct ata_chip_id ids[] =
906 {{ ATA_ATP850R, 0, ATPOLD, 0x00, ATA_UDMA2, "ATP850" },
907 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "ATP860A" },
908 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "ATP860R" },
909 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "ATP865A" },
910 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "ATP865R" },
911 { 0, 0, 0, 0, 0, 0}};
912
913 if (!(ctlr->chip = ata_match_chip(dev, ids)))
914 return ENXIO;
915
916 ata_set_desc(dev);
917 ctlr->chipinit = ata_acard_chipinit;
918 return 0;
919 }
920
921 static int
922 ata_acard_chipinit(device_t dev)
923 {
924 struct ata_pci_controller *ctlr = device_get_softc(dev);
925
926 if (ata_setup_interrupt(dev))
927 return ENXIO;
928
929 ctlr->allocate = ata_acard_allocate;
930 if (ctlr->chip->cfg1 == ATPOLD) {
931 ctlr->setmode = ata_acard_850_setmode;
932 ctlr->locking = ata_serialize;
933 }
934 else
935 ctlr->setmode = ata_acard_86X_setmode;
936 return 0;
937 }
938
939 static int
940 ata_acard_allocate(device_t dev)
941 {
942 struct ata_channel *ch = device_get_softc(dev);
943
944 /* setup the usual register normal pci style */
945 if (ata_pci_allocate(dev))
946 return ENXIO;
947
948 ch->hw.status = ata_acard_status;
949 return 0;
950 }
951
952 static int
953 ata_acard_status(device_t dev)
954 {
955 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
956 struct ata_channel *ch = device_get_softc(dev);
957
958 if (ctlr->chip->cfg1 == ATPOLD &&
959 ATA_LOCKING(ch->dev, ATA_LF_WHICH) != ch->unit)
960 return 0;
961 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
962 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
963
964 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
965 ATA_BMSTAT_INTERRUPT)
966 return 0;
967 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
968 DELAY(1);
969 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
970 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
971 DELAY(1);
972 }
973 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
974 DELAY(100);
975 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
976 return 0;
977 }
978 return 1;
979 }
980
981 static void
982 ata_acard_850_setmode(device_t dev, int mode)
983 {
984 device_t gparent = GRANDPARENT(dev);
985 struct ata_pci_controller *ctlr = device_get_softc(gparent);
986 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
987 struct ata_device *atadev = device_get_softc(dev);
988 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
989 int error;
990
991 mode = ata_limit_mode(dev, mode,
992 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
993
994 /* XXX SOS missing WDMA0+1 + PIO modes */
995 if (mode >= ATA_WDMA2) {
996 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
997 if (bootverbose)
998 device_printf(dev, "%ssetting %s on %s chip\n",
999 (error) ? "FAILURE " : "",
1000 ata_mode2str(mode), ctlr->chip->text);
1001 if (!error) {
1002 u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
1003
1004 reg54 &= ~(0x03 << (devno << 1));
1005 if (mode >= ATA_UDMA0)
1006 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
1007 pci_write_config(gparent, 0x54, reg54, 1);
1008 pci_write_config(gparent, 0x4a, 0xa6, 1);
1009 pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2);
1010 atadev->mode = mode;
1011 return;
1012 }
1013 }
1014 /* we could set PIO mode timings, but we assume the BIOS did that */
1015 }
1016
1017 static void
1018 ata_acard_86X_setmode(device_t dev, int mode)
1019 {
1020 device_t gparent = GRANDPARENT(dev);
1021 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1022 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1023 struct ata_device *atadev = device_get_softc(dev);
1024 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1025 int error;
1026
1027
1028 mode = ata_limit_mode(dev, mode,
1029 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
1030
1031 mode = ata_check_80pin(dev, mode);
1032
1033 /* XXX SOS missing WDMA0+1 + PIO modes */
1034 if (mode >= ATA_WDMA2) {
1035 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1036 if (bootverbose)
1037 device_printf(dev, "%ssetting %s on %s chip\n",
1038 (error) ? "FAILURE " : "",
1039 ata_mode2str(mode), ctlr->chip->text);
1040 if (!error) {
1041 u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
1042
1043 reg44 &= ~(0x000f << (devno << 2));
1044 if (mode >= ATA_UDMA0)
1045 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
1046 pci_write_config(gparent, 0x44, reg44, 2);
1047 pci_write_config(gparent, 0x4a, 0xa6, 1);
1048 pci_write_config(gparent, 0x40 + devno, 0x31, 1);
1049 atadev->mode = mode;
1050 return;
1051 }
1052 }
1053 /* we could set PIO mode timings, but we assume the BIOS did that */
1054 }
1055
1056
1057 /*
1058 * Acer Labs Inc (ALI) chipset support functions
1059 */
1060 int
1061 ata_ali_ident(device_t dev)
1062 {
1063 struct ata_pci_controller *ctlr = device_get_softc(dev);
1064 static struct ata_chip_id ids[] =
1065 {{ ATA_ALI_5289, 0x00, 2, ALISATA, ATA_SA150, "M5289" },
1066 { ATA_ALI_5288, 0x00, 4, ALISATA, ATA_SA300, "M5288" },
1067 { ATA_ALI_5287, 0x00, 4, ALISATA, ATA_SA150, "M5287" },
1068 { ATA_ALI_5281, 0x00, 2, ALISATA, ATA_SA150, "M5281" },
1069 { ATA_ALI_5229, 0xc5, 0, ALINEW, ATA_UDMA6, "M5229" },
1070 { ATA_ALI_5229, 0xc4, 0, ALINEW, ATA_UDMA5, "M5229" },
1071 { ATA_ALI_5229, 0xc2, 0, ALINEW, ATA_UDMA4, "M5229" },
1072 { ATA_ALI_5229, 0x20, 0, ALIOLD, ATA_UDMA2, "M5229" },
1073 { ATA_ALI_5229, 0x00, 0, ALIOLD, ATA_WDMA2, "M5229" },
1074 { 0, 0, 0, 0, 0, 0}};
1075
1076 if (!(ctlr->chip = ata_match_chip(dev, ids)))
1077 return ENXIO;
1078
1079 ata_set_desc(dev);
1080 ctlr->chipinit = ata_ali_chipinit;
1081 return 0;
1082 }
1083
1084 static int
1085 ata_ali_chipinit(device_t dev)
1086 {
1087 struct ata_pci_controller *ctlr = device_get_softc(dev);
1088
1089 if (ata_setup_interrupt(dev))
1090 return ENXIO;
1091
1092 switch (ctlr->chip->cfg2) {
1093 case ALISATA:
1094 ctlr->channels = ctlr->chip->cfg1;
1095 ctlr->allocate = ata_ali_sata_allocate;
1096 ctlr->setmode = ata_sata_setmode;
1097
1098 /* AHCI mode is correctly supported only on the ALi 5288. */
1099 if ((ctlr->chip->chipid == ATA_ALI_5288) &&
1100 (ata_ahci_chipinit(dev) != ENXIO))
1101 return 0;
1102
1103 /* enable PCI interrupt */
1104 pci_write_config(dev, PCIR_COMMAND,
1105 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1106 break;
1107
1108 case ALINEW:
1109 /* use device interrupt as byte count end */
1110 pci_write_config(dev, 0x4a, pci_read_config(dev, 0x4a, 1) | 0x20, 1);
1111
1112 /* enable cable detection and UDMA support on newer chips */
1113 pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
1114
1115 /* enable ATAPI UDMA mode */
1116 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x01, 1);
1117
1118 /* only chips with revision > 0xc4 can do 48bit DMA */
1119 if (ctlr->chip->chiprev <= 0xc4)
1120 device_printf(dev,
1121 "using PIO transfers above 137GB as workaround for "
1122 "48bit DMA access bug, expect reduced performance\n");
1123 ctlr->allocate = ata_ali_allocate;
1124 ctlr->reset = ata_ali_reset;
1125 ctlr->setmode = ata_ali_setmode;
1126 break;
1127
1128 case ALIOLD:
1129 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
1130 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x03, 1);
1131 ctlr->setmode = ata_ali_setmode;
1132 break;
1133 }
1134 return 0;
1135 }
1136
1137 static int
1138 ata_ali_allocate(device_t dev)
1139 {
1140 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1141 struct ata_channel *ch = device_get_softc(dev);
1142
1143 /* setup the usual register normal pci style */
1144 if (ata_pci_allocate(dev))
1145 return ENXIO;
1146
1147 /* older chips can't do 48bit DMA transfers */
1148 if (ctlr->chip->chiprev <= 0xc4)
1149 ch->flags |= ATA_NO_48BIT_DMA;
1150
1151 return 0;
1152 }
1153
1154 static int
1155 ata_ali_sata_allocate(device_t dev)
1156 {
1157 device_t parent = device_get_parent(dev);
1158 struct ata_pci_controller *ctlr = device_get_softc(parent);
1159 struct ata_channel *ch = device_get_softc(dev);
1160 struct resource *io = NULL, *ctlio = NULL;
1161 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
1162 int i, rid;
1163
1164 rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
1165 io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
1166 if (!io)
1167 return ENXIO;
1168
1169 rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
1170 ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
1171 if (!ctlio) {
1172 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
1173 return ENXIO;
1174 }
1175
1176 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
1177 ch->r_io[i].res = io;
1178 ch->r_io[i].offset = i + (unit10 ? 8 : 0);
1179 }
1180 ch->r_io[ATA_CONTROL].res = ctlio;
1181 ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);
1182 ch->r_io[ATA_IDX_ADDR].res = io;
1183 ata_default_registers(dev);
1184 if (ctlr->r_res1) {
1185 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
1186 ch->r_io[i].res = ctlr->r_res1;
1187 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
1188 }
1189 }
1190 ch->flags |= ATA_NO_SLAVE;
1191
1192 /* XXX SOS PHY handling awkward in ALI chip not supported yet */
1193 ata_pci_hw(dev);
1194 return 0;
1195 }
1196
1197 static void
1198 ata_ali_reset(device_t dev)
1199 {
1200 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1201 struct ata_channel *ch = device_get_softc(dev);
1202 device_t *children;
1203 int nchildren, i;
1204
1205 ata_generic_reset(dev);
1206
1207 /*
1208 * workaround for datacorruption bug found on at least SUN Blade-100
1209 * find the ISA function on the southbridge and disable then enable
1210 * the ATA channel tristate buffer
1211 */
1212 if (ctlr->chip->chiprev == 0xc3 || ctlr->chip->chiprev == 0xc2) {
1213 if (!device_get_children(GRANDPARENT(dev), &children, &nchildren)) {
1214 for (i = 0; i < nchildren; i++) {
1215 if (pci_get_devid(children[i]) == ATA_ALI_1533) {
1216 pci_write_config(children[i], 0x58,
1217 pci_read_config(children[i], 0x58, 1) &
1218 ~(0x04 << ch->unit), 1);
1219 pci_write_config(children[i], 0x58,
1220 pci_read_config(children[i], 0x58, 1) |
1221 (0x04 << ch->unit), 1);
1222 break;
1223 }
1224 }
1225 free(children, M_TEMP);
1226 }
1227 }
1228 }
1229
1230 static void
1231 ata_ali_setmode(device_t dev, int mode)
1232 {
1233 device_t gparent = GRANDPARENT(dev);
1234 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1235 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1236 struct ata_device *atadev = device_get_softc(dev);
1237 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1238 int error;
1239
1240 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1241
1242 if (ctlr->chip->cfg2 & ALINEW) {
1243 if (mode > ATA_UDMA2 &&
1244 pci_read_config(gparent, 0x4a, 1) & (1 << ch->unit)) {
1245 ata_print_cable(dev, "controller");
1246 mode = ATA_UDMA2;
1247 }
1248 }
1249 else
1250 mode = ata_check_80pin(dev, mode);
1251
1252 if (ctlr->chip->cfg2 & ALIOLD) {
1253 /* doesn't support ATAPI DMA on write */
1254 ch->flags |= ATA_ATAPI_DMA_RO;
1255 if (ch->devices & ATA_ATAPI_MASTER && ch->devices & ATA_ATAPI_SLAVE) {
1256 /* doesn't support ATAPI DMA on two ATAPI devices */
1257 device_printf(dev, "two atapi devices on this channel, no DMA\n");
1258 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1259 }
1260 }
1261
1262 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1263
1264 if (bootverbose)
1265 device_printf(dev, "%ssetting %s on %s chip\n",
1266 (error) ? "FAILURE " : "",
1267 ata_mode2str(mode), ctlr->chip->text);
1268 if (!error) {
1269 if (mode >= ATA_UDMA0) {
1270 u_int8_t udma[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0d};
1271 u_int32_t word54 = pci_read_config(gparent, 0x54, 4);
1272
1273 word54 &= ~(0x000f000f << (devno << 2));
1274 word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));
1275 pci_write_config(gparent, 0x54, word54, 4);
1276 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1277 0x00310001, 4);
1278 }
1279 else {
1280 u_int32_t piotimings[] =
1281 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
1282 0x00310001, 0x00440001, 0x00330001, 0x00310001};
1283
1284 pci_write_config(gparent, 0x54, pci_read_config(gparent, 0x54, 4) &
1285 ~(0x0008000f << (devno << 2)), 4);
1286 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1287 piotimings[ata_mode2idx(mode)], 4);
1288 }
1289 atadev->mode = mode;
1290 }
1291 }
1292
1293
1294 /*
1295 * American Micro Devices (AMD) chipset support functions
1296 */
1297 int
1298 ata_amd_ident(device_t dev)
1299 {
1300 struct ata_pci_controller *ctlr = device_get_softc(dev);
1301 static struct ata_chip_id ids[] =
1302 {{ ATA_AMD756, 0x00, AMDNVIDIA, 0x00, ATA_UDMA4, "756" },
1303 { ATA_AMD766, 0x00, AMDNVIDIA, AMDCABLE|AMDBUG, ATA_UDMA5, "766" },
1304 { ATA_AMD768, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA5, "768" },
1305 { ATA_AMD8111, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA6, "8111" },
1306 { ATA_AMD5536, 0x00, AMDNVIDIA, 0x00, ATA_UDMA5, "CS5536" },
1307 { 0, 0, 0, 0, 0, 0}};
1308
1309 if (!(ctlr->chip = ata_match_chip(dev, ids)))
1310 return ENXIO;
1311
1312 ata_set_desc(dev);
1313 ctlr->chipinit = ata_amd_chipinit;
1314 return 0;
1315 }
1316
1317 static int
1318 ata_amd_chipinit(device_t dev)
1319 {
1320 struct ata_pci_controller *ctlr = device_get_softc(dev);
1321
1322 if (ata_setup_interrupt(dev))
1323 return ENXIO;
1324
1325 /* disable/set prefetch, postwrite */
1326 if (ctlr->chip->cfg2 & AMDBUG)
1327 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) & 0x0f, 1);
1328 else
1329 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
1330
1331 ctlr->setmode = ata_via_family_setmode;
1332 return 0;
1333 }
1334
1335
1336 /*
1337 * ATI chipset support functions
1338 */
1339 int
1340 ata_ati_ident(device_t dev)
1341 {
1342 struct ata_pci_controller *ctlr = device_get_softc(dev);
1343 static struct ata_chip_id ids[] =
1344 {{ ATA_ATI_IXP200, 0x00, 0, ATIPATA, ATA_UDMA5, "IXP200" },
1345 { ATA_ATI_IXP300, 0x00, 0, ATIPATA, ATA_UDMA6, "IXP300" },
1346 { ATA_ATI_IXP300_S1, 0x00, 0, ATISATA, ATA_SA150, "IXP300" },
1347 { ATA_ATI_IXP400, 0x00, 0, ATIPATA, ATA_UDMA6, "IXP400" },
1348 { ATA_ATI_IXP400_S1, 0x00, 0, ATISATA, ATA_SA150, "IXP400" },
1349 { ATA_ATI_IXP400_S2, 0x00, 0, ATISATA, ATA_SA150, "IXP400" },
1350 { ATA_ATI_IXP600, 0x00, 0, ATIPATA, ATA_UDMA6, "IXP600" },
1351 { ATA_ATI_IXP600_S1, 0x00, 0, ATIAHCI, ATA_SA300, "IXP600" },
1352 { ATA_ATI_IXP700, 0x00, 0, ATIPATA, ATA_UDMA6, "IXP700" },
1353 { ATA_ATI_IXP700_S1, 0x00, 0, ATIAHCI, ATA_SA300, "IXP700" },
1354 { 0, 0, 0, 0, 0, 0}};
1355
1356 if (!(ctlr->chip = ata_match_chip(dev, ids)))
1357 return ENXIO;
1358
1359 ata_set_desc(dev);
1360
1361 switch (ctlr->chip->cfg2) {
1362 case ATIPATA:
1363 ctlr->chipinit = ata_ati_chipinit;
1364 break;
1365 case ATISATA:
1366 /* the ATI SATA controller is actually a SiI 3112 controller */
1367 ctlr->chip->cfg1 = SIIMEMIO;
1368 ctlr->chipinit = ata_sii_chipinit;
1369 break;
1370 case ATIAHCI:
1371 ctlr->chipinit = ata_ahci_chipinit;
1372 break;
1373 }
1374 return 0;
1375 }
1376
1377 static int
1378 ata_ati_chipinit(device_t dev)
1379 {
1380 struct ata_pci_controller *ctlr = device_get_softc(dev);
1381
1382 if (ata_setup_interrupt(dev))
1383 return ENXIO;
1384
1385 /* IXP600 & IXP700 only have 1 PATA channel */
1386 if ((ctlr->chip->chipid == ATA_ATI_IXP600) ||
1387 (ctlr->chip->chipid == ATA_ATI_IXP700))
1388 ctlr->channels = 1;
1389
1390 ctlr->setmode = ata_ati_setmode;
1391 return 0;
1392 }
1393
1394 static void
1395 ata_ati_setmode(device_t dev, int mode)
1396 {
1397 device_t gparent = GRANDPARENT(dev);
1398 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1399 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1400 struct ata_device *atadev = device_get_softc(dev);
1401 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1402 int offset = (devno ^ 0x01) << 3;
1403 int error;
1404 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
1405 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
1406 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
1407
1408 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1409
1410 mode = ata_check_80pin(dev, mode);
1411
1412 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1413
1414 if (bootverbose)
1415 device_printf(dev, "%ssetting %s on %s chip\n",
1416 (error) ? "FAILURE " : "",
1417 ata_mode2str(mode), ctlr->chip->text);
1418 if (!error) {
1419 if (mode >= ATA_UDMA0) {
1420 pci_write_config(gparent, 0x56,
1421 (pci_read_config(gparent, 0x56, 2) &
1422 ~(0xf << (devno << 2))) |
1423 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
1424 pci_write_config(gparent, 0x54,
1425 pci_read_config(gparent, 0x54, 1) |
1426 (0x01 << devno), 1);
1427 pci_write_config(gparent, 0x44,
1428 (pci_read_config(gparent, 0x44, 4) &
1429 ~(0xff << offset)) |
1430 (dmatimings[2] << offset), 4);
1431 }
1432 else if (mode >= ATA_WDMA0) {
1433 pci_write_config(gparent, 0x54,
1434 pci_read_config(gparent, 0x54, 1) &
1435 ~(0x01 << devno), 1);
1436 pci_write_config(gparent, 0x44,
1437 (pci_read_config(gparent, 0x44, 4) &
1438 ~(0xff << offset)) |
1439 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
1440 }
1441 else
1442 pci_write_config(gparent, 0x54,
1443 pci_read_config(gparent, 0x54, 1) &
1444 ~(0x01 << devno), 1);
1445
1446 pci_write_config(gparent, 0x4a,
1447 (pci_read_config(gparent, 0x4a, 2) &
1448 ~(0xf << (devno << 2))) |
1449 (((mode - ATA_PIO0) & ATA_MODE_MASK) << (devno<<2)),2);
1450 pci_write_config(gparent, 0x40,
1451 (pci_read_config(gparent, 0x40, 4) &
1452 ~(0xff << offset)) |
1453 (piotimings[ata_mode2idx(mode)] << offset), 4);
1454 atadev->mode = mode;
1455 }
1456 }
1457
1458
1459 /*
1460 * Cyrix chipset support functions
1461 */
1462 int
1463 ata_cyrix_ident(device_t dev)
1464 {
1465 struct ata_pci_controller *ctlr = device_get_softc(dev);
1466
1467 if (pci_get_devid(dev) == ATA_CYRIX_5530) {
1468 device_set_desc(dev, "Cyrix 5530 ATA33 controller");
1469 ctlr->chipinit = ata_cyrix_chipinit;
1470 return 0;
1471 }
1472 return ENXIO;
1473 }
1474
1475 static int
1476 ata_cyrix_chipinit(device_t dev)
1477 {
1478 struct ata_pci_controller *ctlr = device_get_softc(dev);
1479
1480 if (ata_setup_interrupt(dev))
1481 return ENXIO;
1482
1483 if (ctlr->r_res1)
1484 ctlr->setmode = ata_cyrix_setmode;
1485 else
1486 ctlr->setmode = ata_generic_setmode;
1487 return 0;
1488 }
1489
1490 static void
1491 ata_cyrix_setmode(device_t dev, int mode)
1492 {
1493 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1494 struct ata_device *atadev = device_get_softc(dev);
1495 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1496 u_int32_t piotiming[] =
1497 { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
1498 u_int32_t dmatiming[] = { 0x00077771, 0x00012121, 0x00002020 };
1499 u_int32_t udmatiming[] = { 0x00921250, 0x00911140, 0x00911030 };
1500 int error;
1501
1502 ch->dma->alignment = 16;
1503 ch->dma->max_iosize = 64 * DEV_BSIZE;
1504
1505 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1506
1507 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1508
1509 if (bootverbose)
1510 device_printf(dev, "%ssetting %s on Cyrix chip\n",
1511 (error) ? "FAILURE " : "", ata_mode2str(mode));
1512 if (!error) {
1513 if (mode >= ATA_UDMA0) {
1514 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1515 0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
1516 }
1517 else if (mode >= ATA_WDMA0) {
1518 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1519 0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
1520 }
1521 else {
1522 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1523 0x20 + (devno << 3), piotiming[mode & ATA_MODE_MASK]);
1524 }
1525 atadev->mode = mode;
1526 }
1527 }
1528
1529
1530 /*
1531 * Cypress chipset support functions
1532 */
1533 int
1534 ata_cypress_ident(device_t dev)
1535 {
1536 struct ata_pci_controller *ctlr = device_get_softc(dev);
1537
1538 /*
1539 * the Cypress chip is a mess, it contains two ATA functions, but
1540 * both channels are visible on the first one.
1541 * simply ignore the second function for now, as the right
1542 * solution (ignoring the second channel on the first function)
1543 * doesn't work with the crappy ATA interrupt setup on the alpha.
1544 */
1545 if (pci_get_devid(dev) == ATA_CYPRESS_82C693 &&
1546 pci_get_function(dev) == 1 &&
1547 pci_get_subclass(dev) == PCIS_STORAGE_IDE) {
1548 device_set_desc(dev, "Cypress 82C693 ATA controller");
1549 ctlr->chipinit = ata_cypress_chipinit;
1550 return 0;
1551 }
1552 return ENXIO;
1553 }
1554
1555 static int
1556 ata_cypress_chipinit(device_t dev)
1557 {
1558 struct ata_pci_controller *ctlr = device_get_softc(dev);
1559
1560 if (ata_setup_interrupt(dev))
1561 return ENXIO;
1562
1563 ctlr->setmode = ata_cypress_setmode;
1564 return 0;
1565 }
1566
1567 static void
1568 ata_cypress_setmode(device_t dev, int mode)
1569 {
1570 device_t gparent = GRANDPARENT(dev);
1571 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1572 struct ata_device *atadev = device_get_softc(dev);
1573 int error;
1574
1575 mode = ata_limit_mode(dev, mode, ATA_WDMA2);
1576
1577 /* XXX SOS missing WDMA0+1 + PIO modes */
1578 if (mode == ATA_WDMA2) {
1579 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1580 if (bootverbose)
1581 device_printf(dev, "%ssetting WDMA2 on Cypress chip\n",
1582 error ? "FAILURE " : "");
1583 if (!error) {
1584 pci_write_config(gparent, ch->unit ? 0x4e : 0x4c, 0x2020, 2);
1585 atadev->mode = mode;
1586 return;
1587 }
1588 }
1589 /* we could set PIO mode timings, but we assume the BIOS did that */
1590 }
1591
1592
1593 /*
1594 * HighPoint chipset support functions
1595 */
1596 int
1597 ata_highpoint_ident(device_t dev)
1598 {
1599 struct ata_pci_controller *ctlr = device_get_softc(dev);
1600 struct ata_chip_id *idx;
1601 static struct ata_chip_id ids[] =
1602 {{ ATA_HPT374, 0x07, HPT374, 0x00, ATA_UDMA6, "HPT374" },
1603 { ATA_HPT372, 0x02, HPT372, 0x00, ATA_UDMA6, "HPT372N" },
1604 { ATA_HPT372, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT372" },
1605 { ATA_HPT371, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT371" },
1606 { ATA_HPT366, 0x05, HPT372, 0x00, ATA_UDMA6, "HPT372" },
1607 { ATA_HPT366, 0x03, HPT370, 0x00, ATA_UDMA5, "HPT370" },
1608 { ATA_HPT366, 0x02, HPT366, 0x00, ATA_UDMA4, "HPT368" },
1609 { ATA_HPT366, 0x00, HPT366, HPTOLD, ATA_UDMA4, "HPT366" },
1610 { ATA_HPT302, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT302" },
1611 { 0, 0, 0, 0, 0, 0}};
1612 char buffer[64];
1613
1614 if (!(idx = ata_match_chip(dev, ids)))
1615 return ENXIO;
1616
1617 strcpy(buffer, "HighPoint ");
1618 strcat(buffer, idx->text);
1619 if (idx->cfg1 == HPT374) {
1620 if (pci_get_function(dev) == 0)
1621 strcat(buffer, " (channel 0+1)");
1622 if (pci_get_function(dev) == 1)
1623 strcat(buffer, " (channel 2+3)");
1624 }
1625 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
1626 device_set_desc_copy(dev, buffer);
1627 ctlr->chip = idx;
1628 ctlr->chipinit = ata_highpoint_chipinit;
1629 return 0;
1630 }
1631
1632 static int
1633 ata_highpoint_chipinit(device_t dev)
1634 {
1635 struct ata_pci_controller *ctlr = device_get_softc(dev);
1636
1637 if (ata_setup_interrupt(dev))
1638 return ENXIO;
1639
1640 if (ctlr->chip->cfg2 == HPTOLD) {
1641 /* disable interrupt prediction */
1642 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
1643 }
1644 else {
1645 /* disable interrupt prediction */
1646 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
1647 pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
1648
1649 /* enable interrupts */
1650 pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
1651
1652 /* set clocks etc */
1653 if (ctlr->chip->cfg1 < HPT372)
1654 pci_write_config(dev, 0x5b, 0x22, 1);
1655 else
1656 pci_write_config(dev, 0x5b,
1657 (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
1658 }
1659 ctlr->allocate = ata_highpoint_allocate;
1660 ctlr->setmode = ata_highpoint_setmode;
1661 return 0;
1662 }
1663
1664 static int
1665 ata_highpoint_allocate(device_t dev)
1666 {
1667 struct ata_channel *ch = device_get_softc(dev);
1668
1669 /* setup the usual register normal pci style */
1670 if (ata_pci_allocate(dev))
1671 return ENXIO;
1672
1673 ch->flags |= ATA_ALWAYS_DMASTAT;
1674 return 0;
1675 }
1676
1677 static void
1678 ata_highpoint_setmode(device_t dev, int mode)
1679 {
1680 device_t gparent = GRANDPARENT(dev);
1681 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1682 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1683 struct ata_device *atadev = device_get_softc(dev);
1684 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1685 int error;
1686 u_int32_t timings33[][4] = {
1687 /* HPT366 HPT370 HPT372 HPT374 mode */
1688 { 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a }, /* PIO 0 */
1689 { 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 }, /* PIO 1 */
1690 { 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 }, /* PIO 2 */
1691 { 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 }, /* PIO 3 */
1692 { 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 }, /* PIO 4 */
1693 { 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea }, /* MWDMA 0 */
1694 { 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 }, /* MWDMA 1 */
1695 { 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 }, /* MWDMA 2 */
1696 { 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea }, /* UDMA 0 */
1697 { 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 }, /* UDMA 1 */
1698 { 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 }, /* UDMA 2 */
1699 { 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 }, /* UDMA 3 */
1700 { 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 }, /* UDMA 4 */
1701 { 0, 0x16454e31, 0x1c8a9c62, 0x12848242 }, /* UDMA 5 */
1702 { 0, 0, 0x1c869c62, 0x12808242 } /* UDMA 6 */
1703 };
1704
1705 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1706
1707 if (ctlr->chip->cfg1 == HPT366 && ata_atapi(dev))
1708 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1709
1710 mode = ata_highpoint_check_80pin(dev, mode);
1711
1712 /*
1713 * most if not all HPT chips cant really handle that the device is
1714 * running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to
1715 * a max of ATA_UDMA5/ATA100 to guard against suboptimal performance
1716 */
1717 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
1718 ata_limit_mode(dev, mode, ATA_UDMA5));
1719 if (bootverbose)
1720 device_printf(dev, "%ssetting %s on HighPoint chip\n",
1721 (error) ? "FAILURE " : "", ata_mode2str(mode));
1722 if (!error)
1723 pci_write_config(gparent, 0x40 + (devno << 2),
1724 timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
1725 atadev->mode = mode;
1726 }
1727
1728 static int
1729 ata_highpoint_check_80pin(device_t dev, int mode)
1730 {
1731 device_t gparent = GRANDPARENT(dev);
1732 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1733 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1734 u_int8_t reg, val, res;
1735
1736 if (ctlr->chip->cfg1 == HPT374 && pci_get_function(gparent) == 1) {
1737 reg = ch->unit ? 0x57 : 0x53;
1738 val = pci_read_config(gparent, reg, 1);
1739 pci_write_config(gparent, reg, val | 0x80, 1);
1740 }
1741 else {
1742 reg = 0x5b;
1743 val = pci_read_config(gparent, reg, 1);
1744 pci_write_config(gparent, reg, val & 0xfe, 1);
1745 }
1746 res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
1747 pci_write_config(gparent, reg, val, 1);
1748
1749 if (mode > ATA_UDMA2 && res) {
1750 ata_print_cable(dev, "controller");
1751 mode = ATA_UDMA2;
1752 }
1753 return mode;
1754 }
1755
1756
1757 /*
1758 * Intel chipset support functions
1759 */
1760 int
1761 ata_intel_ident(device_t dev)
1762 {
1763 struct ata_pci_controller *ctlr = device_get_softc(dev);
1764 static struct ata_chip_id ids[] =
1765 {{ ATA_I82371FB, 0, 0, 0x00, ATA_WDMA2, "PIIX" },
1766 { ATA_I82371SB, 0, 0, 0x00, ATA_WDMA2, "PIIX3" },
1767 { ATA_I82371AB, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1768 { ATA_I82443MX, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1769 { ATA_I82451NX, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1770 { ATA_I82801AB, 0, 0, 0x00, ATA_UDMA2, "ICH0" },
1771 { ATA_I82801AA, 0, 0, 0x00, ATA_UDMA4, "ICH" },
1772 { ATA_I82372FB, 0, 0, 0x00, ATA_UDMA4, "ICH" },
1773 { ATA_I82801BA, 0, 0, 0x00, ATA_UDMA5, "ICH2" },
1774 { ATA_I82801BA_1, 0, 0, 0x00, ATA_UDMA5, "ICH2" },
1775 { ATA_I82801CA, 0, 0, 0x00, ATA_UDMA5, "ICH3" },
1776 { ATA_I82801CA_1, 0, 0, 0x00, ATA_UDMA5, "ICH3" },
1777 { ATA_I82801DB, 0, 0, 0x00, ATA_UDMA5, "ICH4" },
1778 { ATA_I82801DB_1, 0, 0, 0x00, ATA_UDMA5, "ICH4" },
1779 { ATA_I82801EB, 0, 0, 0x00, ATA_UDMA5, "ICH5" },
1780 { ATA_I82801EB_S1, 0, 0, 0x00, ATA_SA150, "ICH5" },
1781 { ATA_I82801EB_R1, 0, 0, 0x00, ATA_SA150, "ICH5" },
1782 { ATA_I6300ESB, 0, 0, 0x00, ATA_UDMA5, "6300ESB" },
1783 { ATA_I6300ESB_S1, 0, 0, 0x00, ATA_SA150, "6300ESB" },
1784 { ATA_I6300ESB_R1, 0, 0, 0x00, ATA_SA150, "6300ESB" },
1785 { ATA_I82801FB, 0, 0, 0x00, ATA_UDMA5, "ICH6" },
1786 { ATA_I82801FB_S1, 0, AHCI, 0x00, ATA_SA150, "ICH6" },
1787 { ATA_I82801FB_R1, 0, AHCI, 0x00, ATA_SA150, "ICH6" },
1788 { ATA_I82801FBM, 0, AHCI, 0x00, ATA_SA150, "ICH6M" },
1789 { ATA_I82801GB, 0, 0, 0x00, ATA_UDMA5, "ICH7" },
1790 { ATA_I82801GB_S1, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1791 { ATA_I82801GB_R1, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1792 { ATA_I82801GB_AH, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1793 { ATA_I82801GBM_S1, 0, AHCI, 0x00, ATA_SA300, "ICH7M" },
1794 { ATA_I82801GBM_R1, 0, AHCI, 0x00, ATA_SA300, "ICH7M" },
1795 { ATA_I82801GBM_AH, 0, AHCI, 0x00, ATA_SA300, "ICH7M" },
1796 { ATA_I63XXESB2, 0, 0, 0x00, ATA_UDMA5, "63XXESB2" },
1797 { ATA_I63XXESB2_S1, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1798 { ATA_I63XXESB2_S2, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1799 { ATA_I63XXESB2_R1, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1800 { ATA_I63XXESB2_R2, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1801 { ATA_I82801HB_S1, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1802 { ATA_I82801HB_S2, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1803 { ATA_I82801HB_R1, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1804 { ATA_I82801HB_AH4, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1805 { ATA_I82801HB_AH6, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1806 { ATA_I82801HBM, 0, 0, 0x00, ATA_UDMA5, "ICH8M" },
1807 { ATA_I82801HBM_S1, 0, 0, 0x00, ATA_SA150, "ICH8M" },
1808 { ATA_I82801HBM_S2, 0, AHCI, 0x00, ATA_SA300, "ICH8M" },
1809 { ATA_I82801HBM_S3, 0, AHCI, 0x00, ATA_SA300, "ICH8M" },
1810 { ATA_I82801IB_S1, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1811 { ATA_I82801IB_S2, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1812 { ATA_I82801IB_AH2, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1813 { ATA_I82801IB_AH4, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1814 { ATA_I82801IB_AH6, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1815 { ATA_I82801IB_R1, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1816 { ATA_I31244, 0, 0, 0x00, ATA_SA150, "31244" },
1817 { 0, 0, 0, 0, 0, 0}};
1818
1819 if (!(ctlr->chip = ata_match_chip(dev, ids)))
1820 return ENXIO;
1821
1822 ata_set_desc(dev);
1823 ctlr->chipinit = ata_intel_chipinit;
1824 return 0;
1825 }
1826
1827 static int
1828 ata_intel_chipinit(device_t dev)
1829 {
1830 struct ata_pci_controller *ctlr = device_get_softc(dev);
1831
1832 if (ata_setup_interrupt(dev))
1833 return ENXIO;
1834
1835 /* good old PIIX needs special treatment (not implemented) */
1836 if (ctlr->chip->chipid == ATA_I82371FB) {
1837 ctlr->setmode = ata_intel_old_setmode;
1838 }
1839
1840 /* the intel 31244 needs special care if in DPA mode */
1841 else if (ctlr->chip->chipid == ATA_I31244) {
1842 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE) {
1843 ctlr->r_type2 = SYS_RES_MEMORY;
1844 ctlr->r_rid2 = PCIR_BAR(0);
1845 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1846 &ctlr->r_rid2,
1847 RF_ACTIVE)))
1848 return ENXIO;
1849 ctlr->channels = 4;
1850 ctlr->allocate = ata_intel_31244_allocate;
1851 ctlr->reset = ata_intel_31244_reset;
1852 }
1853 ctlr->setmode = ata_sata_setmode;
1854 }
1855
1856 /* non SATA intel chips goes here */
1857 else if (ctlr->chip->max_dma < ATA_SA150) {
1858 ctlr->allocate = ata_intel_allocate;
1859 ctlr->setmode = ata_intel_new_setmode;
1860 }
1861
1862 /* SATA parts can be either compat or AHCI */
1863 else {
1864 /* force all ports active "the legacy way" */
1865 pci_write_config(dev, 0x92, pci_read_config(dev, 0x92, 2) | 0x0f, 2);
1866
1867 ctlr->allocate = ata_intel_allocate;
1868 ctlr->reset = ata_intel_reset;
1869
1870 /*
1871 * if we have AHCI capability and AHCI or RAID mode enabled
1872 * in BIOS we try for AHCI mode
1873 */
1874 if ((ctlr->chip->cfg1 == AHCI) &&
1875 (pci_read_config(dev, 0x90, 1) & 0xc0) &&
1876 (ata_ahci_chipinit(dev) != ENXIO))
1877 return 0;
1878
1879 /* if BAR(5) is IO it should point to SATA interface registers */
1880 ctlr->r_type2 = SYS_RES_IOPORT;
1881 ctlr->r_rid2 = PCIR_BAR(5);
1882 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1883 &ctlr->r_rid2, RF_ACTIVE)))
1884 ctlr->setmode = ata_intel_sata_setmode;
1885 else
1886 ctlr->setmode = ata_sata_setmode;
1887
1888 /* enable PCI interrupt */
1889 pci_write_config(dev, PCIR_COMMAND,
1890 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1891 }
1892 return 0;
1893 }
1894
1895 static int
1896 ata_intel_allocate(device_t dev)
1897 {
1898 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1899 struct ata_channel *ch = device_get_softc(dev);
1900
1901 /* setup the usual register normal pci style */
1902 if (ata_pci_allocate(dev))
1903 return ENXIO;
1904
1905 /* if r_res2 is valid it points to SATA interface registers */
1906 if (ctlr->r_res2) {
1907 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
1908 ch->r_io[ATA_IDX_ADDR].offset = 0x00;
1909 ch->r_io[ATA_IDX_DATA].res = ctlr->r_res2;
1910 ch->r_io[ATA_IDX_DATA].offset = 0x04;
1911 }
1912
1913 ch->flags |= ATA_ALWAYS_DMASTAT;
1914 return 0;
1915 }
1916
1917 static void
1918 ata_intel_reset(device_t dev)
1919 {
1920 device_t parent = device_get_parent(dev);
1921 struct ata_pci_controller *ctlr = device_get_softc(parent);
1922 struct ata_channel *ch = device_get_softc(dev);
1923 int mask, timeout;
1924
1925 /* ICH6 & ICH7 in compat mode has 4 SATA ports as master/slave on 2 ch's */
1926 if (ctlr->chip->cfg1) {
1927 mask = (0x0005 << ch->unit);
1928 }
1929 else {
1930 /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1931 if (pci_read_config(parent, 0x90, 1) & 0x04)
1932 mask = 0x0003;
1933 else {
1934 mask = (0x0001 << ch->unit);
1935 /* XXX SOS should be in intel_allocate if we grow it */
1936 ch->flags |= ATA_NO_SLAVE;
1937 }
1938 }
1939 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1940 DELAY(10);
1941 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
1942
1943 /* wait up to 1 sec for "connect well" */
1944 for (timeout = 0; timeout < 100 ; timeout++) {
1945 if (((pci_read_config(parent, 0x92, 2) & (mask << 4)) == (mask << 4)) &&
1946 (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
1947 break;
1948 ata_udelay(10000);
1949 }
1950 ata_generic_reset(dev);
1951 }
1952
1953 static void
1954 ata_intel_old_setmode(device_t dev, int mode)
1955 {
1956 /* NOT YET */
1957 }
1958
1959 static void
1960 ata_intel_new_setmode(device_t dev, int mode)
1961 {
1962 device_t gparent = GRANDPARENT(dev);
1963 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1964 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1965 struct ata_device *atadev = device_get_softc(dev);
1966 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1967 u_int32_t reg40 = pci_read_config(gparent, 0x40, 4);
1968 u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
1969 u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
1970 u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
1971 u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
1972 u_int32_t mask40 = 0, new40 = 0;
1973 u_int8_t mask44 = 0, new44 = 0;
1974 int error;
1975 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1976 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1977
1978 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1979
1980 if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
1981 ata_print_cable(dev, "controller");
1982 mode = ATA_UDMA2;
1983 }
1984
1985 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1986
1987 if (bootverbose)
1988 device_printf(dev, "%ssetting %s on %s chip\n",
1989 (error) ? "FAILURE " : "",
1990 ata_mode2str(mode), ctlr->chip->text);
1991 if (error)
1992 return;
1993
1994 if (mode >= ATA_UDMA0) {
1995 pci_write_config(gparent, 0x48, reg48 | (0x0001 << devno), 2);
1996 pci_write_config(gparent, 0x4a,
1997 (reg4a & ~(0x3 << (devno << 2))) |
1998 ((0x01 + !(mode & 0x01)) << (devno << 2)), 2);
1999 }
2000 else {
2001 pci_write_config(gparent, 0x48, reg48 & ~(0x0001 << devno), 2);
2002 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno << 2))), 2);
2003 }
2004 reg54 |= 0x0400;
2005 if (mode >= ATA_UDMA2)
2006 pci_write_config(gparent, 0x54, reg54 | (0x1 << devno), 2);
2007 else
2008 pci_write_config(gparent, 0x54, reg54 & ~(0x1 << devno), 2);
2009
2010 if (mode >= ATA_UDMA5)
2011 pci_write_config(gparent, 0x54, reg54 | (0x1000 << devno), 2);
2012 else
2013 pci_write_config(gparent, 0x54, reg54 & ~(0x1000 << devno), 2);
2014
2015 reg40 &= ~0x00ff00ff;
2016 reg40 |= 0x40774077;
2017
2018 if (atadev->unit == ATA_MASTER) {
2019 mask40 = 0x3300;
2020 new40 = timings[ata_mode2idx(mode)] << 8;
2021 }
2022 else {
2023 mask44 = 0x0f;
2024 new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
2025 (timings[ata_mode2idx(mode)] & 0x03);
2026 }
2027 if (ch->unit) {
2028 mask40 <<= 16;
2029 new40 <<= 16;
2030 mask44 <<= 4;
2031 new44 <<= 4;
2032 }
2033 pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
2034 pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
2035
2036 atadev->mode = mode;
2037 }
2038
2039 static void
2040 ata_intel_sata_setmode(device_t dev, int mode)
2041 {
2042 struct ata_device *atadev = device_get_softc(dev);
2043
2044 if (atadev->param.satacapabilities != 0x0000 &&
2045 atadev->param.satacapabilities != 0xffff) {
2046
2047 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2048 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2049
2050 /* on some drives we need to set the transfer mode */
2051 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
2052 ata_limit_mode(dev, mode, ATA_UDMA6));
2053
2054 /* set ATA_SSTATUS register offset */
2055 ATA_IDX_OUTL(ch, ATA_IDX_ADDR, devno * 0x100);
2056
2057 /* query SATA STATUS for the speed */
2058 if ((ATA_IDX_INL(ch, ATA_IDX_DATA) & ATA_SS_CONWELL_MASK) ==
2059 ATA_SS_CONWELL_GEN2)
2060 atadev->mode = ATA_SA300;
2061 else
2062 atadev->mode = ATA_SA150;
2063 }
2064 else {
2065 mode = ata_limit_mode(dev, mode, ATA_UDMA5);
2066 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
2067 atadev->mode = mode;
2068 }
2069 }
2070
2071 static int
2072 ata_intel_31244_allocate(device_t dev)
2073 {
2074 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2075 struct ata_channel *ch = device_get_softc(dev);
2076 int i;
2077 int ch_offset;
2078
2079 ch_offset = 0x200 + ch->unit * 0x200;
2080
2081 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
2082 ch->r_io[i].res = ctlr->r_res2;
2083
2084 /* setup ATA registers */
2085 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
2086 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x06;
2087 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
2088 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
2089 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
2090 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
2091 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
2092 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1d;
2093 ch->r_io[ATA_ERROR].offset = ch_offset + 0x04;
2094 ch->r_io[ATA_STATUS].offset = ch_offset + 0x1c;
2095 ch->r_io[ATA_ALTSTAT].offset = ch_offset + 0x28;
2096 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x29;
2097
2098 /* setup DMA registers */
2099 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x100;
2100 ch->r_io[ATA_SERROR].offset = ch_offset + 0x104;
2101 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x108;
2102
2103 /* setup SATA registers */
2104 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x70;
2105 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x72;
2106 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x74;
2107
2108 ch->flags |= ATA_NO_SLAVE;
2109 ata_pci_hw(dev);
2110 ch->hw.status = ata_intel_31244_status;
2111 ch->hw.tf_write = ata_intel_31244_tf_write;
2112
2113 /* enable PHY state change interrupt */
2114 ATA_OUTL(ctlr->r_res2, 0x4,
2115 ATA_INL(ctlr->r_res2, 0x04) | (0x01 << (ch->unit << 3)));
2116 return 0;
2117 }
2118
2119 static int
2120 ata_intel_31244_status(device_t dev)
2121 {
2122 /* do we have any PHY events ? */
2123 ata_sata_phy_check_events(dev);
2124
2125 /* any drive action to take care of ? */
2126 return ata_pci_status(dev);
2127 }
2128
2129 static void
2130 ata_intel_31244_tf_write(struct ata_request *request)
2131 {
2132 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2133 struct ata_device *atadev = device_get_softc(request->dev);
2134
2135 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2136 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
2137 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
2138 ATA_IDX_OUTW(ch, ATA_SECTOR, ((request->u.ata.lba >> 16) & 0xff00) |
2139 (request->u.ata.lba & 0x00ff));
2140 ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((request->u.ata.lba >> 24) & 0xff00) |
2141 ((request->u.ata.lba >> 8) & 0x00ff));
2142 ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((request->u.ata.lba >> 32) & 0xff00) |
2143 ((request->u.ata.lba >> 16) & 0x00ff));
2144 ATA_IDX_OUTW(ch, ATA_DRIVE, ATA_D_LBA | atadev->unit);
2145 }
2146 else {
2147 ATA_IDX_OUTB(ch, ATA_FEATURE, request->u.ata.feature);
2148 ATA_IDX_OUTB(ch, ATA_COUNT, request->u.ata.count);
2149 if (atadev->flags & ATA_D_USE_CHS) {
2150 int heads, sectors;
2151
2152 if (atadev->param.atavalid & ATA_FLAG_54_58) {
2153 heads = atadev->param.current_heads;
2154 sectors = atadev->param.current_sectors;
2155 }
2156 else {
2157 heads = atadev->param.heads;
2158 sectors = atadev->param.sectors;
2159 }
2160 ATA_IDX_OUTB(ch, ATA_SECTOR, (request->u.ata.lba % sectors)+1);
2161 ATA_IDX_OUTB(ch, ATA_CYL_LSB,
2162 (request->u.ata.lba / (sectors * heads)));
2163 ATA_IDX_OUTB(ch, ATA_CYL_MSB,
2164 (request->u.ata.lba / (sectors * heads)) >> 8);
2165 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | atadev->unit |
2166 (((request->u.ata.lba% (sectors * heads)) /
2167 sectors) & 0xf));
2168 }
2169 else {
2170 ATA_IDX_OUTB(ch, ATA_SECTOR, request->u.ata.lba);
2171 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->u.ata.lba >> 8);
2172 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->u.ata.lba >> 16);
2173 ATA_IDX_OUTB(ch, ATA_DRIVE,
2174 ATA_D_IBM | ATA_D_LBA | atadev->unit |
2175 ((request->u.ata.lba >> 24) & 0x0f));
2176 }
2177 }
2178 }
2179
2180 static void
2181 ata_intel_31244_reset(device_t dev)
2182 {
2183 if (ata_sata_phy_reset(dev))
2184 ata_generic_reset(dev);
2185 }
2186
2187
2188 /*
2189 * Integrated Technology Express Inc. (ITE) chipset support functions
2190 */
2191 int
2192 ata_ite_ident(device_t dev)
2193 {
2194 struct ata_pci_controller *ctlr = device_get_softc(dev);
2195 static struct ata_chip_id ids[] =
2196 {{ ATA_IT8212F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8212F" },
2197 { ATA_IT8211F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8211F" },
2198 { 0, 0, 0, 0, 0, 0}};
2199
2200 if (!(ctlr->chip = ata_match_chip(dev, ids)))
2201 return ENXIO;
2202
2203 ata_set_desc(dev);
2204 ctlr->chipinit = ata_ite_chipinit;
2205 return 0;
2206 }
2207
2208 static int
2209 ata_ite_chipinit(device_t dev)
2210 {
2211 struct ata_pci_controller *ctlr = device_get_softc(dev);
2212
2213 if (ata_setup_interrupt(dev))
2214 return ENXIO;
2215
2216 ctlr->setmode = ata_ite_setmode;
2217
2218 /* set PCI mode and 66Mhz reference clock */
2219 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
2220
2221 /* set default active & recover timings */
2222 pci_write_config(dev, 0x54, 0x31, 1);
2223 pci_write_config(dev, 0x56, 0x31, 1);
2224 return 0;
2225 }
2226
2227 static void
2228 ata_ite_setmode(device_t dev, int mode)
2229 {
2230 device_t gparent = GRANDPARENT(dev);
2231 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2232 struct ata_device *atadev = device_get_softc(dev);
2233 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2234 int error;
2235
2236 /* correct the mode for what the HW supports */
2237 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
2238
2239 /* check the CBLID bits for 80 conductor cable detection */
2240 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
2241 (ch->unit ? (1<<3) : (1<<2)))) {
2242 ata_print_cable(dev, "controller");
2243 mode = ATA_UDMA2;
2244 }
2245
2246 /* set the wanted mode on the device */
2247 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2248
2249 if (bootverbose)
2250 device_printf(dev, "%s setting %s on ITE8212F chip\n",
2251 (error) ? "failed" : "success", ata_mode2str(mode));
2252
2253 /* if the device accepted the mode change, setup the HW accordingly */
2254 if (!error) {
2255 if (mode >= ATA_UDMA0) {
2256 u_int8_t udmatiming[] =
2257 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
2258
2259 /* enable UDMA mode */
2260 pci_write_config(gparent, 0x50,
2261 pci_read_config(gparent, 0x50, 1) &
2262 ~(1 << (devno + 3)), 1);
2263
2264 /* set UDMA timing */
2265 pci_write_config(gparent,
2266 0x56 + (ch->unit << 2) + ATA_DEV(atadev->unit),
2267 udmatiming[mode & ATA_MODE_MASK], 1);
2268 }
2269 else {
2270 u_int8_t chtiming[] =
2271 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
2272
2273 /* disable UDMA mode */
2274 pci_write_config(gparent, 0x50,
2275 pci_read_config(gparent, 0x50, 1) |
2276 (1 << (devno + 3)), 1);
2277
2278 /* set active and recover timing (shared between master & slave) */
2279 if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
2280 chtiming[ata_mode2idx(mode)])
2281 pci_write_config(gparent, 0x54 + (ch->unit << 2),
2282 chtiming[ata_mode2idx(mode)], 1);
2283 }
2284 atadev->mode = mode;
2285 }
2286 }
2287
2288
2289 /*
2290 * JMicron chipset support functions
2291 */
2292 int
2293 ata_jmicron_ident(device_t dev)
2294 {
2295 struct ata_pci_controller *ctlr = device_get_softc(dev);
2296 struct ata_chip_id *idx;
2297 static struct ata_chip_id ids[] =
2298 {{ ATA_JMB360, 0, 1, 0, ATA_SA300, "JMB360" },
2299 { ATA_JMB361, 0, 1, 1, ATA_SA300, "JMB361" },
2300 { ATA_JMB363, 0, 2, 1, ATA_SA300, "JMB363" },
2301 { ATA_JMB365, 0, 1, 2, ATA_SA300, "JMB365" },
2302 { ATA_JMB366, 0, 2, 2, ATA_SA300, "JMB366" },
2303 { ATA_JMB368, 0, 0, 1, ATA_UDMA6, "JMB368" },
2304 { 0, 0, 0, 0, 0, 0}};
2305 char buffer[64];
2306
2307 if (!(idx = ata_match_chip(dev, ids)))
2308 return ENXIO;
2309
2310 if ((pci_read_config(dev, 0xdf, 1) & 0x40) &&
2311 (pci_get_function(dev) == (pci_read_config(dev, 0x40, 1) & 0x02 >> 1)))
2312 sprintf(buffer, "JMicron %s %s controller",
2313 idx->text, ata_mode2str(ATA_UDMA6));
2314 else
2315 sprintf(buffer, "JMicron %s %s controller",
2316 idx->text, ata_mode2str(idx->max_dma));
2317 device_set_desc_copy(dev, buffer);
2318 ctlr->chip = idx;
2319 ctlr->chipinit = ata_jmicron_chipinit;
2320 return 0;
2321 }
2322
2323 static int
2324 ata_jmicron_chipinit(device_t dev)
2325 {
2326 struct ata_pci_controller *ctlr = device_get_softc(dev);
2327 int error;
2328
2329 if (ata_setup_interrupt(dev))
2330 return ENXIO;
2331
2332 /* do we have multiple PCI functions ? */
2333 if (pci_read_config(dev, 0xdf, 1) & 0x40) {
2334 /* are we on the AHCI part ? */
2335 if (ata_ahci_chipinit(dev) != ENXIO)
2336 return 0;
2337
2338 /* otherwise we are on the PATA part */
2339 ctlr->allocate = ata_pci_allocate;
2340 ctlr->reset = ata_generic_reset;
2341 ctlr->dmainit = ata_pci_dmainit;
2342 ctlr->setmode = ata_jmicron_setmode;
2343 ctlr->channels = ctlr->chip->cfg2;
2344 }
2345 else {
2346 /* set controller configuration to a combined setup we support */
2347 pci_write_config(dev, 0x40, 0x80c0a131, 4);
2348 pci_write_config(dev, 0x80, 0x01200000, 4);
2349
2350 if (ctlr->chip->cfg1 && (error = ata_ahci_chipinit(dev)))
2351 return error;
2352
2353 ctlr->allocate = ata_jmicron_allocate;
2354 ctlr->reset = ata_jmicron_reset;
2355 ctlr->dmainit = ata_jmicron_dmainit;
2356 ctlr->setmode = ata_jmicron_setmode;
2357
2358 /* set the number of HW channels */
2359 ctlr->channels = ctlr->chip->cfg1 + ctlr->chip->cfg2;
2360 }
2361 return 0;
2362 }
2363
2364 static int
2365 ata_jmicron_allocate(device_t dev)
2366 {
2367 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2368 struct ata_channel *ch = device_get_softc(dev);
2369 int error;
2370
2371 if (ch->unit >= ctlr->chip->cfg1) {
2372 ch->unit -= ctlr->chip->cfg1;
2373 error = ata_pci_allocate(dev);
2374 ch->unit += ctlr->chip->cfg1;
2375 }
2376 else
2377 error = ata_ahci_allocate(dev);
2378 return error;
2379 }
2380
2381 static void
2382 ata_jmicron_reset(device_t dev)
2383 {
2384 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2385 struct ata_channel *ch = device_get_softc(dev);
2386
2387 if (ch->unit >= ctlr->chip->cfg1)
2388 ata_generic_reset(dev);
2389 else
2390 ata_ahci_reset(dev);
2391 }
2392
2393 static void
2394 ata_jmicron_dmainit(device_t dev)
2395 {
2396 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2397 struct ata_channel *ch = device_get_softc(dev);
2398
2399 if (ch->unit >= ctlr->chip->cfg1)
2400 ata_pci_dmainit(dev);
2401 else
2402 ata_ahci_dmainit(dev);
2403 }
2404
2405 static void
2406 ata_jmicron_setmode(device_t dev, int mode)
2407 {
2408 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2409 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2410
2411 if (pci_read_config(dev, 0xdf, 1) & 0x40 || ch->unit >= ctlr->chip->cfg1) {
2412 struct ata_device *atadev = device_get_softc(dev);
2413
2414 /* check for 80pin cable present */
2415 if (pci_read_config(dev, 0x40, 1) & 0x08)
2416 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2417 else
2418 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
2419
2420 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
2421 atadev->mode = mode;
2422 }
2423 else
2424 ata_sata_setmode(dev, mode);
2425 }
2426
2427
2428 /*
2429 * Marvell chipset support functions
2430 */
2431 #define ATA_MV_HOST_BASE(ch) \
2432 ((ch->unit & 3) * 0x0100) + (ch->unit > 3 ? 0x30000 : 0x20000)
2433 #define ATA_MV_EDMA_BASE(ch) \
2434 ((ch->unit & 3) * 0x2000) + (ch->unit > 3 ? 0x30000 : 0x20000)
2435
2436 struct ata_marvell_response {
2437 u_int16_t tag;
2438 u_int8_t edma_status;
2439 u_int8_t dev_status;
2440 u_int32_t timestamp;
2441 };
2442
2443 struct ata_marvell_dma_prdentry {
2444 u_int32_t addrlo;
2445 u_int32_t count;
2446 u_int32_t addrhi;
2447 u_int32_t reserved;
2448 };
2449
2450 int
2451 ata_marvell_ident(device_t dev)
2452 {
2453 struct ata_pci_controller *ctlr = device_get_softc(dev);
2454 static struct ata_chip_id ids[] =
2455 {{ ATA_M88SX5040, 0, 4, MV50XX, ATA_SA150, "88SX5040" },
2456 { ATA_M88SX5041, 0, 4, MV50XX, ATA_SA150, "88SX5041" },
2457 { ATA_M88SX5080, 0, 8, MV50XX, ATA_SA150, "88SX5080" },
2458 { ATA_M88SX5081, 0, 8, MV50XX, ATA_SA150, "88SX5081" },
2459 { ATA_M88SX6041, 0, 4, MV60XX, ATA_SA300, "88SX6041" },
2460 { ATA_M88SX6081, 0, 8, MV60XX, ATA_SA300, "88SX6081" },
2461 { ATA_M88SX6101, 0, 1, MV61XX, ATA_UDMA6, "88SX6101" },
2462 { ATA_M88SX6145, 0, 2, MV61XX, ATA_UDMA6, "88SX6145" },
2463 { 0, 0, 0, 0, 0, 0}};
2464
2465 if (!(ctlr->chip = ata_match_chip(dev, ids)))
2466 return ENXIO;
2467
2468 ata_set_desc(dev);
2469
2470 switch (ctlr->chip->cfg2) {
2471 case MV50XX:
2472 case MV60XX:
2473 ctlr->chipinit = ata_marvell_edma_chipinit;
2474 break;
2475 case MV61XX:
2476 ctlr->chipinit = ata_marvell_pata_chipinit;
2477 break;
2478 }
2479 return 0;
2480 }
2481
2482 static int
2483 ata_marvell_pata_chipinit(device_t dev)
2484 {
2485 struct ata_pci_controller *ctlr = device_get_softc(dev);
2486
2487 if (ata_setup_interrupt(dev))
2488 return ENXIO;
2489
2490 ctlr->allocate = ata_marvell_pata_allocate;
2491 ctlr->setmode = ata_marvell_pata_setmode;
2492 ctlr->channels = ctlr->chip->cfg1;
2493 return 0;
2494 }
2495
2496 static int
2497 ata_marvell_pata_allocate(device_t dev)
2498 {
2499 struct ata_channel *ch = device_get_softc(dev);
2500
2501 /* setup the usual register normal pci style */
2502 if (ata_pci_allocate(dev))
2503 return ENXIO;
2504
2505 /* dont use 32 bit PIO transfers */
2506 ch->flags |= ATA_USE_16BIT;
2507
2508 return 0;
2509 }
2510
2511 static void
2512 ata_marvell_pata_setmode(device_t dev, int mode)
2513 {
2514 device_t gparent = GRANDPARENT(dev);
2515 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2516 struct ata_device *atadev = device_get_softc(dev);
2517
2518 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2519 mode = ata_check_80pin(dev, mode);
2520 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
2521 atadev->mode = mode;
2522 }
2523
2524 static int
2525 ata_marvell_edma_chipinit(device_t dev)
2526 {
2527 struct ata_pci_controller *ctlr = device_get_softc(dev);
2528
2529 if (ata_setup_interrupt(dev))
2530 return ENXIO;
2531
2532 ctlr->r_type1 = SYS_RES_MEMORY;
2533 ctlr->r_rid1 = PCIR_BAR(0);
2534 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
2535 &ctlr->r_rid1, RF_ACTIVE)))
2536 return ENXIO;
2537
2538 /* mask all host controller interrupts */
2539 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x00000000);
2540
2541 /* mask all PCI interrupts */
2542 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x00000000);
2543
2544 ctlr->allocate = ata_marvell_edma_allocate;
2545 ctlr->reset = ata_marvell_edma_reset;
2546 ctlr->dmainit = ata_marvell_edma_dmainit;
2547 ctlr->setmode = ata_sata_setmode;
2548 ctlr->channels = ctlr->chip->cfg1;
2549
2550 /* clear host controller interrupts */
2551 ATA_OUTL(ctlr->r_res1, 0x20014, 0x00000000);
2552 if (ctlr->chip->cfg1 > 4)
2553 ATA_OUTL(ctlr->r_res1, 0x30014, 0x00000000);
2554
2555 /* clear PCI interrupts */
2556 ATA_OUTL(ctlr->r_res1, 0x01d58, 0x00000000);
2557
2558 /* unmask PCI interrupts we want */
2559 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x007fffff);
2560
2561 /* unmask host controller interrupts we want */
2562 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x000000ff/*HC0*/ | 0x0001fe00/*HC1*/ |
2563 /*(1<<19) | (1<<20) | (1<<21) |*/(1<<22) | (1<<24) | (0x7f << 25));
2564
2565 /* enable PCI interrupt */
2566 pci_write_config(dev, PCIR_COMMAND,
2567 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
2568 return 0;
2569 }
2570
2571 static int
2572 ata_marvell_edma_allocate(device_t dev)
2573 {
2574 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2575 struct ata_channel *ch = device_get_softc(dev);
2576 u_int64_t work = ch->dma->work_bus;
2577 int i;
2578
2579 /* clear work area */
2580 bzero(ch->dma->work, 1024+256);
2581 bus_dmamap_sync(ch->dma->work_tag, ch->dma->work_map,
2582 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2583
2584 /* set legacy ATA resources */
2585 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2586 ch->r_io[i].res = ctlr->r_res1;
2587 ch->r_io[i].offset = 0x02100 + (i << 2) + ATA_MV_EDMA_BASE(ch);
2588 }
2589 ch->r_io[ATA_CONTROL].res = ctlr->r_res1;
2590 ch->r_io[ATA_CONTROL].offset = 0x02120 + ATA_MV_EDMA_BASE(ch);
2591 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res1;
2592 ata_default_registers(dev);
2593
2594 /* set SATA resources */
2595 switch (ctlr->chip->cfg2) {
2596 case MV50XX:
2597 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
2598 ch->r_io[ATA_SSTATUS].offset = 0x00100 + ATA_MV_HOST_BASE(ch);
2599 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
2600 ch->r_io[ATA_SERROR].offset = 0x00104 + ATA_MV_HOST_BASE(ch);
2601 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
2602 ch->r_io[ATA_SCONTROL].offset = 0x00108 + ATA_MV_HOST_BASE(ch);
2603 break;
2604 case MV60XX:
2605 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
2606 ch->r_io[ATA_SSTATUS].offset = 0x02300 + ATA_MV_EDMA_BASE(ch);
2607 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
2608 ch->r_io[ATA_SERROR].offset = 0x02304 + ATA_MV_EDMA_BASE(ch);
2609 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
2610 ch->r_io[ATA_SCONTROL].offset = 0x02308 + ATA_MV_EDMA_BASE(ch);
2611 ch->r_io[ATA_SACTIVE].res = ctlr->r_res1;
2612 ch->r_io[ATA_SACTIVE].offset = 0x02350 + ATA_MV_EDMA_BASE(ch);
2613 break;
2614 }
2615
2616 ch->flags |= ATA_NO_SLAVE;
2617 ch->flags |= ATA_USE_16BIT; /* XXX SOS needed ? */
2618 ata_generic_hw(dev);
2619 ch->hw.begin_transaction = ata_marvell_edma_begin_transaction;
2620 ch->hw.end_transaction = ata_marvell_edma_end_transaction;
2621 ch->hw.status = ata_marvell_edma_status;
2622
2623 /* disable the EDMA machinery */
2624 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2625 DELAY(100000); /* SOS should poll for disabled */
2626
2627 /* set configuration to non-queued 128b read transfers stop on error */
2628 ATA_OUTL(ctlr->r_res1, 0x02000 + ATA_MV_EDMA_BASE(ch), (1<<11) | (1<<13));
2629
2630 /* request queue base high */
2631 ATA_OUTL(ctlr->r_res1, 0x02010 + ATA_MV_EDMA_BASE(ch), work >> 32);
2632
2633 /* request queue in ptr */
2634 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
2635
2636 /* request queue out ptr */
2637 ATA_OUTL(ctlr->r_res1, 0x02018 + ATA_MV_EDMA_BASE(ch), 0x0);
2638
2639 /* response queue base high */
2640 work += 1024;
2641 ATA_OUTL(ctlr->r_res1, 0x0201c + ATA_MV_EDMA_BASE(ch), work >> 32);
2642
2643 /* response queue in ptr */
2644 ATA_OUTL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch), 0x0);
2645
2646 /* response queue out ptr */
2647 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
2648
2649 /* clear SATA error register */
2650 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2651
2652 /* clear any outstanding error interrupts */
2653 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2654
2655 /* unmask all error interrupts */
2656 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2657
2658 /* enable EDMA machinery */
2659 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2660 return 0;
2661 }
2662
2663 static int
2664 ata_marvell_edma_status(device_t dev)
2665 {
2666 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2667 struct ata_channel *ch = device_get_softc(dev);
2668 u_int32_t cause = ATA_INL(ctlr->r_res1, 0x01d60);
2669 int shift = (ch->unit << 1) + (ch->unit > 3);
2670
2671 if (cause & (1 << shift)) {
2672
2673 /* clear interrupt(s) */
2674 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2675
2676 /* do we have any PHY events ? */
2677 ata_sata_phy_check_events(dev);
2678 }
2679
2680 /* do we have any device action ? */
2681 return (cause & (2 << shift));
2682 }
2683
2684 /* must be called with ATA channel locked and state_mtx held */
2685 static int
2686 ata_marvell_edma_begin_transaction(struct ata_request *request)
2687 {
2688 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2689 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2690 u_int32_t req_in;
2691 u_int8_t *bytep;
2692 int i, tag = 0x07;
2693 int dummy, error, slot;
2694
2695 /* only DMA R/W goes through the EMDA machine */
2696 if (request->u.ata.command != ATA_READ_DMA &&
2697 request->u.ata.command != ATA_WRITE_DMA) {
2698
2699 /* disable the EDMA machinery */
2700 if (ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)
2701 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2702 return ata_begin_transaction(request);
2703 }
2704
2705 /* check for 48 bit access and convert if needed */
2706 ata_modify_if_48bit(request);
2707
2708 /* check sanity, setup SG list and DMA engine */
2709 if ((error = ch->dma->load(ch->dev, request->data, request->bytecount,
2710 request->flags & ATA_R_READ, ch->dma->sg,
2711 &dummy))) {
2712 device_printf(request->dev, "setting up DMA failed\n");
2713 request->result = error;
2714 return ATA_OP_FINISHED;
2715 }
2716
2717 /* get next free request queue slot */
2718 req_in = ATA_INL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch));
2719 slot = (((req_in & ~0xfffffc00) >> 5) + 0) & 0x1f;
2720 bytep = (u_int8_t *)(ch->dma->work);
2721 bytep += (slot << 5);
2722
2723 /* fill in this request */
2724 le32enc(bytep + 0 * sizeof(u_int32_t),
2725 ch->dma->sg_bus & 0xffffffff);
2726 le32enc(bytep + 1 * sizeof(u_int32_t),
2727 (u_int64_t)ch->dma->sg_bus >> 32);
2728 le16enc(bytep + 4 * sizeof(u_int16_t),
2729 (request->flags & ATA_R_READ ? 0x01 : 0x00) | (tag << 1));
2730
2731 i = 10;
2732 bytep[i++] = (request->u.ata.count >> 8) & 0xff;
2733 bytep[i++] = 0x10 | ATA_COUNT;
2734 bytep[i++] = request->u.ata.count & 0xff;
2735 bytep[i++] = 0x10 | ATA_COUNT;
2736
2737 bytep[i++] = (request->u.ata.lba >> 24) & 0xff;
2738 bytep[i++] = 0x10 | ATA_SECTOR;
2739 bytep[i++] = request->u.ata.lba & 0xff;
2740 bytep[i++] = 0x10 | ATA_SECTOR;
2741
2742 bytep[i++] = (request->u.ata.lba >> 32) & 0xff;
2743 bytep[i++] = 0x10 | ATA_CYL_LSB;
2744 bytep[i++] = (request->u.ata.lba >> 8) & 0xff;
2745 bytep[i++] = 0x10 | ATA_CYL_LSB;
2746
2747 bytep[i++] = (request->u.ata.lba >> 40) & 0xff;
2748 bytep[i++] = 0x10 | ATA_CYL_MSB;
2749 bytep[i++] = (request->u.ata.lba >> 16) & 0xff;
2750 bytep[i++] = 0x10 | ATA_CYL_MSB;
2751
2752 bytep[i++] = ATA_D_LBA | ATA_D_IBM | ((request->u.ata.lba >> 24) & 0xf);
2753 bytep[i++] = 0x10 | ATA_DRIVE;
2754
2755 bytep[i++] = request->u.ata.command;
2756 bytep[i++] = 0x90 | ATA_COMMAND;
2757
2758 bus_dmamap_sync(ch->dma->work_tag, ch->dma->work_map,
2759 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2760
2761 /* enable EDMA machinery if needed */
2762 if (!(ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)) {
2763 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2764 while (!(ATA_INL(ctlr->r_res1,
2765 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
2766 DELAY(10);
2767 }
2768
2769 /* tell EDMA it has a new request */
2770 slot = (((req_in & ~0xfffffc00) >> 5) + 1) & 0x1f;
2771 req_in &= 0xfffffc00;
2772 req_in += (slot << 5);
2773 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), req_in);
2774
2775 return ATA_OP_CONTINUES;
2776 }
2777
2778 /* must be called with ATA channel locked and state_mtx held */
2779 static int
2780 ata_marvell_edma_end_transaction(struct ata_request *request)
2781 {
2782 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2783 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2784 int offset = (ch->unit > 3 ? 0x30014 : 0x20014);
2785 u_int32_t icr = ATA_INL(ctlr->r_res1, offset);
2786 int res;
2787
2788 /* EDMA interrupt */
2789 if ((icr & (0x0001 << (ch->unit & 3)))) {
2790 struct ata_marvell_response *response;
2791 u_int32_t rsp_in, rsp_out;
2792 int slot;
2793
2794 /* stop timeout */
2795 callout_stop(&request->callout);
2796
2797 /* get response ptr's */
2798 rsp_in = ATA_INL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch));
2799 rsp_out = ATA_INL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch));
2800 slot = (((rsp_in & ~0xffffff00) >> 3)) & 0x1f;
2801 rsp_out &= 0xffffff00;
2802 rsp_out += (slot << 3);
2803 bus_dmamap_sync(ch->dma->work_tag, ch->dma->work_map,
2804 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2805 response = (struct ata_marvell_response *)
2806 (ch->dma->work + 1024 + (slot << 3));
2807
2808 /* record status for this request */
2809 request->status = response->dev_status;
2810 request->error = 0;
2811
2812 /* ack response */
2813 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), rsp_out);
2814
2815 /* update progress */
2816 if (!(request->status & ATA_S_ERROR) &&
2817 !(request->flags & ATA_R_TIMEOUT))
2818 request->donecount = request->bytecount;
2819
2820 /* unload SG list */
2821 ch->dma->unload(ch->dev);
2822
2823 res = ATA_OP_FINISHED;
2824 }
2825
2826 /* legacy ATA interrupt */
2827 else {
2828 res = ata_end_transaction(request);
2829 }
2830
2831 /* ack interrupt */
2832 ATA_OUTL(ctlr->r_res1, offset, ~(icr & (0x0101 << (ch->unit & 3))));
2833 return res;
2834 }
2835
2836 static void
2837 ata_marvell_edma_reset(device_t dev)
2838 {
2839 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2840 struct ata_channel *ch = device_get_softc(dev);
2841
2842 /* disable the EDMA machinery */
2843 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2844 while ((ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
2845 DELAY(10);
2846
2847 /* clear SATA error register */
2848 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2849
2850 /* clear any outstanding error interrupts */
2851 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2852
2853 /* unmask all error interrupts */
2854 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2855
2856 /* enable channel and test for devices */
2857 if (ata_sata_phy_reset(dev))
2858 ata_generic_reset(dev);
2859
2860 /* enable EDMA machinery */
2861 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2862 }
2863
2864 static void
2865 ata_marvell_edma_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs,
2866 int error)
2867 {
2868 struct ata_dmasetprd_args *args = xsc;
2869 struct ata_marvell_dma_prdentry *prd = args->dmatab;
2870 int i;
2871
2872 if ((args->error = error))
2873 return;
2874
2875 for (i = 0; i < nsegs; i++) {
2876 prd[i].addrlo = htole32(segs[i].ds_addr);
2877 prd[i].count = htole32(segs[i].ds_len);
2878 prd[i].addrhi = htole32((u_int64_t)segs[i].ds_addr >> 32);
2879 prd[i].reserved = 0;
2880 }
2881 prd[i - 1].count |= htole32(ATA_DMA_EOT);
2882 KASSERT(nsegs <= ATA_DMA_ENTRIES, ("too many DMA segment entries\n"));
2883 args->nsegs = nsegs;
2884 }
2885
2886 static void
2887 ata_marvell_edma_dmainit(device_t dev)
2888 {
2889 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2890 struct ata_channel *ch = device_get_softc(dev);
2891
2892 ata_dmainit(dev);
2893 if (ch->dma) {
2894 /* note start and stop are not used here */
2895 ch->dma->setprd = ata_marvell_edma_dmasetprd;
2896
2897 /* if 64bit support present adjust max address used */
2898 if (ATA_INL(ctlr->r_res1, 0x00d00) & 0x00000004)
2899 ch->dma->max_address = BUS_SPACE_MAXADDR;
2900
2901 /* chip does not reliably do 64K DMA transfers */
2902 ch->dma->max_iosize = 64 * DEV_BSIZE;
2903 }
2904 }
2905
2906
2907 /*
2908 * National chipset support functions
2909 */
2910 int
2911 ata_national_ident(device_t dev)
2912 {
2913 struct ata_pci_controller *ctlr = device_get_softc(dev);
2914
2915 /* this chip is a clone of the Cyrix chip, bugs and all */
2916 if (pci_get_devid(dev) == ATA_SC1100) {
2917 device_set_desc(dev, "National Geode SC1100 ATA33 controller");
2918 ctlr->chipinit = ata_national_chipinit;
2919 return 0;
2920 }
2921 return ENXIO;
2922 }
2923
2924 static int
2925 ata_national_chipinit(device_t dev)
2926 {
2927 struct ata_pci_controller *ctlr = device_get_softc(dev);
2928
2929 if (ata_setup_interrupt(dev))
2930 return ENXIO;
2931
2932 ctlr->setmode = ata_national_setmode;
2933 return 0;
2934 }
2935
2936 static void
2937 ata_national_setmode(device_t dev, int mode)
2938 {
2939 device_t gparent = GRANDPARENT(dev);
2940 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2941 struct ata_device *atadev = device_get_softc(dev);
2942 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2943 u_int32_t piotiming[] =
2944 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
2945 0x00803020, 0x20102010, 0x00100010,
2946 0x00100010, 0x00100010, 0x00100010 };
2947 u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
2948 u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
2949 int error;
2950
2951 ch->dma->alignment = 16;
2952 ch->dma->max_iosize = 64 * DEV_BSIZE;
2953
2954 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2955
2956 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2957
2958 if (bootverbose)
2959 device_printf(dev, "%s setting %s on National chip\n",
2960 (error) ? "failed" : "success", ata_mode2str(mode));
2961 if (!error) {
2962 if (mode >= ATA_UDMA0) {
2963 pci_write_config(gparent, 0x44 + (devno << 3),
2964 udmatiming[mode & ATA_MODE_MASK], 4);
2965 }
2966 else if (mode >= ATA_WDMA0) {
2967 pci_write_config(gparent, 0x44 + (devno << 3),
2968 dmatiming[mode & ATA_MODE_MASK], 4);
2969 }
2970 else {
2971 pci_write_config(gparent, 0x44 + (devno << 3),
2972 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
2973 0x80000000, 4);
2974 }
2975 pci_write_config(gparent, 0x40 + (devno << 3),
2976 piotiming[ata_mode2idx(mode)], 4);
2977 atadev->mode = mode;
2978 }
2979 }
2980
2981
2982 /*
2983 * NetCell chipset support functions
2984 */
2985 int
2986 ata_netcell_ident(device_t dev)
2987 {
2988 struct ata_pci_controller *ctlr = device_get_softc(dev);
2989
2990 if (pci_get_devid(dev) == ATA_NETCELL_SR) {
2991 device_set_desc(dev, "Netcell SyncRAID SR3000/5000 RAID Controller");
2992 ctlr->chipinit = ata_netcell_chipinit;
2993 return 0;
2994 }
2995 return ENXIO;
2996 }
2997
2998 static int
2999 ata_netcell_chipinit(device_t dev)
3000 {
3001 struct ata_pci_controller *ctlr = device_get_softc(dev);
3002
3003 if (ata_generic_chipinit(dev))
3004 return ENXIO;
3005
3006 ctlr->allocate = ata_netcell_allocate;
3007 return 0;
3008 }
3009
3010 static int
3011 ata_netcell_allocate(device_t dev)
3012 {
3013 struct ata_channel *ch = device_get_softc(dev);
3014
3015 /* setup the usual register normal pci style */
3016 if (ata_pci_allocate(dev))
3017 return ENXIO;
3018
3019 /* the NetCell only supports 16 bit PIO transfers */
3020 ch->flags |= ATA_USE_16BIT;
3021
3022 return 0;
3023 }
3024
3025
3026 /*
3027 * nVidia chipset support functions
3028 */
3029 int
3030 ata_nvidia_ident(device_t dev)
3031 {
3032 struct ata_pci_controller *ctlr = device_get_softc(dev);
3033 static struct ata_chip_id ids[] =
3034 {{ ATA_NFORCE1, 0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nForce" },
3035 { ATA_NFORCE2, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce2" },
3036 { ATA_NFORCE2_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce2 Pro" },
3037 { ATA_NFORCE2_PRO_S1, 0, 0, 0, ATA_SA150, "nForce2 Pro" },
3038 { ATA_NFORCE3, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce3" },
3039 { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce3 Pro" },
3040 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
3041 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
3042 { ATA_NFORCE_MCP04, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP" },
3043 { ATA_NFORCE_MCP04_S1, 0, 0, NV4, ATA_SA150, "nForce MCP" },
3044 { ATA_NFORCE_MCP04_S2, 0, 0, NV4, ATA_SA150, "nForce MCP" },
3045 { ATA_NFORCE_CK804, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce CK804" },
3046 { ATA_NFORCE_CK804_S1, 0, 0, NV4, ATA_SA300, "nForce CK804" },
3047 { ATA_NFORCE_CK804_S2, 0, 0, NV4, ATA_SA300, "nForce CK804" },
3048 { ATA_NFORCE_MCP51, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP51" },
3049 { ATA_NFORCE_MCP51_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP51" },
3050 { ATA_NFORCE_MCP51_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP51" },
3051 { ATA_NFORCE_MCP55, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP55" },
3052 { ATA_NFORCE_MCP55_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP55" },
3053 { ATA_NFORCE_MCP55_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP55" },
3054 { ATA_NFORCE_MCP61, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP61" },
3055 { ATA_NFORCE_MCP61_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP61" },
3056 { ATA_NFORCE_MCP61_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP61" },
3057 { ATA_NFORCE_MCP61_S3, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP61" },
3058 { ATA_NFORCE_MCP65, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP65" },
3059 { ATA_NFORCE_MCP67, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP67" },
3060 { ATA_NFORCE_MCP73, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP73" },
3061 { ATA_NFORCE_MCP77, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP77" },
3062 { 0, 0, 0, 0, 0, 0}} ;
3063
3064 if (!(ctlr->chip = ata_match_chip(dev, ids)))
3065 return ENXIO;
3066
3067 ata_set_desc(dev);
3068 ctlr->chipinit = ata_nvidia_chipinit;
3069 return 0;
3070 }
3071
3072 static int
3073 ata_nvidia_chipinit(device_t dev)
3074 {
3075 struct ata_pci_controller *ctlr = device_get_softc(dev);
3076
3077 if (ata_setup_interrupt(dev))
3078 return ENXIO;
3079
3080 if (ctlr->chip->max_dma >= ATA_SA150) {
3081 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
3082 ctlr->r_type2 = SYS_RES_IOPORT;
3083 else
3084 ctlr->r_type2 = SYS_RES_MEMORY;
3085 ctlr->r_rid2 = PCIR_BAR(5);
3086 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3087 &ctlr->r_rid2, RF_ACTIVE))) {
3088 int offset = ctlr->chip->cfg2 & NV4 ? 0x0440 : 0x0010;
3089
3090 ctlr->allocate = ata_nvidia_allocate;
3091 ctlr->reset = ata_nvidia_reset;
3092
3093 /* enable control access */
3094 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1);
3095
3096 if (ctlr->chip->cfg2 & NVQ) {
3097 /* clear interrupt status */
3098 ATA_OUTL(ctlr->r_res2, offset, 0x00ff00ff);
3099
3100 /* enable device and PHY state change interrupts */
3101 ATA_OUTL(ctlr->r_res2, offset + 4, 0x000d000d);
3102
3103 /* disable NCQ support */
3104 ATA_OUTL(ctlr->r_res2, 0x0400,
3105 ATA_INL(ctlr->r_res2, 0x0400) & 0xfffffff9);
3106 }
3107 else {
3108 /* clear interrupt status */
3109 ATA_OUTB(ctlr->r_res2, offset, 0xff);
3110
3111 /* enable device and PHY state change interrupts */
3112 ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd);
3113 }
3114
3115 /* enable PCI interrupt */
3116 pci_write_config(dev, PCIR_COMMAND,
3117 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3118
3119 }
3120 ctlr->setmode = ata_sata_setmode;
3121 }
3122 else {
3123 /* disable prefetch, postwrite */
3124 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
3125 ctlr->setmode = ata_via_family_setmode;
3126 }
3127 return 0;
3128 }
3129
3130 static int
3131 ata_nvidia_allocate(device_t dev)
3132 {
3133 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3134 struct ata_channel *ch = device_get_softc(dev);
3135
3136 /* setup the usual register normal pci style */
3137 if (ata_pci_allocate(dev))
3138 return ENXIO;
3139
3140 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3141 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
3142 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3143 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
3144 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3145 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
3146
3147 ch->hw.status = ata_nvidia_status;
3148 ch->flags |= ATA_NO_SLAVE;
3149
3150 return 0;
3151 }
3152
3153 static int
3154 ata_nvidia_status(device_t dev)
3155 {
3156 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3157 struct ata_channel *ch = device_get_softc(dev);
3158 int offset = ctlr->chip->cfg2 & NV4 ? 0x0440 : 0x0010;
3159 int shift = ch->unit << (ctlr->chip->cfg2 & NVQ ? 4 : 2);
3160 u_int32_t istatus = ATA_INL(ctlr->r_res2, offset);
3161
3162 /* do we have any PHY events ? */
3163 if (istatus & (0x0c << shift))
3164 ata_sata_phy_check_events(dev);
3165
3166 /* clear interrupt(s) */
3167 ATA_OUTB(ctlr->r_res2, offset,
3168 (0x0f << shift) | (ctlr->chip->cfg2 & NVQ ? 0x00f000f0 : 0));
3169
3170 /* do we have any device action ? */
3171 return (istatus & (0x01 << shift));
3172 }
3173
3174 static void
3175 ata_nvidia_reset(device_t dev)
3176 {
3177 if (ata_sata_phy_reset(dev))
3178 ata_generic_reset(dev);
3179 }
3180
3181
3182 /*
3183 * Promise chipset support functions
3184 */
3185 #define ATA_PDC_APKT_OFFSET 0x00000010
3186 #define ATA_PDC_HPKT_OFFSET 0x00000040
3187 #define ATA_PDC_ASG_OFFSET 0x00000080
3188 #define ATA_PDC_LSG_OFFSET 0x000000c0
3189 #define ATA_PDC_HSG_OFFSET 0x00000100
3190 #define ATA_PDC_CHN_OFFSET 0x00000400
3191 #define ATA_PDC_BUF_BASE 0x00400000
3192 #define ATA_PDC_BUF_OFFSET 0x00100000
3193 #define ATA_PDC_MAX_HPKT 8
3194 #define ATA_PDC_WRITE_REG 0x00
3195 #define ATA_PDC_WRITE_CTL 0x0e
3196 #define ATA_PDC_WRITE_END 0x08
3197 #define ATA_PDC_WAIT_NBUSY 0x10
3198 #define ATA_PDC_WAIT_READY 0x18
3199 #define ATA_PDC_1B 0x20
3200 #define ATA_PDC_2B 0x40
3201
3202 struct host_packet {
3203 u_int32_t addr;
3204 TAILQ_ENTRY(host_packet) chain;
3205 };
3206
3207 struct ata_promise_sx4 {
3208 struct mtx mtx;
3209 TAILQ_HEAD(, host_packet) queue;
3210 int busy;
3211 };
3212
3213 int
3214 ata_promise_ident(device_t dev)
3215 {
3216 struct ata_pci_controller *ctlr = device_get_softc(dev);
3217 struct ata_chip_id *idx;
3218 static struct ata_chip_id ids[] =
3219 {{ ATA_PDC20246, 0, PROLD, 0x00, ATA_UDMA2, "PDC20246" },
3220 { ATA_PDC20262, 0, PRNEW, 0x00, ATA_UDMA4, "PDC20262" },
3221 { ATA_PDC20263, 0, PRNEW, 0x00, ATA_UDMA4, "PDC20263" },
3222 { ATA_PDC20265, 0, PRNEW, 0x00, ATA_UDMA5, "PDC20265" },
3223 { ATA_PDC20267, 0, PRNEW, 0x00, ATA_UDMA5, "PDC20267" },
3224 { ATA_PDC20268, 0, PRTX, PRTX4, ATA_UDMA5, "PDC20268" },
3225 { ATA_PDC20269, 0, PRTX, 0x00, ATA_UDMA6, "PDC20269" },
3226 { ATA_PDC20270, 0, PRTX, PRTX4, ATA_UDMA5, "PDC20270" },
3227 { ATA_PDC20271, 0, PRTX, 0x00, ATA_UDMA6, "PDC20271" },
3228 { ATA_PDC20275, 0, PRTX, 0x00, ATA_UDMA6, "PDC20275" },
3229 { ATA_PDC20276, 0, PRTX, PRSX6K, ATA_UDMA6, "PDC20276" },
3230 { ATA_PDC20277, 0, PRTX, 0x00, ATA_UDMA6, "PDC20277" },
3231 { ATA_PDC20318, 0, PRMIO, PRSATA, ATA_SA150, "PDC20318" },
3232 { ATA_PDC20319, 0, PRMIO, PRSATA, ATA_SA150, "PDC20319" },
3233 { ATA_PDC20371, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20371" },
3234 { ATA_PDC20375, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20375" },
3235 { ATA_PDC20376, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20376" },
3236 { ATA_PDC20377, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20377" },
3237 { ATA_PDC20378, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20378" },
3238 { ATA_PDC20379, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20379" },
3239 { ATA_PDC20571, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20571" },
3240 { ATA_PDC20575, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20575" },
3241 { ATA_PDC20579, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20579" },
3242 { ATA_PDC20771, 0, PRMIO, PRCMBO2, ATA_SA300, "PDC20771" },
3243 { ATA_PDC40775, 0, PRMIO, PRCMBO2, ATA_SA300, "PDC40775" },
3244 { ATA_PDC20617, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20617" },
3245 { ATA_PDC20618, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20618" },
3246 { ATA_PDC20619, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20619" },
3247 { ATA_PDC20620, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20620" },
3248 { ATA_PDC20621, 0, PRMIO, PRSX4X, ATA_UDMA5, "PDC20621" },
3249 { ATA_PDC20622, 0, PRMIO, PRSX4X, ATA_SA150, "PDC20622" },
3250 { ATA_PDC40518, 0, PRMIO, PRSATA2, ATA_SA150, "PDC40518" },
3251 { ATA_PDC40519, 0, PRMIO, PRSATA2, ATA_SA150, "PDC40519" },
3252 { ATA_PDC40718, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40718" },
3253 { ATA_PDC40719, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40719" },
3254 { ATA_PDC40779, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40779" },
3255 { 0, 0, 0, 0, 0, 0}};
3256 char buffer[64];
3257 uintptr_t devid = 0;
3258
3259 if (!(idx = ata_match_chip(dev, ids)))
3260 return ENXIO;
3261
3262 /* if we are on a SuperTrak SX6000 dont attach */
3263 if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
3264 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
3265 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
3266 devid == ATA_I960RM)
3267 return ENXIO;
3268
3269 strcpy(buffer, "Promise ");
3270 strcat(buffer, idx->text);
3271
3272 /* if we are on a FastTrak TX4, adjust the interrupt resource */
3273 if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
3274 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
3275 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
3276 ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
3277 static long start = 0, end = 0;
3278
3279 if (pci_get_slot(dev) == 1) {
3280 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
3281 strcat(buffer, " (channel 0+1)");
3282 }
3283 else if (pci_get_slot(dev) == 2 && start && end) {
3284 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
3285 strcat(buffer, " (channel 2+3)");
3286 }
3287 else {
3288 start = end = 0;
3289 }
3290 }
3291 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
3292 device_set_desc_copy(dev, buffer);
3293 ctlr->chip = idx;
3294 ctlr->chipinit = ata_promise_chipinit;
3295 return 0;
3296 }
3297
3298 static int
3299 ata_promise_chipinit(device_t dev)
3300 {
3301 struct ata_pci_controller *ctlr = device_get_softc(dev);
3302 int fake_reg, stat_reg;
3303
3304 if (ata_setup_interrupt(dev))
3305 return ENXIO;
3306
3307 switch (ctlr->chip->cfg1) {
3308 case PRNEW:
3309 /* setup clocks */
3310 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
3311
3312 ctlr->dmainit = ata_promise_dmainit;
3313 /* FALLTHROUGH */
3314
3315 case PROLD:
3316 /* enable burst mode */
3317 ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
3318 ctlr->allocate = ata_promise_allocate;
3319 ctlr->setmode = ata_promise_setmode;
3320 return 0;
3321
3322 case PRTX:
3323 ctlr->allocate = ata_promise_tx2_allocate;
3324 ctlr->setmode = ata_promise_setmode;
3325 return 0;
3326
3327 case PRMIO:
3328 ctlr->r_type1 = SYS_RES_MEMORY;
3329 ctlr->r_rid1 = PCIR_BAR(4);
3330 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
3331 &ctlr->r_rid1, RF_ACTIVE)))
3332 goto failnfree;
3333
3334 ctlr->r_type2 = SYS_RES_MEMORY;
3335 ctlr->r_rid2 = PCIR_BAR(3);
3336 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3337 &ctlr->r_rid2, RF_ACTIVE)))
3338 goto failnfree;
3339
3340 if (ctlr->chip->cfg2 == PRSX4X) {
3341 struct ata_promise_sx4 *hpkt;
3342 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
3343
3344 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
3345 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3346 ata_promise_sx4_intr, ctlr, &ctlr->handle)) {
3347 device_printf(dev, "unable to setup interrupt\n");
3348 goto failnfree;
3349 }
3350
3351 /* print info about cache memory */
3352 device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
3353 (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
3354 ((dimm >> 24) & 0xff),
3355 ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
3356 " ECC enabled" : "" );
3357
3358 /* adjust cache memory parameters */
3359 ATA_OUTL(ctlr->r_res2, 0x000c000c,
3360 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
3361
3362 /* setup host packet controls */
3363 hpkt = malloc(sizeof(struct ata_promise_sx4),
3364 M_TEMP, M_NOWAIT | M_ZERO);
3365 mtx_init(&hpkt->mtx, "ATA promise HPKT lock", NULL, MTX_DEF);
3366 TAILQ_INIT(&hpkt->queue);
3367 hpkt->busy = 0;
3368 device_set_ivars(dev, hpkt);
3369 ctlr->allocate = ata_promise_mio_allocate;
3370 ctlr->reset = ata_promise_mio_reset;
3371 ctlr->dmainit = ata_promise_mio_dmainit;
3372 ctlr->setmode = ata_promise_setmode;
3373 ctlr->channels = 4;
3374 return 0;
3375 }
3376
3377 /* mio type controllers need an interrupt intercept */
3378 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
3379 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3380 ata_promise_mio_intr, ctlr, &ctlr->handle)) {
3381 device_printf(dev, "unable to setup interrupt\n");
3382 goto failnfree;
3383 }
3384
3385 switch (ctlr->chip->cfg2) {
3386 case PRPATA:
3387 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
3388 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
3389 goto sata150;
3390 case PRCMBO:
3391 ctlr->channels = 3;
3392 goto sata150;
3393 case PRSATA:
3394 ctlr->channels = 4;
3395 sata150:
3396 fake_reg = 0x60;
3397 stat_reg = 0x6c;
3398 break;
3399
3400 case PRCMBO2:
3401 ctlr->channels = 3;
3402 goto sataii;
3403 case PRSATA2:
3404 default:
3405 ctlr->channels = 4;
3406 sataii:
3407 fake_reg = 0x54;
3408 stat_reg = 0x60;
3409 break;
3410 }
3411
3412 /* prime fake interrupt register */
3413 ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
3414
3415 /* clear SATA status and unmask interrupts */
3416 ATA_OUTL(ctlr->r_res2, stat_reg, 0x000000ff);
3417
3418 /* enable "long burst lenght" on gen2 chips */
3419 if ((ctlr->chip->cfg2 == PRSATA2) || (ctlr->chip->cfg2 == PRCMBO2))
3420 ATA_OUTL(ctlr->r_res2, 0x44, ATA_INL(ctlr->r_res2, 0x44) | 0x2000);
3421
3422 ctlr->allocate = ata_promise_mio_allocate;
3423 ctlr->reset = ata_promise_mio_reset;
3424 ctlr->dmainit = ata_promise_mio_dmainit;
3425 ctlr->setmode = ata_promise_mio_setmode;
3426
3427 return 0;
3428 }
3429
3430 failnfree:
3431 if (ctlr->r_res2)
3432 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
3433 if (ctlr->r_res1)
3434 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
3435 return ENXIO;
3436 }
3437
3438 static int
3439 ata_promise_allocate(device_t dev)
3440 {
3441 struct ata_channel *ch = device_get_softc(dev);
3442
3443 if (ata_pci_allocate(dev))
3444 return ENXIO;
3445
3446 ch->hw.status = ata_promise_status;
3447 return 0;
3448 }
3449
3450 static int
3451 ata_promise_status(device_t dev)
3452 {
3453 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3454 struct ata_channel *ch = device_get_softc(dev);
3455
3456 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)) {
3457 return ata_pci_status(dev);
3458 }
3459 return 0;
3460 }
3461
3462 static int
3463 ata_promise_dmastart(device_t dev)
3464 {
3465 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3466 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3467 struct ata_device *atadev = device_get_softc(dev);
3468
3469 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3470 ATA_OUTB(ctlr->r_res1, 0x11,
3471 ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
3472 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20,
3473 ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
3474 (ch->dma->cur_iosize >> 1));
3475 }
3476 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
3477 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
3478 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
3479 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3480 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
3481 ATA_BMCMD_START_STOP);
3482 ch->flags |= ATA_DMA_ACTIVE;
3483 return 0;
3484 }
3485
3486 static int
3487 ata_promise_dmastop(device_t dev)
3488 {
3489 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3490 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3491 struct ata_device *atadev = device_get_softc(dev);
3492 int error;
3493
3494 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3495 ATA_OUTB(ctlr->r_res1, 0x11,
3496 ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
3497 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20, 0);
3498 }
3499 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
3500 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3501 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3502 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3503 ch->flags &= ~ATA_DMA_ACTIVE;
3504 return error;
3505 }
3506
3507 static void
3508 ata_promise_dmareset(device_t dev)
3509 {
3510 struct ata_channel *ch = device_get_softc(dev);
3511
3512 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3513 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3514 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3515 ch->flags &= ~ATA_DMA_ACTIVE;
3516 }
3517
3518 static void
3519 ata_promise_dmainit(device_t dev)
3520 {
3521 struct ata_channel *ch = device_get_softc(dev);
3522
3523 ata_dmainit(dev);
3524 if (ch->dma) {
3525 ch->dma->start = ata_promise_dmastart;
3526 ch->dma->stop = ata_promise_dmastop;
3527 ch->dma->reset = ata_promise_dmareset;
3528 }
3529 }
3530
3531 static void
3532 ata_promise_setmode(device_t dev, int mode)
3533 {
3534 device_t gparent = GRANDPARENT(dev);
3535 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3536 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3537 struct ata_device *atadev = device_get_softc(dev);
3538 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3539 int error;
3540 u_int32_t timings[][2] = {
3541 /* PROLD PRNEW mode */
3542 { 0x004ff329, 0x004fff2f }, /* PIO 0 */
3543 { 0x004fec25, 0x004ff82a }, /* PIO 1 */
3544 { 0x004fe823, 0x004ff026 }, /* PIO 2 */
3545 { 0x004fe622, 0x004fec24 }, /* PIO 3 */
3546 { 0x004fe421, 0x004fe822 }, /* PIO 4 */
3547 { 0x004567f3, 0x004acef6 }, /* MWDMA 0 */
3548 { 0x004467f3, 0x0048cef6 }, /* MWDMA 1 */
3549 { 0x004367f3, 0x0046cef6 }, /* MWDMA 2 */
3550 { 0x004367f3, 0x0046cef6 }, /* UDMA 0 */
3551 { 0x004247f3, 0x00448ef6 }, /* UDMA 1 */
3552 { 0x004127f3, 0x00436ef6 }, /* UDMA 2 */
3553 { 0, 0x00424ef6 }, /* UDMA 3 */
3554 { 0, 0x004127f3 }, /* UDMA 4 */
3555 { 0, 0x004127f3 } /* UDMA 5 */
3556 };
3557
3558 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3559
3560 switch (ctlr->chip->cfg1) {
3561 case PROLD:
3562 case PRNEW:
3563 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
3564 (ch->unit ? 1 << 11 : 1 << 10))) {
3565 ata_print_cable(dev, "controller");
3566 mode = ATA_UDMA2;
3567 }
3568 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
3569 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
3570 break;
3571
3572 case PRTX:
3573 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3574 if (mode > ATA_UDMA2 &&
3575 ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
3576 ata_print_cable(dev, "controller");
3577 mode = ATA_UDMA2;
3578 }
3579 break;
3580
3581 case PRMIO:
3582 if (mode > ATA_UDMA2 &&
3583 (ATA_INL(ctlr->r_res2,
3584 (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
3585 (ch->unit << 7)) & 0x01000000)) {
3586 ata_print_cable(dev, "controller");
3587 mode = ATA_UDMA2;
3588 }
3589 break;
3590 }
3591
3592 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3593
3594 if (bootverbose)
3595 device_printf(dev, "%ssetting %s on %s chip\n",
3596 (error) ? "FAILURE " : "",
3597 ata_mode2str(mode), ctlr->chip->text);
3598 if (!error) {
3599 if (ctlr->chip->cfg1 < PRTX)
3600 pci_write_config(gparent, 0x60 + (devno << 2),
3601 timings[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
3602 atadev->mode = mode;
3603 }
3604 return;
3605 }
3606
3607 static int
3608 ata_promise_tx2_allocate(device_t dev)
3609 {
3610 struct ata_channel *ch = device_get_softc(dev);
3611
3612 if (ata_pci_allocate(dev))
3613 return ENXIO;
3614
3615 ch->hw.status = ata_promise_tx2_status;
3616 return 0;
3617 }
3618
3619 static int
3620 ata_promise_tx2_status(device_t dev)
3621 {
3622 struct ata_channel *ch = device_get_softc(dev);
3623
3624 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3625 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
3626 return ata_pci_status(dev);
3627 }
3628 return 0;
3629 }
3630
3631 static int
3632 ata_promise_mio_allocate(device_t dev)
3633 {
3634 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3635 struct ata_channel *ch = device_get_softc(dev);
3636 int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
3637 int i;
3638
3639 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
3640 ch->r_io[i].res = ctlr->r_res2;
3641 ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7);
3642 }
3643 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
3644 ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
3645 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
3646 ata_default_registers(dev);
3647 if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
3648 ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
3649 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3650 ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
3651 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3652 ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
3653 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3654 ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
3655 ch->flags |= ATA_NO_SLAVE;
3656 }
3657 ch->flags |= ATA_USE_16BIT;
3658
3659 ata_generic_hw(dev);
3660 if (ctlr->chip->cfg2 & PRSX4X) {
3661 ch->hw.command = ata_promise_sx4_command;
3662 }
3663 else {
3664 ch->hw.command = ata_promise_mio_command;
3665 ch->hw.status = ata_promise_mio_status;
3666 }
3667 return 0;
3668 }
3669
3670 static void
3671 ata_promise_mio_intr(void *data)
3672 {
3673 struct ata_pci_controller *ctlr = data;
3674 struct ata_channel *ch;
3675 u_int32_t vector;
3676 int unit, fake_reg;
3677
3678 switch (ctlr->chip->cfg2) {
3679 case PRPATA:
3680 case PRCMBO:
3681 case PRSATA:
3682 fake_reg = 0x60;
3683 break;
3684 case PRCMBO2:
3685 case PRSATA2:
3686 default:
3687 fake_reg = 0x54;
3688 break;
3689 }
3690
3691 /*
3692 * since reading interrupt status register on early "mio" chips
3693 * clears the status bits we cannot read it for each channel later on
3694 * in the generic interrupt routine.
3695 * store the bits in an unused register in the chip so we can read
3696 * it from there safely to get around this "feature".
3697 */
3698 vector = ATA_INL(ctlr->r_res2, 0x040);
3699 ATA_OUTL(ctlr->r_res2, 0x040, vector);
3700 ATA_OUTL(ctlr->r_res2, fake_reg, vector);
3701
3702 for (unit = 0; unit < ctlr->channels; unit++) {
3703 if ((ch = ctlr->interrupt[unit].argument))
3704 ctlr->interrupt[unit].function(ch);
3705 }
3706
3707 ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
3708 }
3709
3710 static int
3711 ata_promise_mio_status(device_t dev)
3712 {
3713 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3714 struct ata_channel *ch = device_get_softc(dev);
3715 struct ata_connect_task *tp;
3716 u_int32_t fake_reg, stat_reg, vector, status;
3717
3718 switch (ctlr->chip->cfg2) {
3719 case PRPATA:
3720 case PRCMBO:
3721 case PRSATA:
3722 fake_reg = 0x60;
3723 stat_reg = 0x6c;
3724 break;
3725 case PRCMBO2:
3726 case PRSATA2:
3727 default:
3728 fake_reg = 0x54;
3729 stat_reg = 0x60;
3730 break;
3731 }
3732
3733 /* read and acknowledge interrupt */
3734 vector = ATA_INL(ctlr->r_res2, fake_reg);
3735
3736 /* read and clear interface status */
3737 status = ATA_INL(ctlr->r_res2, stat_reg);
3738 ATA_OUTL(ctlr->r_res2, stat_reg, status & (0x00000011 << ch->unit));
3739
3740 /* check for and handle disconnect events */
3741 if ((status & (0x00000001 << ch->unit)) &&
3742 (tp = (struct ata_connect_task *)
3743 malloc(sizeof(struct ata_connect_task),
3744 M_ATA, M_NOWAIT | M_ZERO))) {
3745
3746 if (bootverbose)
3747 device_printf(ch->dev, "DISCONNECT requested\n");
3748 tp->action = ATA_C_DETACH;
3749 tp->dev = ch->dev;
3750 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3751 taskqueue_enqueue(taskqueue_thread, &tp->task);
3752 }
3753
3754 /* check for and handle connect events */
3755 if ((status & (0x00000010 << ch->unit)) &&
3756 (tp = (struct ata_connect_task *)
3757 malloc(sizeof(struct ata_connect_task),
3758 M_ATA, M_NOWAIT | M_ZERO))) {
3759
3760 if (bootverbose)
3761 device_printf(ch->dev, "CONNECT requested\n");
3762 tp->action = ATA_C_ATTACH;
3763 tp->dev = ch->dev;
3764 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3765 taskqueue_enqueue(taskqueue_thread, &tp->task);
3766 }
3767
3768 /* do we have any device action ? */
3769 return (vector & (1 << (ch->unit + 1)));
3770 }
3771
3772 static int
3773 ata_promise_mio_command(struct ata_request *request)
3774 {
3775 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
3776 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
3777 u_int32_t *wordp = (u_int32_t *)ch->dma->work;
3778
3779 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
3780
3781 /* XXX SOS add ATAPI commands support later */
3782 switch (request->u.ata.command) {
3783 default:
3784 return ata_generic_command(request);
3785
3786 case ATA_READ_DMA:
3787 case ATA_READ_DMA48:
3788 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
3789 break;
3790
3791 case ATA_WRITE_DMA:
3792 case ATA_WRITE_DMA48:
3793 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
3794 break;
3795 }
3796 wordp[1] = htole32(ch->dma->sg_bus);
3797 wordp[2] = 0;
3798 ata_promise_apkt((u_int8_t*)wordp, request);
3799
3800 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
3801 return 0;
3802 }
3803
3804 static void
3805 ata_promise_mio_reset(device_t dev)
3806 {
3807 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3808 struct ata_channel *ch = device_get_softc(dev);
3809 struct ata_promise_sx4 *hpktp;
3810
3811 switch (ctlr->chip->cfg2) {
3812 case PRSX4X:
3813
3814 /* softreset channel ATA module */
3815 hpktp = device_get_ivars(ctlr->dev);
3816 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
3817 ata_udelay(1000);
3818 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
3819 (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
3820 ~0x00003f9f) | (ch->unit + 1));
3821
3822 /* softreset HOST module */ /* XXX SOS what about other outstandings */
3823 mtx_lock(&hpktp->mtx);
3824 ATA_OUTL(ctlr->r_res2, 0xc012c,
3825 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
3826 DELAY(10);
3827 ATA_OUTL(ctlr->r_res2, 0xc012c,
3828 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
3829 hpktp->busy = 0;
3830 mtx_unlock(&hpktp->mtx);
3831 ata_generic_reset(dev);
3832 break;
3833
3834 case PRPATA:
3835 case PRCMBO:
3836 case PRSATA:
3837 if ((ctlr->chip->cfg2 == PRSATA) ||
3838 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
3839
3840 /* mask plug/unplug intr */
3841 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
3842 }
3843
3844 /* softreset channels ATA module */
3845 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
3846 ata_udelay(10000);
3847 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
3848 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
3849 ~0x00003f9f) | (ch->unit + 1));
3850
3851 if ((ctlr->chip->cfg2 == PRSATA) ||
3852 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
3853
3854 if (ata_sata_phy_reset(dev))
3855 ata_generic_reset(dev);
3856
3857 /* reset and enable plug/unplug intr */
3858 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
3859 }
3860 else
3861 ata_generic_reset(dev);
3862 break;
3863
3864 case PRCMBO2:
3865 case PRSATA2:
3866 if ((ctlr->chip->cfg2 == PRSATA2) ||
3867 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
3868 /* set portmultiplier port */
3869 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
3870
3871 /* mask plug/unplug intr */
3872 ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
3873 }
3874
3875 /* softreset channels ATA module */
3876 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
3877 ata_udelay(10000);
3878 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
3879 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
3880 ~0x00003f9f) | (ch->unit + 1));
3881
3882 if ((ctlr->chip->cfg2 == PRSATA2) ||
3883 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
3884
3885 /* set PHY mode to "improved" */
3886 ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
3887 (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
3888 ~0x00000003) | 0x00000001);
3889
3890 if (ata_sata_phy_reset(dev))
3891 ata_generic_reset(dev);
3892
3893 /* reset and enable plug/unplug intr */
3894 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
3895
3896 /* set portmultiplier port */
3897 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
3898 }
3899 else
3900 ata_generic_reset(dev);
3901 break;
3902
3903 }
3904 }
3905
3906 static void
3907 ata_promise_mio_dmainit(device_t dev)
3908 {
3909 struct ata_channel *ch = device_get_softc(dev);
3910
3911 /* note start and stop are not used here */
3912 ata_dmainit(dev);
3913 if (ch->dma)
3914 ch->dma->setprd = ata_promise_mio_setprd;
3915 }
3916
3917
3918 #define MAXLASTSGSIZE (32 * sizeof(u_int32_t))
3919 static void
3920 ata_promise_mio_setprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
3921 {
3922 struct ata_dmasetprd_args *args = xsc;
3923 struct ata_dma_prdentry *prd = args->dmatab;
3924 int i;
3925
3926 if ((args->error = error))
3927 return;
3928
3929 for (i = 0; i < nsegs; i++) {
3930 prd[i].addr = htole32(segs[i].ds_addr);
3931 prd[i].count = htole32(segs[i].ds_len);
3932 }
3933 if (segs[i - 1].ds_len > MAXLASTSGSIZE) {
3934 //printf("split last SG element of %u\n", segs[i - 1].ds_len);
3935 prd[i - 1].count = htole32(segs[i - 1].ds_len - MAXLASTSGSIZE);
3936 prd[i].count = htole32(MAXLASTSGSIZE);
3937 prd[i].addr = htole32(segs[i - 1].ds_addr +
3938 (segs[i - 1].ds_len - MAXLASTSGSIZE));
3939 nsegs++;
3940 i++;
3941 }
3942 prd[i - 1].count |= htole32(ATA_DMA_EOT);
3943 KASSERT(nsegs <= ATA_DMA_ENTRIES, ("too many DMA segment entries\n"));
3944 args->nsegs = nsegs;
3945 }
3946
3947 static void
3948 ata_promise_mio_setmode(device_t dev, int mode)
3949 {
3950 device_t gparent = GRANDPARENT(dev);
3951 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3952 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3953
3954 if ( (ctlr->chip->cfg2 == PRSATA) ||
3955 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2)) ||
3956 (ctlr->chip->cfg2 == PRSATA2) ||
3957 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2)))
3958 ata_sata_setmode(dev, mode);
3959 else
3960 ata_promise_setmode(dev, mode);
3961 }
3962
3963 static void
3964 ata_promise_sx4_intr(void *data)
3965 {
3966 struct ata_pci_controller *ctlr = data;
3967 struct ata_channel *ch;
3968 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
3969 int unit;
3970
3971 for (unit = 0; unit < ctlr->channels; unit++) {
3972 if (vector & (1 << (unit + 1)))
3973 if ((ch = ctlr->interrupt[unit].argument))
3974 ctlr->interrupt[unit].function(ch);
3975 if (vector & (1 << (unit + 5)))
3976 if ((ch = ctlr->interrupt[unit].argument))
3977 ata_promise_queue_hpkt(ctlr,
3978 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
3979 ATA_PDC_HPKT_OFFSET));
3980 if (vector & (1 << (unit + 9))) {
3981 ata_promise_next_hpkt(ctlr);
3982 if ((ch = ctlr->interrupt[unit].argument))
3983 ctlr->interrupt[unit].function(ch);
3984 }
3985 if (vector & (1 << (unit + 13))) {
3986 ata_promise_next_hpkt(ctlr);
3987 if ((ch = ctlr->interrupt[unit].argument))
3988 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
3989 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
3990 ATA_PDC_APKT_OFFSET));
3991 }
3992 }
3993 }
3994
3995 static int
3996 ata_promise_sx4_command(struct ata_request *request)
3997 {
3998 device_t gparent = GRANDPARENT(request->dev);
3999 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4000 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4001 struct ata_dma_prdentry *prd = ch->dma->sg;
4002 caddr_t window = rman_get_virtual(ctlr->r_res1);
4003 u_int32_t *wordp;
4004 int i, idx, length = 0;
4005
4006 /* XXX SOS add ATAPI commands support later */
4007 switch (request->u.ata.command) {
4008
4009 default:
4010 return -1;
4011
4012 case ATA_ATA_IDENTIFY:
4013 case ATA_READ:
4014 case ATA_READ48:
4015 case ATA_READ_MUL:
4016 case ATA_READ_MUL48:
4017 case ATA_WRITE:
4018 case ATA_WRITE48:
4019 case ATA_WRITE_MUL:
4020 case ATA_WRITE_MUL48:
4021 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
4022 return ata_generic_command(request);
4023
4024 case ATA_SETFEATURES:
4025 case ATA_FLUSHCACHE:
4026 case ATA_FLUSHCACHE48:
4027 case ATA_SLEEP:
4028 case ATA_SET_MULTI:
4029 wordp = (u_int32_t *)
4030 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
4031 wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
4032 wordp[1] = 0;
4033 wordp[2] = 0;
4034 ata_promise_apkt((u_int8_t *)wordp, request);
4035 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
4036 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
4037 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
4038 htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
4039 return 0;
4040
4041 case ATA_READ_DMA:
4042 case ATA_READ_DMA48:
4043 case ATA_WRITE_DMA:
4044 case ATA_WRITE_DMA48:
4045 wordp = (u_int32_t *)
4046 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
4047 i = idx = 0;
4048 do {
4049 wordp[idx++] = prd[i].addr;
4050 wordp[idx++] = prd[i].count;
4051 length += (prd[i].count & ~ATA_DMA_EOT);
4052 } while (!(prd[i++].count & ATA_DMA_EOT));
4053
4054 wordp = (u_int32_t *)
4055 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
4056 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
4057 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
4058
4059 wordp = (u_int32_t *)
4060 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
4061 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
4062 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
4063
4064 wordp = (u_int32_t *)
4065 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
4066 if (request->flags & ATA_R_READ)
4067 wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
4068 if (request->flags & ATA_R_WRITE)
4069 wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
4070 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
4071 wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
4072 wordp[3] = 0;
4073
4074 wordp = (u_int32_t *)
4075 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
4076 if (request->flags & ATA_R_READ)
4077 wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
4078 if (request->flags & ATA_R_WRITE)
4079 wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
4080 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
4081 wordp[2] = 0;
4082 ata_promise_apkt((u_int8_t *)wordp, request);
4083 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
4084
4085 if (request->flags & ATA_R_READ) {
4086 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
4087 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
4088 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
4089 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
4090 }
4091 if (request->flags & ATA_R_WRITE) {
4092 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
4093 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
4094 ata_promise_queue_hpkt(ctlr,
4095 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
4096 }
4097 return 0;
4098 }
4099 }
4100
4101 static int
4102 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
4103 {
4104 struct ata_device *atadev = device_get_softc(request->dev);
4105 int i = 12;
4106
4107 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
4108 bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
4109 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
4110 bytep[i++] = ATA_A_4BIT;
4111
4112 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
4113 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
4114 bytep[i++] = request->u.ata.feature >> 8;
4115 bytep[i++] = request->u.ata.feature;
4116 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
4117 bytep[i++] = request->u.ata.count >> 8;
4118 bytep[i++] = request->u.ata.count;
4119 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
4120 bytep[i++] = request->u.ata.lba >> 24;
4121 bytep[i++] = request->u.ata.lba;
4122 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
4123 bytep[i++] = request->u.ata.lba >> 32;
4124 bytep[i++] = request->u.ata.lba >> 8;
4125 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
4126 bytep[i++] = request->u.ata.lba >> 40;
4127 bytep[i++] = request->u.ata.lba >> 16;
4128 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
4129 bytep[i++] = ATA_D_LBA | atadev->unit;
4130 }
4131 else {
4132 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
4133 bytep[i++] = request->u.ata.feature;
4134 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
4135 bytep[i++] = request->u.ata.count;
4136 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
4137 bytep[i++] = request->u.ata.lba;
4138 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
4139 bytep[i++] = request->u.ata.lba >> 8;
4140 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
4141 bytep[i++] = request->u.ata.lba >> 16;
4142 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
4143 bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
4144 ATA_D_IBM | atadev->unit | ((request->u.ata.lba >> 24)&0xf);
4145 }
4146 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
4147 bytep[i++] = request->u.ata.command;
4148 return i;
4149 }
4150
4151 static void
4152 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
4153 {
4154 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
4155
4156 mtx_lock(&hpktp->mtx);
4157 if (hpktp->busy) {
4158 struct host_packet *hp =
4159 malloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
4160 hp->addr = hpkt;
4161 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
4162 }
4163 else {
4164 hpktp->busy = 1;
4165 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
4166 }
4167 mtx_unlock(&hpktp->mtx);
4168 }
4169
4170 static void
4171 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
4172 {
4173 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
4174 struct host_packet *hp;
4175
4176 mtx_lock(&hpktp->mtx);
4177 if ((hp = TAILQ_FIRST(&hpktp->queue))) {
4178 TAILQ_REMOVE(&hpktp->queue, hp, chain);
4179 ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
4180 free(hp, M_TEMP);
4181 }
4182 else
4183 hpktp->busy = 0;
4184 mtx_unlock(&hpktp->mtx);
4185 }
4186
4187
4188 /*
4189 * ServerWorks chipset support functions
4190 */
4191 int
4192 ata_serverworks_ident(device_t dev)
4193 {
4194 struct ata_pci_controller *ctlr = device_get_softc(dev);
4195 static struct ata_chip_id ids[] =
4196 {{ ATA_ROSB4, 0x00, SWKS33, 0, ATA_UDMA2, "ROSB4" },
4197 { ATA_CSB5, 0x92, SWKS100, 0, ATA_UDMA5, "CSB5" },
4198 { ATA_CSB5, 0x00, SWKS66, 0, ATA_UDMA4, "CSB5" },
4199 { ATA_CSB6, 0x00, SWKS100, 0, ATA_UDMA5, "CSB6" },
4200 { ATA_CSB6_1, 0x00, SWKS66, 0, ATA_UDMA4, "CSB6" },
4201 { ATA_HT1000, 0x00, SWKS100, 0, ATA_UDMA5, "HT1000" },
4202 { ATA_HT1000_S1, 0x00, SWKSMIO, 4, ATA_SA150, "HT1000" },
4203 { ATA_HT1000_S2, 0x00, SWKSMIO, 4, ATA_SA150, "HT1000" },
4204 { ATA_K2, 0x00, SWKSMIO, 4, ATA_SA150, "K2" },
4205 { ATA_FRODO4, 0x00, SWKSMIO, 4, ATA_SA150, "Frodo4" },
4206 { ATA_FRODO8, 0x00, SWKSMIO, 8, ATA_SA150, "Frodo8" },
4207 { 0, 0, 0, 0, 0, 0}};
4208
4209 if (!(ctlr->chip = ata_match_chip(dev, ids)))
4210 return ENXIO;
4211
4212 ata_set_desc(dev);
4213 ctlr->chipinit = ata_serverworks_chipinit;
4214 return 0;
4215 }
4216
4217 static int
4218 ata_serverworks_chipinit(device_t dev)
4219 {
4220 struct ata_pci_controller *ctlr = device_get_softc(dev);
4221
4222 if (ata_setup_interrupt(dev))
4223 return ENXIO;
4224
4225 if (ctlr->chip->cfg1 == SWKSMIO) {
4226 ctlr->r_type2 = SYS_RES_MEMORY;
4227 ctlr->r_rid2 = PCIR_BAR(5);
4228 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4229 &ctlr->r_rid2, RF_ACTIVE)))
4230 return ENXIO;
4231
4232 ctlr->channels = ctlr->chip->cfg2;
4233 ctlr->allocate = ata_serverworks_allocate;
4234 ctlr->setmode = ata_sata_setmode;
4235 return 0;
4236 }
4237 else if (ctlr->chip->cfg1 == SWKS33) {
4238 device_t *children;
4239 int nchildren, i;
4240
4241 /* locate the ISA part in the southbridge and enable UDMA33 */
4242 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
4243 for (i = 0; i < nchildren; i++) {
4244 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
4245 pci_write_config(children[i], 0x64,
4246 (pci_read_config(children[i], 0x64, 4) &
4247 ~0x00002000) | 0x00004000, 4);
4248 break;
4249 }
4250 }
4251 free(children, M_TEMP);
4252 }
4253 }
4254 else {
4255 pci_write_config(dev, 0x5a,
4256 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
4257 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
4258 }
4259 ctlr->setmode = ata_serverworks_setmode;
4260 return 0;
4261 }
4262
4263 static int
4264 ata_serverworks_allocate(device_t dev)
4265 {
4266 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4267 struct ata_channel *ch = device_get_softc(dev);
4268 int ch_offset;
4269 int i;
4270
4271 ch_offset = ch->unit * 0x100;
4272
4273 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
4274 ch->r_io[i].res = ctlr->r_res2;
4275
4276 /* setup ATA registers */
4277 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
4278 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x04;
4279 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
4280 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
4281 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
4282 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
4283 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
4284 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1c;
4285 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x20;
4286 ata_default_registers(dev);
4287
4288 /* setup DMA registers */
4289 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x30;
4290 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x32;
4291 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x34;
4292
4293 /* setup SATA registers */
4294 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x40;
4295 ch->r_io[ATA_SERROR].offset = ch_offset + 0x44;
4296 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x48;
4297
4298 ch->flags |= ATA_NO_SLAVE;
4299 ata_pci_hw(dev);
4300 ch->hw.tf_read = ata_serverworks_tf_read;
4301 ch->hw.tf_write = ata_serverworks_tf_write;
4302
4303 /* chip does not reliably do 64K DMA transfers */
4304 if (ch->dma)
4305 ch->dma->max_iosize = 64 * DEV_BSIZE;
4306
4307 return 0;
4308 }
4309
4310 static void
4311 ata_serverworks_tf_read(struct ata_request *request)
4312 {
4313 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4314 struct ata_device *atadev = device_get_softc(request->dev);
4315
4316 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
4317 u_int16_t temp;
4318
4319 request->u.ata.count = ATA_IDX_INW(ch, ATA_COUNT);
4320 temp = ATA_IDX_INW(ch, ATA_SECTOR);
4321 request->u.ata.lba = (u_int64_t)(temp & 0x00ff) |
4322 ((u_int64_t)(temp & 0xff00) << 24);
4323 temp = ATA_IDX_INW(ch, ATA_CYL_LSB);
4324 request->u.ata.lba |= ((u_int64_t)(temp & 0x00ff) << 8) |
4325 ((u_int64_t)(temp & 0xff00) << 32);
4326 temp = ATA_IDX_INW(ch, ATA_CYL_MSB);
4327 request->u.ata.lba |= ((u_int64_t)(temp & 0x00ff) << 16) |
4328 ((u_int64_t)(temp & 0xff00) << 40);
4329 }
4330 else {
4331 request->u.ata.count = ATA_IDX_INW(ch, ATA_COUNT) & 0x00ff;
4332 request->u.ata.lba = (ATA_IDX_INW(ch, ATA_SECTOR) & 0x00ff) |
4333 ((ATA_IDX_INW(ch, ATA_CYL_LSB) & 0x00ff) << 8) |
4334 ((ATA_IDX_INW(ch, ATA_CYL_MSB) & 0x00ff) << 16) |
4335 ((ATA_IDX_INW(ch, ATA_DRIVE) & 0xf) << 24);
4336 }
4337 }
4338
4339 static void
4340 ata_serverworks_tf_write(struct ata_request *request)
4341 {
4342 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4343 struct ata_device *atadev = device_get_softc(request->dev);
4344
4345 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
4346 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
4347 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
4348 ATA_IDX_OUTW(ch, ATA_SECTOR, ((request->u.ata.lba >> 16) & 0xff00) |
4349 (request->u.ata.lba & 0x00ff));
4350 ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((request->u.ata.lba >> 24) & 0xff00) |
4351 ((request->u.ata.lba >> 8) & 0x00ff));
4352 ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((request->u.ata.lba >> 32) & 0xff00) |
4353 ((request->u.ata.lba >> 16) & 0x00ff));
4354 ATA_IDX_OUTW(ch, ATA_DRIVE, ATA_D_LBA | atadev->unit);
4355 }
4356 else {
4357 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
4358 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
4359 if (atadev->flags & ATA_D_USE_CHS) {
4360 int heads, sectors;
4361
4362 if (atadev->param.atavalid & ATA_FLAG_54_58) {
4363 heads = atadev->param.current_heads;
4364 sectors = atadev->param.current_sectors;
4365 }
4366 else {
4367 heads = atadev->param.heads;
4368 sectors = atadev->param.sectors;
4369 }
4370 ATA_IDX_OUTW(ch, ATA_SECTOR, (request->u.ata.lba % sectors)+1);
4371 ATA_IDX_OUTW(ch, ATA_CYL_LSB,
4372 (request->u.ata.lba / (sectors * heads)));
4373 ATA_IDX_OUTW(ch, ATA_CYL_MSB,
4374 (request->u.ata.lba / (sectors * heads)) >> 8);
4375 ATA_IDX_OUTW(ch, ATA_DRIVE, ATA_D_IBM | atadev->unit |
4376 (((request->u.ata.lba% (sectors * heads)) /
4377 sectors) & 0xf));
4378 }
4379 else {
4380 ATA_IDX_OUTW(ch, ATA_SECTOR, request->u.ata.lba);
4381 ATA_IDX_OUTW(ch, ATA_CYL_LSB, request->u.ata.lba >> 8);
4382 ATA_IDX_OUTW(ch, ATA_CYL_MSB, request->u.ata.lba >> 16);
4383 ATA_IDX_OUTW(ch, ATA_DRIVE,
4384 ATA_D_IBM | ATA_D_LBA | atadev->unit |
4385 ((request->u.ata.lba >> 24) & 0x0f));
4386 }
4387 }
4388 }
4389
4390 static void
4391 ata_serverworks_setmode(device_t dev, int mode)
4392 {
4393 device_t gparent = GRANDPARENT(dev);
4394 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4395 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4396 struct ata_device *atadev = device_get_softc(dev);
4397 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4398 int offset = (devno ^ 0x01) << 3;
4399 int error;
4400 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
4401 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
4402 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
4403
4404 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4405
4406 mode = ata_check_80pin(dev, mode);
4407
4408 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4409
4410 if (bootverbose)
4411 device_printf(dev, "%ssetting %s on %s chip\n",
4412 (error) ? "FAILURE " : "",
4413 ata_mode2str(mode), ctlr->chip->text);
4414 if (!error) {
4415 if (mode >= ATA_UDMA0) {
4416 pci_write_config(gparent, 0x56,
4417 (pci_read_config(gparent, 0x56, 2) &
4418 ~(0xf << (devno << 2))) |
4419 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
4420 pci_write_config(gparent, 0x54,
4421 pci_read_config(gparent, 0x54, 1) |
4422 (0x01 << devno), 1);
4423 pci_write_config(gparent, 0x44,
4424 (pci_read_config(gparent, 0x44, 4) &
4425 ~(0xff << offset)) |
4426 (dmatimings[2] << offset), 4);
4427 }
4428 else if (mode >= ATA_WDMA0) {
4429 pci_write_config(gparent, 0x54,
4430 pci_read_config(gparent, 0x54, 1) &
4431 ~(0x01 << devno), 1);
4432 pci_write_config(gparent, 0x44,
4433 (pci_read_config(gparent, 0x44, 4) &
4434 ~(0xff << offset)) |
4435 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
4436 }
4437 else
4438 pci_write_config(gparent, 0x54,
4439 pci_read_config(gparent, 0x54, 1) &
4440 ~(0x01 << devno), 1);
4441
4442 pci_write_config(gparent, 0x40,
4443 (pci_read_config(gparent, 0x40, 4) &
4444 ~(0xff << offset)) |
4445 (piotimings[ata_mode2idx(mode)] << offset), 4);
4446 atadev->mode = mode;
4447 }
4448 }
4449
4450
4451 /*
4452 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
4453 */
4454 int
4455 ata_sii_ident(device_t dev)
4456 {
4457 struct ata_pci_controller *ctlr = device_get_softc(dev);
4458 static struct ata_chip_id ids[] =
4459 {{ ATA_SII3114, 0x00, SIIMEMIO, SII4CH, ATA_SA150, "SiI 3114" },
4460 { ATA_SII3512, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3512" },
4461 { ATA_SII3112, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
4462 { ATA_SII3112_1, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
4463 { ATA_SII3512, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3512" },
4464 { ATA_SII3112, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
4465 { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
4466 { ATA_SII3124, 0x00, SIIPRBIO, SII4CH, ATA_SA300, "SiI 3124" },
4467 { ATA_SII3132, 0x00, SIIPRBIO, 0, ATA_SA300, "SiI 3132" },
4468 { ATA_SII0680, 0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
4469 { ATA_CMD649, 0x00, 0, SIIINTR, ATA_UDMA5, "CMD 649" },
4470 { ATA_CMD648, 0x00, 0, SIIINTR, ATA_UDMA4, "CMD 648" },
4471 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "CMD 646U2" },
4472 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "CMD 646" },
4473 { 0, 0, 0, 0, 0, 0}};
4474
4475 if (!(ctlr->chip = ata_match_chip(dev, ids)))
4476 return ENXIO;
4477
4478 ata_set_desc(dev);
4479 ctlr->chipinit = ata_sii_chipinit;
4480 return 0;
4481 }
4482
4483 static int
4484 ata_sii_chipinit(device_t dev)
4485 {
4486 struct ata_pci_controller *ctlr = device_get_softc(dev);
4487
4488 if (ata_setup_interrupt(dev))
4489 return ENXIO;
4490
4491 switch (ctlr->chip->cfg1) {
4492 case SIIPRBIO:
4493 ctlr->r_type1 = SYS_RES_MEMORY;
4494 ctlr->r_rid1 = PCIR_BAR(0);
4495 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
4496 &ctlr->r_rid1, RF_ACTIVE)))
4497 return ENXIO;
4498
4499 ctlr->r_rid2 = PCIR_BAR(2);
4500 ctlr->r_type2 = SYS_RES_MEMORY;
4501 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4502 &ctlr->r_rid2, RF_ACTIVE))){
4503 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
4504 return ENXIO;
4505 }
4506 ctlr->allocate = ata_siiprb_allocate;
4507 ctlr->reset = ata_siiprb_reset;
4508 ctlr->dmainit = ata_siiprb_dmainit;
4509 ctlr->setmode = ata_sata_setmode;
4510 ctlr->channels = (ctlr->chip->cfg2 == SII4CH) ? 4 : 2;
4511
4512 /* reset controller */
4513 ATA_OUTL(ctlr->r_res1, 0x0040, 0x80000000);
4514 DELAY(10000);
4515 ATA_OUTL(ctlr->r_res1, 0x0040, 0x0000000f);
4516
4517 /* enable PCI interrupt */
4518 pci_write_config(dev, PCIR_COMMAND,
4519 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
4520 break;
4521
4522 case SIIMEMIO:
4523 ctlr->r_type2 = SYS_RES_MEMORY;
4524 ctlr->r_rid2 = PCIR_BAR(5);
4525 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4526 &ctlr->r_rid2, RF_ACTIVE))) {
4527 if (ctlr->chip->chipid != ATA_SII0680 ||
4528 (pci_read_config(dev, 0x8a, 1) & 1))
4529 return ENXIO;
4530 }
4531
4532 if (ctlr->chip->cfg2 & SIISETCLK) {
4533 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
4534 pci_write_config(dev, 0x8a,
4535 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
4536 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
4537 device_printf(dev, "%s could not set ATA133 clock\n",
4538 ctlr->chip->text);
4539 }
4540
4541 /* if we have 4 channels enable the second set */
4542 if (ctlr->chip->cfg2 & SII4CH) {
4543 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
4544 ctlr->channels = 4;
4545 }
4546
4547 /* dont block interrupts from any channel */
4548 pci_write_config(dev, 0x48,
4549 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
4550
4551 /* enable PCI interrupt as BIOS might not */
4552 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
4553
4554 if (ctlr->r_res2)
4555 ctlr->allocate = ata_sii_allocate;
4556
4557 if (ctlr->chip->max_dma >= ATA_SA150) {
4558 ctlr->reset = ata_sii_reset;
4559 ctlr->setmode = ata_sata_setmode;
4560 }
4561 else
4562 ctlr->setmode = ata_sii_setmode;
4563 break;
4564
4565 default:
4566 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
4567 device_printf(dev, "HW has secondary channel disabled\n");
4568 ctlr->channels = 1;
4569 }
4570
4571 /* enable interrupt as BIOS might not */
4572 pci_write_config(dev, 0x71, 0x01, 1);
4573
4574 ctlr->allocate = ata_cmd_allocate;
4575 ctlr->setmode = ata_cmd_setmode;
4576 break;
4577 }
4578 return 0;
4579 }
4580
4581 static int
4582 ata_cmd_allocate(device_t dev)
4583 {
4584 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4585 struct ata_channel *ch = device_get_softc(dev);
4586
4587 /* setup the usual register normal pci style */
4588 if (ata_pci_allocate(dev))
4589 return ENXIO;
4590
4591 if (ctlr->chip->cfg2 & SIIINTR)
4592 ch->hw.status = ata_cmd_status;
4593
4594 return 0;
4595 }
4596
4597 static int
4598 ata_cmd_status(device_t dev)
4599 {
4600 struct ata_channel *ch = device_get_softc(dev);
4601 u_int8_t reg71;
4602
4603 if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
4604 (ch->unit ? 0x08 : 0x04))) {
4605 pci_write_config(device_get_parent(ch->dev), 0x71,
4606 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
4607 return ata_pci_status(dev);
4608 }
4609 return 0;
4610 }
4611
4612 static void
4613 ata_cmd_setmode(device_t dev, int mode)
4614 {
4615 device_t gparent = GRANDPARENT(dev);
4616 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4617 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4618 struct ata_device *atadev = device_get_softc(dev);
4619 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4620 int error;
4621
4622 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4623
4624 mode = ata_check_80pin(dev, mode);
4625
4626 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4627
4628 if (bootverbose)
4629 device_printf(dev, "%ssetting %s on %s chip\n",
4630 (error) ? "FAILURE " : "",
4631 ata_mode2str(mode), ctlr->chip->text);
4632 if (!error) {
4633 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
4634 int ureg = ch->unit ? 0x7b : 0x73;
4635
4636 if (mode >= ATA_UDMA0) {
4637 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
4638 { 0x11, 0x42 }, { 0x25, 0x8a },
4639 { 0x15, 0x4a }, { 0x05, 0x0a } };
4640
4641 u_int8_t umode = pci_read_config(gparent, ureg, 1);
4642
4643 umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
4644 umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
4645 pci_write_config(gparent, ureg, umode, 1);
4646 }
4647 else if (mode >= ATA_WDMA0) {
4648 int dmatimings[] = { 0x87, 0x32, 0x3f };
4649
4650 pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
4651 pci_write_config(gparent, ureg,
4652 pci_read_config(gparent, ureg, 1) &
4653 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
4654 }
4655 else {
4656 int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
4657 pci_write_config(gparent, treg,
4658 piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
4659 pci_write_config(gparent, ureg,
4660 pci_read_config(gparent, ureg, 1) &
4661 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
4662 }
4663 atadev->mode = mode;
4664 }
4665 }
4666
4667 static int
4668 ata_sii_allocate(device_t dev)
4669 {
4670 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4671 struct ata_channel *ch = device_get_softc(dev);
4672 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
4673 int i;
4674
4675 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
4676 ch->r_io[i].res = ctlr->r_res2;
4677 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
4678 }
4679 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
4680 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
4681 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
4682 ata_default_registers(dev);
4683
4684 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
4685 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
4686 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
4687 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
4688 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
4689 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
4690
4691 if (ctlr->chip->max_dma >= ATA_SA150) {
4692 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4693 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
4694 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4695 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
4696 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4697 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
4698 ch->flags |= ATA_NO_SLAVE;
4699
4700 /* enable PHY state change interrupt */
4701 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
4702 }
4703
4704 if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
4705 /* work around errata in early chips */
4706 ch->dma->boundary = 8192;
4707 ch->dma->segsize = 15 * DEV_BSIZE;
4708 }
4709
4710 ata_pci_hw(dev);
4711 ch->hw.status = ata_sii_status;
4712 return 0;
4713 }
4714
4715 static int
4716 ata_sii_status(device_t dev)
4717 {
4718 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4719 struct ata_channel *ch = device_get_softc(dev);
4720 int offset0 = ((ch->unit & 1) << 3) + ((ch->unit & 2) << 8);
4721 int offset1 = ((ch->unit & 1) << 6) + ((ch->unit & 2) << 8);
4722
4723 /* do we have any PHY events ? */
4724 if (ctlr->chip->max_dma >= ATA_SA150 &&
4725 (ATA_INL(ctlr->r_res2, 0x10 + offset0) & 0x00000010))
4726 ata_sata_phy_check_events(dev);
4727
4728 if (ATA_INL(ctlr->r_res2, 0xa0 + offset1) & 0x00000800)
4729 return ata_pci_status(dev);
4730 else
4731 return 0;
4732 }
4733
4734 static void
4735 ata_sii_reset(device_t dev)
4736 {
4737 if (ata_sata_phy_reset(dev))
4738 ata_generic_reset(dev);
4739 }
4740
4741 static void
4742 ata_sii_setmode(device_t dev, int mode)
4743 {
4744 device_t gparent = GRANDPARENT(dev);
4745 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4746 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4747 struct ata_device *atadev = device_get_softc(dev);
4748 int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
4749 int mreg = ch->unit ? 0x84 : 0x80;
4750 int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
4751 int mval = pci_read_config(gparent, mreg, 1) & ~mask;
4752 int error;
4753
4754 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4755
4756 if (ctlr->chip->cfg2 & SIISETCLK) {
4757 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
4758 (ch->unit ? 0x02 : 0x01))) {
4759 ata_print_cable(dev, "controller");
4760 mode = ATA_UDMA2;
4761 }
4762 }
4763 else
4764 mode = ata_check_80pin(dev, mode);
4765
4766 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4767
4768 if (bootverbose)
4769 device_printf(dev, "%ssetting %s on %s chip\n",
4770 (error) ? "FAILURE " : "",
4771 ata_mode2str(mode), ctlr->chip->text);
4772 if (error)
4773 return;
4774
4775 if (mode >= ATA_UDMA0) {
4776 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
4777 u_int8_t ureg = 0xac + rego;
4778
4779 pci_write_config(gparent, mreg,
4780 mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
4781 pci_write_config(gparent, ureg,
4782 (pci_read_config(gparent, ureg, 1) & ~0x3f) |
4783 udmatimings[mode & ATA_MODE_MASK], 1);
4784
4785 }
4786 else if (mode >= ATA_WDMA0) {
4787 u_int8_t dreg = 0xa8 + rego;
4788 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
4789
4790 pci_write_config(gparent, mreg,
4791 mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
4792 pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
4793
4794 }
4795 else {
4796 u_int8_t preg = 0xa4 + rego;
4797 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
4798
4799 pci_write_config(gparent, mreg,
4800 mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
4801 pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
4802 }
4803 atadev->mode = mode;
4804 }
4805
4806
4807 struct ata_siiprb_dma_prdentry {
4808 u_int64_t addr;
4809 u_int32_t count;
4810 u_int32_t control;
4811 } __packed;
4812
4813 struct ata_siiprb_ata_command {
4814 struct ata_siiprb_dma_prdentry prd[126];
4815 } __packed;
4816
4817 struct ata_siiprb_atapi_command {
4818 u_int8_t ccb[16];
4819 struct ata_siiprb_dma_prdentry prd[125];
4820 } __packed;
4821
4822 struct ata_siiprb_command {
4823 u_int16_t control;
4824 u_int16_t protocol_override;
4825 u_int32_t transfer_count;
4826 u_int8_t fis[24];
4827 union {
4828 struct ata_siiprb_ata_command ata;
4829 struct ata_siiprb_atapi_command atapi;
4830 } u;
4831 } __packed;
4832
4833 static int
4834 ata_siiprb_allocate(device_t dev)
4835 {
4836 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4837 struct ata_channel *ch = device_get_softc(dev);
4838 int offset = ch->unit * 0x2000;
4839
4840 /* set the SATA resources */
4841 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4842 ch->r_io[ATA_SSTATUS].offset = 0x1f04 + offset;
4843 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4844 ch->r_io[ATA_SERROR].offset = 0x1f08 + offset;
4845 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4846 ch->r_io[ATA_SCONTROL].offset = 0x1f00 + offset;
4847 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
4848 ch->r_io[ATA_SACTIVE].offset = 0x1f0c + offset;
4849
4850 ch->hw.begin_transaction = ata_siiprb_begin_transaction;
4851 ch->hw.end_transaction = ata_siiprb_end_transaction;
4852 ch->hw.status = ata_siiprb_status;
4853 ch->hw.command = NULL; /* not used here */
4854 return 0;
4855 }
4856
4857 static int
4858 ata_siiprb_status(device_t dev)
4859 {
4860 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4861 struct ata_channel *ch = device_get_softc(dev);
4862 u_int32_t action = ATA_INL(ctlr->r_res1, 0x0044);
4863 int offset = ch->unit * 0x2000;
4864
4865 if (action & (1 << ch->unit)) {
4866 u_int32_t istatus = ATA_INL(ctlr->r_res2, 0x1008 + offset);
4867
4868 /* do we have any PHY events ? */
4869 ata_sata_phy_check_events(dev);
4870
4871 /* clear interrupt(s) */
4872 ATA_OUTL(ctlr->r_res2, 0x1008 + offset, istatus);
4873
4874 /* do we have any device action ? */
4875 return (istatus & 0x00000003);
4876 }
4877 return 0;
4878 }
4879
4880 static int
4881 ata_siiprb_begin_transaction(struct ata_request *request)
4882 {
4883 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
4884 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4885 struct ata_siiprb_command *prb;
4886 struct ata_siiprb_dma_prdentry *prd;
4887 int offset = ch->unit * 0x2000;
4888 u_int64_t prb_bus;
4889 int tag = 0, dummy;
4890
4891 /* SOS XXX */
4892 if (request->u.ata.command == ATA_DEVICE_RESET) {
4893 request->result = 0;
4894 return ATA_OP_FINISHED;
4895 }
4896
4897 /* check for 48 bit access and convert if needed */
4898 ata_modify_if_48bit(request);
4899
4900 /* get a piece of the workspace for this request */
4901 prb = (struct ata_siiprb_command *)
4902 (ch->dma->work + (sizeof(struct ata_siiprb_command) * tag));
4903
4904 /* set basic prd options ata/atapi etc etc */
4905 bzero(prb, sizeof(struct ata_siiprb_command));
4906
4907 /* setup the FIS for this request */
4908 if (!ata_request2fis_h2d(request, &prb->fis[0])) {
4909 device_printf(request->dev, "setting up SATA FIS failed\n");
4910 request->result = EIO;
4911 return ATA_OP_FINISHED;
4912 }
4913
4914 /* setup transfer type */
4915 if (request->flags & ATA_R_ATAPI) {
4916 struct ata_device *atadev = device_get_softc(request->dev);
4917
4918 bcopy(request->u.atapi.ccb, prb->u.atapi.ccb, 16);
4919 if ((atadev->param.config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12)
4920 ATA_OUTL(ctlr->r_res2, 0x1004 + offset, 0x00000020);
4921 else
4922 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000020);
4923 if (request->flags & ATA_R_READ)
4924 prb->control = htole16(0x0010);
4925 if (request->flags & ATA_R_WRITE)
4926 prb->control = htole16(0x0020);
4927 prd = &prb->u.atapi.prd[0];
4928 }
4929 else
4930 prd = &prb->u.ata.prd[0];
4931
4932 /* if request moves data setup and load SG list */
4933 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
4934 if (ch->dma->load(ch->dev, request->data, request->bytecount,
4935 request->flags & ATA_R_READ, prd, &dummy)) {
4936 device_printf(request->dev, "setting up DMA failed\n");
4937 request->result = EIO;
4938 return ATA_OP_FINISHED;
4939 }
4940 }
4941
4942 /* activate the prb */
4943 prb_bus = ch->dma->work_bus + (sizeof(struct ata_siiprb_command) * tag);
4944 ATA_OUTL(ctlr->r_res2,
4945 0x1c00 + offset + (tag * sizeof(u_int64_t)), prb_bus);
4946 ATA_OUTL(ctlr->r_res2,
4947 0x1c04 + offset + (tag * sizeof(u_int64_t)), prb_bus>>32);
4948
4949 /* start the timeout */
4950 callout_reset(&request->callout, request->timeout * hz,
4951 (timeout_t*)ata_timeout, request);
4952 return ATA_OP_CONTINUES;
4953 }
4954
4955 static int
4956 ata_siiprb_end_transaction(struct ata_request *request)
4957 {
4958 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
4959 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4960 struct ata_siiprb_command *prb;
4961 int offset = ch->unit * 0x2000;
4962 int error, timeout, tag = 0;
4963
4964 /* kill the timeout */
4965 callout_stop(&request->callout);
4966
4967 prb = (struct ata_siiprb_command *)
4968 ((u_int8_t *)rman_get_virtual(ctlr->r_res2) + (tag << 7) + offset);
4969
4970 /* any controller errors flagged ? */
4971 if ((error = ATA_INL(ctlr->r_res2, 0x1024 + offset))) {
4972 if (bootverbose)
4973 printf("ata_siiprb_end_transaction %s error=%08x\n",
4974 ata_cmd2str(request), error);
4975
4976 /* if device error status get details */
4977 if (error == 1 || error == 2) {
4978 request->status = prb->fis[2];
4979 if (request->status & ATA_S_ERROR)
4980 request->error = prb->fis[3];
4981 }
4982
4983 /* SOS XXX handle other controller errors here */
4984
4985 /* initialize port */
4986 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000004);
4987
4988 /* poll for port ready */
4989 for (timeout = 0; timeout < 1000; timeout++) {
4990 DELAY(1000);
4991 if (ATA_INL(ctlr->r_res2, 0x1008 + offset) & 0x00040000)
4992 break;
4993 }
4994 if (bootverbose) {
4995 if (timeout >= 1000)
4996 device_printf(ch->dev, "port initialize timeout\n");
4997 else
4998 device_printf(ch->dev, "port initialize time=%dms\n", timeout);
4999 }
5000 }
5001
5002 /* update progress */
5003 if (!(request->status & ATA_S_ERROR) && !(request->flags & ATA_R_TIMEOUT)) {
5004 if (request->flags & ATA_R_READ)
5005 request->donecount = prb->transfer_count;
5006 else
5007 request->donecount = request->bytecount;
5008 }
5009
5010 /* release SG list etc */
5011 ch->dma->unload(ch->dev);
5012
5013 return ATA_OP_FINISHED;
5014 }
5015
5016 static void
5017 ata_siiprb_reset(device_t dev)
5018 {
5019 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5020 struct ata_channel *ch = device_get_softc(dev);
5021 int offset = ch->unit * 0x2000;
5022 struct ata_siiprb_command *prb;
5023 u_int64_t prb_bus;
5024 u_int32_t status, signature;
5025 int timeout, tag = 0;
5026
5027 /* reset channel HW */
5028 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000001);
5029 DELAY(1000);
5030 ATA_OUTL(ctlr->r_res2, 0x1004 + offset, 0x00000001);
5031 DELAY(10000);
5032
5033 /* poll for channel ready */
5034 for (timeout = 0; timeout < 1000; timeout++) {
5035 if ((status = ATA_INL(ctlr->r_res2, 0x1008 + offset)) & 0x00040000)
5036 break;
5037 DELAY(1000);
5038 }
5039
5040 if (bootverbose) {
5041 if (timeout >= 1000)
5042 device_printf(ch->dev, "channel HW reset timeout\n");
5043 else
5044 device_printf(ch->dev, "channel HW reset time=%dms\n", timeout);
5045 }
5046
5047 /* reset phy */
5048 if (!ata_sata_phy_reset(dev)) {
5049 if (bootverbose)
5050 device_printf(ch->dev, "phy reset found no device\n");
5051 ch->devices = 0;
5052 goto finish;
5053 }
5054
5055 /* get a piece of the workspace for a soft reset request */
5056 prb = (struct ata_siiprb_command *)
5057 (ch->dma->work + (sizeof(struct ata_siiprb_command) * tag));
5058 bzero(prb, sizeof(struct ata_siiprb_command));
5059 prb->control = htole16(0x0080);
5060
5061 /* activate the soft reset prb */
5062 prb_bus = ch->dma->work_bus + (sizeof(struct ata_siiprb_command) * tag);
5063 ATA_OUTL(ctlr->r_res2,
5064 0x1c00 + offset + (tag * sizeof(u_int64_t)), prb_bus);
5065 ATA_OUTL(ctlr->r_res2,
5066 0x1c04 + offset + (tag * sizeof(u_int64_t)), prb_bus>>32);
5067
5068 /* poll for command finished */
5069 for (timeout = 0; timeout < 10000; timeout++) {
5070 DELAY(1000);
5071 if ((status = ATA_INL(ctlr->r_res2, 0x1008 + offset)) & 0x00010000)
5072 break;
5073 }
5074 if (timeout >= 1000) {
5075 device_printf(ch->dev, "reset timeout - no device found\n");
5076 ch->devices = 0;
5077 goto finish;
5078 }
5079 if (bootverbose)
5080 device_printf(ch->dev, "soft reset exec time=%dms status=%08x\n",
5081 timeout, status);
5082
5083 /* find out whats there */
5084 prb = (struct ata_siiprb_command *)
5085 ((u_int8_t *)rman_get_virtual(ctlr->r_res2) + (tag << 7) + offset);
5086 signature =
5087 prb->fis[12]|(prb->fis[4]<<8)|(prb->fis[5]<<16)|(prb->fis[6]<<24);
5088 if (bootverbose)
5089 device_printf(ch->dev, "SIGNATURE=%08x\n", signature);
5090 switch (signature) {
5091 case 0x00000101:
5092 ch->devices = ATA_ATA_MASTER;
5093 break;
5094 case 0x96690101:
5095 ch->devices = ATA_PORTMULTIPLIER;
5096 device_printf(ch->dev, "Portmultipliers not supported yet\n");
5097 ch->devices = 0;
5098 break;
5099 case 0xeb140101:
5100 ch->devices = ATA_ATAPI_MASTER;
5101 break;
5102 default:
5103 ch->devices = 0;
5104 }
5105 if (bootverbose)
5106 device_printf(dev, "siiprb_reset devices=0x%b\n", ch->devices,
5107 "\2\4ATAPI_SLAVE\3ATAPI_MASTER\2ATA_SLAVE\1ATA_MASTER");
5108
5109 finish:
5110 /* clear interrupt(s) */
5111 ATA_OUTL(ctlr->r_res2, 0x1008 + offset, 0x000008ff);
5112
5113 /* require explicit interrupt ack */
5114 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000008);
5115
5116 /* 64bit mode */
5117 ATA_OUTL(ctlr->r_res2, 0x1004 + offset, 0x00000400);
5118
5119 /* enable interrupts wanted */
5120 ATA_OUTL(ctlr->r_res2, 0x1010 + offset, 0x000000ff);
5121 }
5122
5123 static void
5124 ata_siiprb_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
5125 {
5126 struct ata_dmasetprd_args *args = xsc;
5127 struct ata_siiprb_dma_prdentry *prd = args->dmatab;
5128 int i;
5129
5130 if ((args->error = error))
5131 return;
5132
5133 for (i = 0; i < nsegs; i++) {
5134 prd[i].addr = htole64(segs[i].ds_addr);
5135 prd[i].count = htole32(segs[i].ds_len);
5136 }
5137 prd[i - 1].control = htole32(ATA_DMA_EOT);
5138 KASSERT(nsegs <= ATA_DMA_ENTRIES, ("too many DMA segment entries\n"));
5139 args->nsegs = nsegs;
5140 }
5141
5142 static void
5143 ata_siiprb_dmainit(device_t dev)
5144 {
5145 struct ata_channel *ch = device_get_softc(dev);
5146
5147 ata_dmainit(dev);
5148 if (ch->dma) {
5149 /* note start and stop are not used here */
5150 ch->dma->setprd = ata_siiprb_dmasetprd;
5151 ch->dma->max_address = BUS_SPACE_MAXADDR;
5152 }
5153 }
5154
5155
5156 /*
5157 * Silicon Integrated Systems Corp. (SiS) chipset support functions
5158 */
5159 int
5160 ata_sis_ident(device_t dev)
5161 {
5162 struct ata_pci_controller *ctlr = device_get_softc(dev);
5163 struct ata_chip_id *idx;
5164 static struct ata_chip_id ids[] =
5165 {{ ATA_SIS182, 0x00, SISSATA, 0, ATA_SA150, "182" }, /* south */
5166 { ATA_SIS181, 0x00, SISSATA, 0, ATA_SA150, "181" }, /* south */
5167 { ATA_SIS180, 0x00, SISSATA, 0, ATA_SA150, "180" }, /* south */
5168 { ATA_SIS965, 0x00, SIS133NEW, 0, ATA_UDMA6, "965" }, /* south */
5169 { ATA_SIS964, 0x00, SIS133NEW, 0, ATA_UDMA6, "964" }, /* south */
5170 { ATA_SIS963, 0x00, SIS133NEW, 0, ATA_UDMA6, "963" }, /* south */
5171 { ATA_SIS962, 0x00, SIS133NEW, 0, ATA_UDMA6, "962" }, /* south */
5172
5173 { ATA_SIS745, 0x00, SIS100NEW, 0, ATA_UDMA5, "745" }, /* 1chip */
5174 { ATA_SIS735, 0x00, SIS100NEW, 0, ATA_UDMA5, "735" }, /* 1chip */
5175 { ATA_SIS733, 0x00, SIS100NEW, 0, ATA_UDMA5, "733" }, /* 1chip */
5176 { ATA_SIS730, 0x00, SIS100OLD, 0, ATA_UDMA5, "730" }, /* 1chip */
5177
5178 { ATA_SIS635, 0x00, SIS100NEW, 0, ATA_UDMA5, "635" }, /* 1chip */
5179 { ATA_SIS633, 0x00, SIS100NEW, 0, ATA_UDMA5, "633" }, /* unknown */
5180 { ATA_SIS630, 0x30, SIS100OLD, 0, ATA_UDMA5, "630S"}, /* 1chip */
5181 { ATA_SIS630, 0x00, SIS66, 0, ATA_UDMA4, "630" }, /* 1chip */
5182 { ATA_SIS620, 0x00, SIS66, 0, ATA_UDMA4, "620" }, /* 1chip */
5183
5184 { ATA_SIS550, 0x00, SIS66, 0, ATA_UDMA5, "550" },
5185 { ATA_SIS540, 0x00, SIS66, 0, ATA_UDMA4, "540" },
5186 { ATA_SIS530, 0x00, SIS66, 0, ATA_UDMA4, "530" },
5187
5188 { ATA_SIS5513, 0xc2, SIS33, 1, ATA_UDMA2, "5513" },
5189 { ATA_SIS5513, 0x00, SIS33, 1, ATA_WDMA2, "5513" },
5190 { 0, 0, 0, 0, 0, 0 }};
5191 char buffer[64];
5192 int found = 0;
5193
5194 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
5195 return ENXIO;
5196
5197 if (idx->cfg2 && !found) {
5198 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
5199
5200 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
5201 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
5202 found = 1;
5203 idx->cfg1 = SIS133NEW;
5204 idx->max_dma = ATA_UDMA6;
5205 sprintf(buffer, "SiS 962/963 %s controller",
5206 ata_mode2str(idx->max_dma));
5207 }
5208 pci_write_config(dev, 0x57, reg57, 1);
5209 }
5210 if (idx->cfg2 && !found) {
5211 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
5212
5213 pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
5214 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
5215 struct ata_chip_id id[] =
5216 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
5217
5218 found = 1;
5219 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
5220 idx->cfg1 = SIS133OLD;
5221 idx->max_dma = ATA_UDMA6;
5222 }
5223 else {
5224 idx->cfg1 = SIS100NEW;
5225 idx->max_dma = ATA_UDMA5;
5226 }
5227 sprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
5228 }
5229 pci_write_config(dev, 0x4a, reg4a, 1);
5230 }
5231 if (!found)
5232 sprintf(buffer,"SiS %s %s controller",
5233 idx->text, ata_mode2str(idx->max_dma));
5234
5235 device_set_desc_copy(dev, buffer);
5236 ctlr->chip = idx;
5237 ctlr->chipinit = ata_sis_chipinit;
5238 return 0;
5239 }
5240
5241 static int
5242 ata_sis_chipinit(device_t dev)
5243 {
5244 struct ata_pci_controller *ctlr = device_get_softc(dev);
5245
5246 if (ata_setup_interrupt(dev))
5247 return ENXIO;
5248
5249 switch (ctlr->chip->cfg1) {
5250 case SIS33:
5251 break;
5252 case SIS66:
5253 case SIS100OLD:
5254 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
5255 break;
5256 case SIS100NEW:
5257 case SIS133OLD:
5258 pci_write_config(dev, 0x49, |