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: src/sys/dev/ata/ata-chipset.c,v 1.126.2.31.2.2 2008/10/21 18:40:29 jhb Exp $");
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
2582 /* set legacy ATA resources */
2583 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2584 ch->r_io[i].res = ctlr->r_res1;
2585 ch->r_io[i].offset = 0x02100 + (i << 2) + ATA_MV_EDMA_BASE(ch);
2586 }
2587 ch->r_io[ATA_CONTROL].res = ctlr->r_res1;
2588 ch->r_io[ATA_CONTROL].offset = 0x02120 + ATA_MV_EDMA_BASE(ch);
2589 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res1;
2590 ata_default_registers(dev);
2591
2592 /* set SATA resources */
2593 switch (ctlr->chip->cfg2) {
2594 case MV50XX:
2595 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
2596 ch->r_io[ATA_SSTATUS].offset = 0x00100 + ATA_MV_HOST_BASE(ch);
2597 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
2598 ch->r_io[ATA_SERROR].offset = 0x00104 + ATA_MV_HOST_BASE(ch);
2599 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
2600 ch->r_io[ATA_SCONTROL].offset = 0x00108 + ATA_MV_HOST_BASE(ch);
2601 break;
2602 case MV60XX:
2603 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
2604 ch->r_io[ATA_SSTATUS].offset = 0x02300 + ATA_MV_EDMA_BASE(ch);
2605 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
2606 ch->r_io[ATA_SERROR].offset = 0x02304 + ATA_MV_EDMA_BASE(ch);
2607 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
2608 ch->r_io[ATA_SCONTROL].offset = 0x02308 + ATA_MV_EDMA_BASE(ch);
2609 ch->r_io[ATA_SACTIVE].res = ctlr->r_res1;
2610 ch->r_io[ATA_SACTIVE].offset = 0x02350 + ATA_MV_EDMA_BASE(ch);
2611 break;
2612 }
2613
2614 ch->flags |= ATA_NO_SLAVE;
2615 ch->flags |= ATA_USE_16BIT; /* XXX SOS needed ? */
2616 ata_generic_hw(dev);
2617 ch->hw.begin_transaction = ata_marvell_edma_begin_transaction;
2618 ch->hw.end_transaction = ata_marvell_edma_end_transaction;
2619 ch->hw.status = ata_marvell_edma_status;
2620
2621 /* disable the EDMA machinery */
2622 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2623 DELAY(100000); /* SOS should poll for disabled */
2624
2625 /* set configuration to non-queued 128b read transfers stop on error */
2626 ATA_OUTL(ctlr->r_res1, 0x02000 + ATA_MV_EDMA_BASE(ch), (1<<11) | (1<<13));
2627
2628 /* request queue base high */
2629 ATA_OUTL(ctlr->r_res1, 0x02010 + ATA_MV_EDMA_BASE(ch), work >> 32);
2630
2631 /* request queue in ptr */
2632 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
2633
2634 /* request queue out ptr */
2635 ATA_OUTL(ctlr->r_res1, 0x02018 + ATA_MV_EDMA_BASE(ch), 0x0);
2636
2637 /* response queue base high */
2638 work += 1024;
2639 ATA_OUTL(ctlr->r_res1, 0x0201c + ATA_MV_EDMA_BASE(ch), work >> 32);
2640
2641 /* response queue in ptr */
2642 ATA_OUTL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch), 0x0);
2643
2644 /* response queue out ptr */
2645 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
2646
2647 /* clear SATA error register */
2648 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2649
2650 /* clear any outstanding error interrupts */
2651 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2652
2653 /* unmask all error interrupts */
2654 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2655
2656 /* enable EDMA machinery */
2657 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2658 return 0;
2659 }
2660
2661 static int
2662 ata_marvell_edma_status(device_t dev)
2663 {
2664 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2665 struct ata_channel *ch = device_get_softc(dev);
2666 u_int32_t cause = ATA_INL(ctlr->r_res1, 0x01d60);
2667 int shift = (ch->unit << 1) + (ch->unit > 3);
2668
2669 if (cause & (1 << shift)) {
2670
2671 /* clear interrupt(s) */
2672 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2673
2674 /* do we have any PHY events ? */
2675 ata_sata_phy_check_events(dev);
2676 }
2677
2678 /* do we have any device action ? */
2679 return (cause & (2 << shift));
2680 }
2681
2682 /* must be called with ATA channel locked and state_mtx held */
2683 static int
2684 ata_marvell_edma_begin_transaction(struct ata_request *request)
2685 {
2686 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2687 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2688 u_int32_t req_in;
2689 u_int8_t *bytep;
2690 u_int16_t *wordp;
2691 u_int32_t *quadp;
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 wordp = (u_int16_t *)bytep;
2723 quadp = (u_int32_t *)bytep;
2724
2725 /* fill in this request */
2726 quadp[0] = (long)ch->dma->sg_bus & 0xffffffff;
2727 quadp[1] = (u_int64_t)ch->dma->sg_bus >> 32;
2728 wordp[4] = (request->flags & ATA_R_READ ? 0x01 : 0x00) | (tag<<1);
2729
2730 i = 10;
2731 bytep[i++] = (request->u.ata.count >> 8) & 0xff;
2732 bytep[i++] = 0x10 | ATA_COUNT;
2733 bytep[i++] = request->u.ata.count & 0xff;
2734 bytep[i++] = 0x10 | ATA_COUNT;
2735
2736 bytep[i++] = (request->u.ata.lba >> 24) & 0xff;
2737 bytep[i++] = 0x10 | ATA_SECTOR;
2738 bytep[i++] = request->u.ata.lba & 0xff;
2739 bytep[i++] = 0x10 | ATA_SECTOR;
2740
2741 bytep[i++] = (request->u.ata.lba >> 32) & 0xff;
2742 bytep[i++] = 0x10 | ATA_CYL_LSB;
2743 bytep[i++] = (request->u.ata.lba >> 8) & 0xff;
2744 bytep[i++] = 0x10 | ATA_CYL_LSB;
2745
2746 bytep[i++] = (request->u.ata.lba >> 40) & 0xff;
2747 bytep[i++] = 0x10 | ATA_CYL_MSB;
2748 bytep[i++] = (request->u.ata.lba >> 16) & 0xff;
2749 bytep[i++] = 0x10 | ATA_CYL_MSB;
2750
2751 bytep[i++] = ATA_D_LBA | ATA_D_IBM | ((request->u.ata.lba >> 24) & 0xf);
2752 bytep[i++] = 0x10 | ATA_DRIVE;
2753
2754 bytep[i++] = request->u.ata.command;
2755 bytep[i++] = 0x90 | ATA_COMMAND;
2756
2757 /* enable EDMA machinery if needed */
2758 if (!(ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)) {
2759 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2760 while (!(ATA_INL(ctlr->r_res1,
2761 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
2762 DELAY(10);
2763 }
2764
2765 /* tell EDMA it has a new request */
2766 slot = (((req_in & ~0xfffffc00) >> 5) + 1) & 0x1f;
2767 req_in &= 0xfffffc00;
2768 req_in += (slot << 5);
2769 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), req_in);
2770
2771 return ATA_OP_CONTINUES;
2772 }
2773
2774 /* must be called with ATA channel locked and state_mtx held */
2775 static int
2776 ata_marvell_edma_end_transaction(struct ata_request *request)
2777 {
2778 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2779 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2780 int offset = (ch->unit > 3 ? 0x30014 : 0x20014);
2781 u_int32_t icr = ATA_INL(ctlr->r_res1, offset);
2782 int res;
2783
2784 /* EDMA interrupt */
2785 if ((icr & (0x0001 << (ch->unit & 3)))) {
2786 struct ata_marvell_response *response;
2787 u_int32_t rsp_in, rsp_out;
2788 int slot;
2789
2790 /* stop timeout */
2791 callout_stop(&request->callout);
2792
2793 /* get response ptr's */
2794 rsp_in = ATA_INL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch));
2795 rsp_out = ATA_INL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch));
2796 slot = (((rsp_in & ~0xffffff00) >> 3)) & 0x1f;
2797 rsp_out &= 0xffffff00;
2798 rsp_out += (slot << 3);
2799 response = (struct ata_marvell_response *)
2800 (ch->dma->work + 1024 + (slot << 3));
2801
2802 /* record status for this request */
2803 request->status = response->dev_status;
2804 request->error = 0;
2805
2806 /* ack response */
2807 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), rsp_out);
2808
2809 /* update progress */
2810 if (!(request->status & ATA_S_ERROR) &&
2811 !(request->flags & ATA_R_TIMEOUT))
2812 request->donecount = request->bytecount;
2813
2814 /* unload SG list */
2815 ch->dma->unload(ch->dev);
2816
2817 res = ATA_OP_FINISHED;
2818 }
2819
2820 /* legacy ATA interrupt */
2821 else {
2822 res = ata_end_transaction(request);
2823 }
2824
2825 /* ack interrupt */
2826 ATA_OUTL(ctlr->r_res1, offset, ~(icr & (0x0101 << (ch->unit & 3))));
2827 return res;
2828 }
2829
2830 static void
2831 ata_marvell_edma_reset(device_t dev)
2832 {
2833 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2834 struct ata_channel *ch = device_get_softc(dev);
2835
2836 /* disable the EDMA machinery */
2837 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2838 while ((ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
2839 DELAY(10);
2840
2841 /* clear SATA error register */
2842 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2843
2844 /* clear any outstanding error interrupts */
2845 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2846
2847 /* unmask all error interrupts */
2848 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2849
2850 /* enable channel and test for devices */
2851 if (ata_sata_phy_reset(dev))
2852 ata_generic_reset(dev);
2853
2854 /* enable EDMA machinery */
2855 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2856 }
2857
2858 static void
2859 ata_marvell_edma_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs,
2860 int error)
2861 {
2862 struct ata_dmasetprd_args *args = xsc;
2863 struct ata_marvell_dma_prdentry *prd = args->dmatab;
2864 int i;
2865
2866 if ((args->error = error))
2867 return;
2868
2869 for (i = 0; i < nsegs; i++) {
2870 prd[i].addrlo = htole32(segs[i].ds_addr);
2871 prd[i].count = htole32(segs[i].ds_len);
2872 prd[i].addrhi = htole32((u_int64_t)segs[i].ds_addr >> 32);
2873 }
2874 prd[i - 1].count |= htole32(ATA_DMA_EOT);
2875 KASSERT(nsegs <= ATA_DMA_ENTRIES, ("too many DMA segment entries\n"));
2876 args->nsegs = nsegs;
2877 }
2878
2879 static void
2880 ata_marvell_edma_dmainit(device_t dev)
2881 {
2882 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2883 struct ata_channel *ch = device_get_softc(dev);
2884
2885 ata_dmainit(dev);
2886 if (ch->dma) {
2887 /* note start and stop are not used here */
2888 ch->dma->setprd = ata_marvell_edma_dmasetprd;
2889
2890 /* if 64bit support present adjust max address used */
2891 if (ATA_INL(ctlr->r_res1, 0x00d00) & 0x00000004)
2892 ch->dma->max_address = BUS_SPACE_MAXADDR;
2893
2894 /* chip does not reliably do 64K DMA transfers */
2895 ch->dma->max_iosize = 64 * DEV_BSIZE;
2896 }
2897 }
2898
2899
2900 /*
2901 * National chipset support functions
2902 */
2903 int
2904 ata_national_ident(device_t dev)
2905 {
2906 struct ata_pci_controller *ctlr = device_get_softc(dev);
2907
2908 /* this chip is a clone of the Cyrix chip, bugs and all */
2909 if (pci_get_devid(dev) == ATA_SC1100) {
2910 device_set_desc(dev, "National Geode SC1100 ATA33 controller");
2911 ctlr->chipinit = ata_national_chipinit;
2912 return 0;
2913 }
2914 return ENXIO;
2915 }
2916
2917 static int
2918 ata_national_chipinit(device_t dev)
2919 {
2920 struct ata_pci_controller *ctlr = device_get_softc(dev);
2921
2922 if (ata_setup_interrupt(dev))
2923 return ENXIO;
2924
2925 ctlr->setmode = ata_national_setmode;
2926 return 0;
2927 }
2928
2929 static void
2930 ata_national_setmode(device_t dev, int mode)
2931 {
2932 device_t gparent = GRANDPARENT(dev);
2933 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2934 struct ata_device *atadev = device_get_softc(dev);
2935 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2936 u_int32_t piotiming[] =
2937 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
2938 0x00803020, 0x20102010, 0x00100010,
2939 0x00100010, 0x00100010, 0x00100010 };
2940 u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
2941 u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
2942 int error;
2943
2944 ch->dma->alignment = 16;
2945 ch->dma->max_iosize = 64 * DEV_BSIZE;
2946
2947 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2948
2949 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2950
2951 if (bootverbose)
2952 device_printf(dev, "%s setting %s on National chip\n",
2953 (error) ? "failed" : "success", ata_mode2str(mode));
2954 if (!error) {
2955 if (mode >= ATA_UDMA0) {
2956 pci_write_config(gparent, 0x44 + (devno << 3),
2957 udmatiming[mode & ATA_MODE_MASK], 4);
2958 }
2959 else if (mode >= ATA_WDMA0) {
2960 pci_write_config(gparent, 0x44 + (devno << 3),
2961 dmatiming[mode & ATA_MODE_MASK], 4);
2962 }
2963 else {
2964 pci_write_config(gparent, 0x44 + (devno << 3),
2965 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
2966 0x80000000, 4);
2967 }
2968 pci_write_config(gparent, 0x40 + (devno << 3),
2969 piotiming[ata_mode2idx(mode)], 4);
2970 atadev->mode = mode;
2971 }
2972 }
2973
2974
2975 /*
2976 * NetCell chipset support functions
2977 */
2978 int
2979 ata_netcell_ident(device_t dev)
2980 {
2981 struct ata_pci_controller *ctlr = device_get_softc(dev);
2982
2983 if (pci_get_devid(dev) == ATA_NETCELL_SR) {
2984 device_set_desc(dev, "Netcell SyncRAID SR3000/5000 RAID Controller");
2985 ctlr->chipinit = ata_netcell_chipinit;
2986 return 0;
2987 }
2988 return ENXIO;
2989 }
2990
2991 static int
2992 ata_netcell_chipinit(device_t dev)
2993 {
2994 struct ata_pci_controller *ctlr = device_get_softc(dev);
2995
2996 if (ata_generic_chipinit(dev))
2997 return ENXIO;
2998
2999 ctlr->allocate = ata_netcell_allocate;
3000 return 0;
3001 }
3002
3003 static int
3004 ata_netcell_allocate(device_t dev)
3005 {
3006 struct ata_channel *ch = device_get_softc(dev);
3007
3008 /* setup the usual register normal pci style */
3009 if (ata_pci_allocate(dev))
3010 return ENXIO;
3011
3012 /* the NetCell only supports 16 bit PIO transfers */
3013 ch->flags |= ATA_USE_16BIT;
3014
3015 return 0;
3016 }
3017
3018
3019 /*
3020 * nVidia chipset support functions
3021 */
3022 int
3023 ata_nvidia_ident(device_t dev)
3024 {
3025 struct ata_pci_controller *ctlr = device_get_softc(dev);
3026 static struct ata_chip_id ids[] =
3027 {{ ATA_NFORCE1, 0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nForce" },
3028 { ATA_NFORCE2, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce2" },
3029 { ATA_NFORCE2_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce2 Pro" },
3030 { ATA_NFORCE2_PRO_S1, 0, 0, 0, ATA_SA150, "nForce2 Pro" },
3031 { ATA_NFORCE3, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce3" },
3032 { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce3 Pro" },
3033 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
3034 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
3035 { ATA_NFORCE_MCP04, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP" },
3036 { ATA_NFORCE_MCP04_S1, 0, 0, NV4, ATA_SA150, "nForce MCP" },
3037 { ATA_NFORCE_MCP04_S2, 0, 0, NV4, ATA_SA150, "nForce MCP" },
3038 { ATA_NFORCE_CK804, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce CK804" },
3039 { ATA_NFORCE_CK804_S1, 0, 0, NV4, ATA_SA300, "nForce CK804" },
3040 { ATA_NFORCE_CK804_S2, 0, 0, NV4, ATA_SA300, "nForce CK804" },
3041 { ATA_NFORCE_MCP51, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP51" },
3042 { ATA_NFORCE_MCP51_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP51" },
3043 { ATA_NFORCE_MCP51_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP51" },
3044 { ATA_NFORCE_MCP55, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP55" },
3045 { ATA_NFORCE_MCP55_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP55" },
3046 { ATA_NFORCE_MCP55_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP55" },
3047 { ATA_NFORCE_MCP61, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP61" },
3048 { ATA_NFORCE_MCP61_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP61" },
3049 { ATA_NFORCE_MCP61_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP61" },
3050 { ATA_NFORCE_MCP61_S3, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP61" },
3051 { ATA_NFORCE_MCP65, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP65" },
3052 { ATA_NFORCE_MCP67, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP67" },
3053 { ATA_NFORCE_MCP73, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP73" },
3054 { ATA_NFORCE_MCP77, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP77" },
3055 { 0, 0, 0, 0, 0, 0}} ;
3056
3057 if (!(ctlr->chip = ata_match_chip(dev, ids)))
3058 return ENXIO;
3059
3060 ata_set_desc(dev);
3061 ctlr->chipinit = ata_nvidia_chipinit;
3062 return 0;
3063 }
3064
3065 static int
3066 ata_nvidia_chipinit(device_t dev)
3067 {
3068 struct ata_pci_controller *ctlr = device_get_softc(dev);
3069
3070 if (ata_setup_interrupt(dev))
3071 return ENXIO;
3072
3073 if (ctlr->chip->max_dma >= ATA_SA150) {
3074 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
3075 ctlr->r_type2 = SYS_RES_IOPORT;
3076 else
3077 ctlr->r_type2 = SYS_RES_MEMORY;
3078 ctlr->r_rid2 = PCIR_BAR(5);
3079 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3080 &ctlr->r_rid2, RF_ACTIVE))) {
3081 int offset = ctlr->chip->cfg2 & NV4 ? 0x0440 : 0x0010;
3082
3083 ctlr->allocate = ata_nvidia_allocate;
3084 ctlr->reset = ata_nvidia_reset;
3085
3086 /* enable control access */
3087 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1);
3088
3089 if (ctlr->chip->cfg2 & NVQ) {
3090 /* clear interrupt status */
3091 ATA_OUTL(ctlr->r_res2, offset, 0x00ff00ff);
3092
3093 /* enable device and PHY state change interrupts */
3094 ATA_OUTL(ctlr->r_res2, offset + 4, 0x000d000d);
3095
3096 /* disable NCQ support */
3097 ATA_OUTL(ctlr->r_res2, 0x0400,
3098 ATA_INL(ctlr->r_res2, 0x0400) & 0xfffffff9);
3099 }
3100 else {
3101 /* clear interrupt status */
3102 ATA_OUTB(ctlr->r_res2, offset, 0xff);
3103
3104 /* enable device and PHY state change interrupts */
3105 ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd);
3106 }
3107
3108 /* enable PCI interrupt */
3109 pci_write_config(dev, PCIR_COMMAND,
3110 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3111
3112 }
3113 ctlr->setmode = ata_sata_setmode;
3114 }
3115 else {
3116 /* disable prefetch, postwrite */
3117 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
3118 ctlr->setmode = ata_via_family_setmode;
3119 }
3120 return 0;
3121 }
3122
3123 static int
3124 ata_nvidia_allocate(device_t dev)
3125 {
3126 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3127 struct ata_channel *ch = device_get_softc(dev);
3128
3129 /* setup the usual register normal pci style */
3130 if (ata_pci_allocate(dev))
3131 return ENXIO;
3132
3133 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3134 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
3135 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3136 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
3137 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3138 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
3139
3140 ch->hw.status = ata_nvidia_status;
3141 ch->flags |= ATA_NO_SLAVE;
3142
3143 return 0;
3144 }
3145
3146 static int
3147 ata_nvidia_status(device_t dev)
3148 {
3149 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3150 struct ata_channel *ch = device_get_softc(dev);
3151 int offset = ctlr->chip->cfg2 & NV4 ? 0x0440 : 0x0010;
3152 int shift = ch->unit << (ctlr->chip->cfg2 & NVQ ? 4 : 2);
3153 u_int32_t istatus = ATA_INL(ctlr->r_res2, offset);
3154
3155 /* do we have any PHY events ? */
3156 if (istatus & (0x0c << shift))
3157 ata_sata_phy_check_events(dev);
3158
3159 /* clear interrupt(s) */
3160 ATA_OUTB(ctlr->r_res2, offset,
3161 (0x0f << shift) | (ctlr->chip->cfg2 & NVQ ? 0x00f000f0 : 0));
3162
3163 /* do we have any device action ? */
3164 return (istatus & (0x01 << shift));
3165 }
3166
3167 static void
3168 ata_nvidia_reset(device_t dev)
3169 {
3170 if (ata_sata_phy_reset(dev))
3171 ata_generic_reset(dev);
3172 }
3173
3174
3175 /*
3176 * Promise chipset support functions
3177 */
3178 #define ATA_PDC_APKT_OFFSET 0x00000010
3179 #define ATA_PDC_HPKT_OFFSET 0x00000040
3180 #define ATA_PDC_ASG_OFFSET 0x00000080
3181 #define ATA_PDC_LSG_OFFSET 0x000000c0
3182 #define ATA_PDC_HSG_OFFSET 0x00000100
3183 #define ATA_PDC_CHN_OFFSET 0x00000400
3184 #define ATA_PDC_BUF_BASE 0x00400000
3185 #define ATA_PDC_BUF_OFFSET 0x00100000
3186 #define ATA_PDC_MAX_HPKT 8
3187 #define ATA_PDC_WRITE_REG 0x00
3188 #define ATA_PDC_WRITE_CTL 0x0e
3189 #define ATA_PDC_WRITE_END 0x08
3190 #define ATA_PDC_WAIT_NBUSY 0x10
3191 #define ATA_PDC_WAIT_READY 0x18
3192 #define ATA_PDC_1B 0x20
3193 #define ATA_PDC_2B 0x40
3194
3195 struct host_packet {
3196 u_int32_t addr;
3197 TAILQ_ENTRY(host_packet) chain;
3198 };
3199
3200 struct ata_promise_sx4 {
3201 struct mtx mtx;
3202 TAILQ_HEAD(, host_packet) queue;
3203 int busy;
3204 };
3205
3206 int
3207 ata_promise_ident(device_t dev)
3208 {
3209 struct ata_pci_controller *ctlr = device_get_softc(dev);
3210 struct ata_chip_id *idx;
3211 static struct ata_chip_id ids[] =
3212 {{ ATA_PDC20246, 0, PROLD, 0x00, ATA_UDMA2, "PDC20246" },
3213 { ATA_PDC20262, 0, PRNEW, 0x00, ATA_UDMA4, "PDC20262" },
3214 { ATA_PDC20263, 0, PRNEW, 0x00, ATA_UDMA4, "PDC20263" },
3215 { ATA_PDC20265, 0, PRNEW, 0x00, ATA_UDMA5, "PDC20265" },
3216 { ATA_PDC20267, 0, PRNEW, 0x00, ATA_UDMA5, "PDC20267" },
3217 { ATA_PDC20268, 0, PRTX, PRTX4, ATA_UDMA5, "PDC20268" },
3218 { ATA_PDC20269, 0, PRTX, 0x00, ATA_UDMA6, "PDC20269" },
3219 { ATA_PDC20270, 0, PRTX, PRTX4, ATA_UDMA5, "PDC20270" },
3220 { ATA_PDC20271, 0, PRTX, 0x00, ATA_UDMA6, "PDC20271" },
3221 { ATA_PDC20275, 0, PRTX, 0x00, ATA_UDMA6, "PDC20275" },
3222 { ATA_PDC20276, 0, PRTX, PRSX6K, ATA_UDMA6, "PDC20276" },
3223 { ATA_PDC20277, 0, PRTX, 0x00, ATA_UDMA6, "PDC20277" },
3224 { ATA_PDC20318, 0, PRMIO, PRSATA, ATA_SA150, "PDC20318" },
3225 { ATA_PDC20319, 0, PRMIO, PRSATA, ATA_SA150, "PDC20319" },
3226 { ATA_PDC20371, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20371" },
3227 { ATA_PDC20375, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20375" },
3228 { ATA_PDC20376, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20376" },
3229 { ATA_PDC20377, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20377" },
3230 { ATA_PDC20378, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20378" },
3231 { ATA_PDC20379, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20379" },
3232 { ATA_PDC20571, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20571" },
3233 { ATA_PDC20575, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20575" },
3234 { ATA_PDC20579, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20579" },
3235 { ATA_PDC20771, 0, PRMIO, PRCMBO2, ATA_SA300, "PDC20771" },
3236 { ATA_PDC40775, 0, PRMIO, PRCMBO2, ATA_SA300, "PDC40775" },
3237 { ATA_PDC20617, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20617" },
3238 { ATA_PDC20618, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20618" },
3239 { ATA_PDC20619, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20619" },
3240 { ATA_PDC20620, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20620" },
3241 { ATA_PDC20621, 0, PRMIO, PRSX4X, ATA_UDMA5, "PDC20621" },
3242 { ATA_PDC20622, 0, PRMIO, PRSX4X, ATA_SA150, "PDC20622" },
3243 { ATA_PDC40518, 0, PRMIO, PRSATA2, ATA_SA150, "PDC40518" },
3244 { ATA_PDC40519, 0, PRMIO, PRSATA2, ATA_SA150, "PDC40519" },
3245 { ATA_PDC40718, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40718" },
3246 { ATA_PDC40719, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40719" },
3247 { ATA_PDC40779, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40779" },
3248 { 0, 0, 0, 0, 0, 0}};
3249 char buffer[64];
3250 uintptr_t devid = 0;
3251
3252 if (!(idx = ata_match_chip(dev, ids)))
3253 return ENXIO;
3254
3255 /* if we are on a SuperTrak SX6000 dont attach */
3256 if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
3257 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
3258 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
3259 devid == ATA_I960RM)
3260 return ENXIO;
3261
3262 strcpy(buffer, "Promise ");
3263 strcat(buffer, idx->text);
3264
3265 /* if we are on a FastTrak TX4, adjust the interrupt resource */
3266 if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
3267 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
3268 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
3269 ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
3270 static long start = 0, end = 0;
3271
3272 if (pci_get_slot(dev) == 1) {
3273 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
3274 strcat(buffer, " (channel 0+1)");
3275 }
3276 else if (pci_get_slot(dev) == 2 && start && end) {
3277 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
3278 strcat(buffer, " (channel 2+3)");
3279 }
3280 else {
3281 start = end = 0;
3282 }
3283 }
3284 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
3285 device_set_desc_copy(dev, buffer);
3286 ctlr->chip = idx;
3287 ctlr->chipinit = ata_promise_chipinit;
3288 return 0;
3289 }
3290
3291 static int
3292 ata_promise_chipinit(device_t dev)
3293 {
3294 struct ata_pci_controller *ctlr = device_get_softc(dev);
3295 int fake_reg, stat_reg;
3296
3297 if (ata_setup_interrupt(dev))
3298 return ENXIO;
3299
3300 switch (ctlr->chip->cfg1) {
3301 case PRNEW:
3302 /* setup clocks */
3303 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
3304
3305 ctlr->dmainit = ata_promise_dmainit;
3306 /* FALLTHROUGH */
3307
3308 case PROLD:
3309 /* enable burst mode */
3310 ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
3311 ctlr->allocate = ata_promise_allocate;
3312 ctlr->setmode = ata_promise_setmode;
3313 return 0;
3314
3315 case PRTX:
3316 ctlr->allocate = ata_promise_tx2_allocate;
3317 ctlr->setmode = ata_promise_setmode;
3318 return 0;
3319
3320 case PRMIO:
3321 ctlr->r_type1 = SYS_RES_MEMORY;
3322 ctlr->r_rid1 = PCIR_BAR(4);
3323 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
3324 &ctlr->r_rid1, RF_ACTIVE)))
3325 goto failnfree;
3326
3327 ctlr->r_type2 = SYS_RES_MEMORY;
3328 ctlr->r_rid2 = PCIR_BAR(3);
3329 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3330 &ctlr->r_rid2, RF_ACTIVE)))
3331 goto failnfree;
3332
3333 if (ctlr->chip->cfg2 == PRSX4X) {
3334 struct ata_promise_sx4 *hpkt;
3335 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
3336
3337 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
3338 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3339 ata_promise_sx4_intr, ctlr, &ctlr->handle)) {
3340 device_printf(dev, "unable to setup interrupt\n");
3341 goto failnfree;
3342 }
3343
3344 /* print info about cache memory */
3345 device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
3346 (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
3347 ((dimm >> 24) & 0xff),
3348 ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
3349 " ECC enabled" : "" );
3350
3351 /* adjust cache memory parameters */
3352 ATA_OUTL(ctlr->r_res2, 0x000c000c,
3353 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
3354
3355 /* setup host packet controls */
3356 hpkt = malloc(sizeof(struct ata_promise_sx4),
3357 M_TEMP, M_NOWAIT | M_ZERO);
3358 mtx_init(&hpkt->mtx, "ATA promise HPKT lock", NULL, MTX_DEF);
3359 TAILQ_INIT(&hpkt->queue);
3360 hpkt->busy = 0;
3361 device_set_ivars(dev, hpkt);
3362 ctlr->allocate = ata_promise_mio_allocate;
3363 ctlr->reset = ata_promise_mio_reset;
3364 ctlr->dmainit = ata_promise_mio_dmainit;
3365 ctlr->setmode = ata_promise_setmode;
3366 ctlr->channels = 4;
3367 return 0;
3368 }
3369
3370 /* mio type controllers need an interrupt intercept */
3371 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
3372 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3373 ata_promise_mio_intr, ctlr, &ctlr->handle)) {
3374 device_printf(dev, "unable to setup interrupt\n");
3375 goto failnfree;
3376 }
3377
3378 switch (ctlr->chip->cfg2) {
3379 case PRPATA:
3380 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
3381 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
3382 goto sata150;
3383 case PRCMBO:
3384 ctlr->channels = 3;
3385 goto sata150;
3386 case PRSATA:
3387 ctlr->channels = 4;
3388 sata150:
3389 fake_reg = 0x60;
3390 stat_reg = 0x6c;
3391 break;
3392
3393 case PRCMBO2:
3394 ctlr->channels = 3;
3395 goto sataii;
3396 case PRSATA2:
3397 default:
3398 ctlr->channels = 4;
3399 sataii:
3400 fake_reg = 0x54;
3401 stat_reg = 0x60;
3402 break;
3403 }
3404
3405 /* prime fake interrupt register */
3406 ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
3407
3408 /* clear SATA status and unmask interrupts */
3409 ATA_OUTL(ctlr->r_res2, stat_reg, 0x000000ff);
3410
3411 /* enable "long burst lenght" on gen2 chips */
3412 if ((ctlr->chip->cfg2 == PRSATA2) || (ctlr->chip->cfg2 == PRCMBO2))
3413 ATA_OUTL(ctlr->r_res2, 0x44, ATA_INL(ctlr->r_res2, 0x44) | 0x2000);
3414
3415 ctlr->allocate = ata_promise_mio_allocate;
3416 ctlr->reset = ata_promise_mio_reset;
3417 ctlr->dmainit = ata_promise_mio_dmainit;
3418 ctlr->setmode = ata_promise_mio_setmode;
3419
3420 return 0;
3421 }
3422
3423 failnfree:
3424 if (ctlr->r_res2)
3425 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
3426 if (ctlr->r_res1)
3427 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
3428 return ENXIO;
3429 }
3430
3431 static int
3432 ata_promise_allocate(device_t dev)
3433 {
3434 struct ata_channel *ch = device_get_softc(dev);
3435
3436 if (ata_pci_allocate(dev))
3437 return ENXIO;
3438
3439 ch->hw.status = ata_promise_status;
3440 return 0;
3441 }
3442
3443 static int
3444 ata_promise_status(device_t dev)
3445 {
3446 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3447 struct ata_channel *ch = device_get_softc(dev);
3448
3449 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)) {
3450 return ata_pci_status(dev);
3451 }
3452 return 0;
3453 }
3454
3455 static int
3456 ata_promise_dmastart(device_t dev)
3457 {
3458 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3459 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3460 struct ata_device *atadev = device_get_softc(dev);
3461
3462 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3463 ATA_OUTB(ctlr->r_res1, 0x11,
3464 ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
3465 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20,
3466 ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
3467 (ch->dma->cur_iosize >> 1));
3468 }
3469 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
3470 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
3471 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
3472 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3473 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
3474 ATA_BMCMD_START_STOP);
3475 ch->flags |= ATA_DMA_ACTIVE;
3476 return 0;
3477 }
3478
3479 static int
3480 ata_promise_dmastop(device_t dev)
3481 {
3482 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3483 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3484 struct ata_device *atadev = device_get_softc(dev);
3485 int error;
3486
3487 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3488 ATA_OUTB(ctlr->r_res1, 0x11,
3489 ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
3490 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20, 0);
3491 }
3492 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
3493 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3494 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3495 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3496 ch->flags &= ~ATA_DMA_ACTIVE;
3497 return error;
3498 }
3499
3500 static void
3501 ata_promise_dmareset(device_t dev)
3502 {
3503 struct ata_channel *ch = device_get_softc(dev);
3504
3505 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3506 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3507 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3508 ch->flags &= ~ATA_DMA_ACTIVE;
3509 }
3510
3511 static void
3512 ata_promise_dmainit(device_t dev)
3513 {
3514 struct ata_channel *ch = device_get_softc(dev);
3515
3516 ata_dmainit(dev);
3517 if (ch->dma) {
3518 ch->dma->start = ata_promise_dmastart;
3519 ch->dma->stop = ata_promise_dmastop;
3520 ch->dma->reset = ata_promise_dmareset;
3521 }
3522 }
3523
3524 static void
3525 ata_promise_setmode(device_t dev, int mode)
3526 {
3527 device_t gparent = GRANDPARENT(dev);
3528 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3529 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3530 struct ata_device *atadev = device_get_softc(dev);
3531 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3532 int error;
3533 u_int32_t timings[][2] = {
3534 /* PROLD PRNEW mode */
3535 { 0x004ff329, 0x004fff2f }, /* PIO 0 */
3536 { 0x004fec25, 0x004ff82a }, /* PIO 1 */
3537 { 0x004fe823, 0x004ff026 }, /* PIO 2 */
3538 { 0x004fe622, 0x004fec24 }, /* PIO 3 */
3539 { 0x004fe421, 0x004fe822 }, /* PIO 4 */
3540 { 0x004567f3, 0x004acef6 }, /* MWDMA 0 */
3541 { 0x004467f3, 0x0048cef6 }, /* MWDMA 1 */
3542 { 0x004367f3, 0x0046cef6 }, /* MWDMA 2 */
3543 { 0x004367f3, 0x0046cef6 }, /* UDMA 0 */
3544 { 0x004247f3, 0x00448ef6 }, /* UDMA 1 */
3545 { 0x004127f3, 0x00436ef6 }, /* UDMA 2 */
3546 { 0, 0x00424ef6 }, /* UDMA 3 */
3547 { 0, 0x004127f3 }, /* UDMA 4 */
3548 { 0, 0x004127f3 } /* UDMA 5 */
3549 };
3550
3551 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3552
3553 switch (ctlr->chip->cfg1) {
3554 case PROLD:
3555 case PRNEW:
3556 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
3557 (ch->unit ? 1 << 11 : 1 << 10))) {
3558 ata_print_cable(dev, "controller");
3559 mode = ATA_UDMA2;
3560 }
3561 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
3562 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
3563 break;
3564
3565 case PRTX:
3566 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3567 if (mode > ATA_UDMA2 &&
3568 ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
3569 ata_print_cable(dev, "controller");
3570 mode = ATA_UDMA2;
3571 }
3572 break;
3573
3574 case PRMIO:
3575 if (mode > ATA_UDMA2 &&
3576 (ATA_INL(ctlr->r_res2,
3577 (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
3578 (ch->unit << 7)) & 0x01000000)) {
3579 ata_print_cable(dev, "controller");
3580 mode = ATA_UDMA2;
3581 }
3582 break;
3583 }
3584
3585 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3586
3587 if (bootverbose)
3588 device_printf(dev, "%ssetting %s on %s chip\n",
3589 (error) ? "FAILURE " : "",
3590 ata_mode2str(mode), ctlr->chip->text);
3591 if (!error) {
3592 if (ctlr->chip->cfg1 < PRTX)
3593 pci_write_config(gparent, 0x60 + (devno << 2),
3594 timings[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
3595 atadev->mode = mode;
3596 }
3597 return;
3598 }
3599
3600 static int
3601 ata_promise_tx2_allocate(device_t dev)
3602 {
3603 struct ata_channel *ch = device_get_softc(dev);
3604
3605 if (ata_pci_allocate(dev))
3606 return ENXIO;
3607
3608 ch->hw.status = ata_promise_tx2_status;
3609 return 0;
3610 }
3611
3612 static int
3613 ata_promise_tx2_status(device_t dev)
3614 {
3615 struct ata_channel *ch = device_get_softc(dev);
3616
3617 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3618 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
3619 return ata_pci_status(dev);
3620 }
3621 return 0;
3622 }
3623
3624 static int
3625 ata_promise_mio_allocate(device_t dev)
3626 {
3627 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3628 struct ata_channel *ch = device_get_softc(dev);
3629 int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
3630 int i;
3631
3632 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
3633 ch->r_io[i].res = ctlr->r_res2;
3634 ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7);
3635 }
3636 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
3637 ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
3638 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
3639 ata_default_registers(dev);
3640 if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
3641 ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
3642 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3643 ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
3644 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3645 ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
3646 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3647 ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
3648 ch->flags |= ATA_NO_SLAVE;
3649 }
3650 ch->flags |= ATA_USE_16BIT;
3651
3652 ata_generic_hw(dev);
3653 if (ctlr->chip->cfg2 & PRSX4X) {
3654 ch->hw.command = ata_promise_sx4_command;
3655 }
3656 else {
3657 ch->hw.command = ata_promise_mio_command;
3658 ch->hw.status = ata_promise_mio_status;
3659 }
3660 return 0;
3661 }
3662
3663 static void
3664 ata_promise_mio_intr(void *data)
3665 {
3666 struct ata_pci_controller *ctlr = data;
3667 struct ata_channel *ch;
3668 u_int32_t vector;
3669 int unit, fake_reg;
3670
3671 switch (ctlr->chip->cfg2) {
3672 case PRPATA:
3673 case PRCMBO:
3674 case PRSATA:
3675 fake_reg = 0x60;
3676 break;
3677 case PRCMBO2:
3678 case PRSATA2:
3679 default:
3680 fake_reg = 0x54;
3681 break;
3682 }
3683
3684 /*
3685 * since reading interrupt status register on early "mio" chips
3686 * clears the status bits we cannot read it for each channel later on
3687 * in the generic interrupt routine.
3688 * store the bits in an unused register in the chip so we can read
3689 * it from there safely to get around this "feature".
3690 */
3691 vector = ATA_INL(ctlr->r_res2, 0x040);
3692 ATA_OUTL(ctlr->r_res2, 0x040, vector);
3693 ATA_OUTL(ctlr->r_res2, fake_reg, vector);
3694
3695 for (unit = 0; unit < ctlr->channels; unit++) {
3696 if ((ch = ctlr->interrupt[unit].argument))
3697 ctlr->interrupt[unit].function(ch);
3698 }
3699
3700 ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
3701 }
3702
3703 static int
3704 ata_promise_mio_status(device_t dev)
3705 {
3706 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3707 struct ata_channel *ch = device_get_softc(dev);
3708 struct ata_connect_task *tp;
3709 u_int32_t fake_reg, stat_reg, vector, status;
3710
3711 switch (ctlr->chip->cfg2) {
3712 case PRPATA:
3713 case PRCMBO:
3714 case PRSATA:
3715 fake_reg = 0x60;
3716 stat_reg = 0x6c;
3717 break;
3718 case PRCMBO2:
3719 case PRSATA2:
3720 default:
3721 fake_reg = 0x54;
3722 stat_reg = 0x60;
3723 break;
3724 }
3725
3726 /* read and acknowledge interrupt */
3727 vector = ATA_INL(ctlr->r_res2, fake_reg);
3728
3729 /* read and clear interface status */
3730 status = ATA_INL(ctlr->r_res2, stat_reg);
3731 ATA_OUTL(ctlr->r_res2, stat_reg, status & (0x00000011 << ch->unit));
3732
3733 /* check for and handle disconnect events */
3734 if ((status & (0x00000001 << ch->unit)) &&
3735 (tp = (struct ata_connect_task *)
3736 malloc(sizeof(struct ata_connect_task),
3737 M_ATA, M_NOWAIT | M_ZERO))) {
3738
3739 if (bootverbose)
3740 device_printf(ch->dev, "DISCONNECT requested\n");
3741 tp->action = ATA_C_DETACH;
3742 tp->dev = ch->dev;
3743 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3744 taskqueue_enqueue(taskqueue_thread, &tp->task);
3745 }
3746
3747 /* check for and handle connect events */
3748 if ((status & (0x00000010 << ch->unit)) &&
3749 (tp = (struct ata_connect_task *)
3750 malloc(sizeof(struct ata_connect_task),
3751 M_ATA, M_NOWAIT | M_ZERO))) {
3752
3753 if (bootverbose)
3754 device_printf(ch->dev, "CONNECT requested\n");
3755 tp->action = ATA_C_ATTACH;
3756 tp->dev = ch->dev;
3757 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3758 taskqueue_enqueue(taskqueue_thread, &tp->task);
3759 }
3760
3761 /* do we have any device action ? */
3762 return (vector & (1 << (ch->unit + 1)));
3763 }
3764
3765 static int
3766 ata_promise_mio_command(struct ata_request *request)
3767 {
3768 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
3769 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
3770 u_int32_t *wordp = (u_int32_t *)ch->dma->work;
3771
3772 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
3773
3774 /* XXX SOS add ATAPI commands support later */
3775 switch (request->u.ata.command) {
3776 default:
3777 return ata_generic_command(request);
3778
3779 case ATA_READ_DMA:
3780 case ATA_READ_DMA48:
3781 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
3782 break;
3783
3784 case ATA_WRITE_DMA:
3785 case ATA_WRITE_DMA48:
3786 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
3787 break;
3788 }
3789 wordp[1] = htole32(ch->dma->sg_bus);
3790 wordp[2] = 0;
3791 ata_promise_apkt((u_int8_t*)wordp, request);
3792
3793 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
3794 return 0;
3795 }
3796
3797 static void
3798 ata_promise_mio_reset(device_t dev)
3799 {
3800 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3801 struct ata_channel *ch = device_get_softc(dev);
3802 struct ata_promise_sx4 *hpktp;
3803
3804 switch (ctlr->chip->cfg2) {
3805 case PRSX4X:
3806
3807 /* softreset channel ATA module */
3808 hpktp = device_get_ivars(ctlr->dev);
3809 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
3810 ata_udelay(1000);
3811 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
3812 (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
3813 ~0x00003f9f) | (ch->unit + 1));
3814
3815 /* softreset HOST module */ /* XXX SOS what about other outstandings */
3816 mtx_lock(&hpktp->mtx);
3817 ATA_OUTL(ctlr->r_res2, 0xc012c,
3818 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
3819 DELAY(10);
3820 ATA_OUTL(ctlr->r_res2, 0xc012c,
3821 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
3822 hpktp->busy = 0;
3823 mtx_unlock(&hpktp->mtx);
3824 ata_generic_reset(dev);
3825 break;
3826
3827 case PRPATA:
3828 case PRCMBO:
3829 case PRSATA:
3830 if ((ctlr->chip->cfg2 == PRSATA) ||
3831 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
3832
3833 /* mask plug/unplug intr */
3834 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
3835 }
3836
3837 /* softreset channels ATA module */
3838 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
3839 ata_udelay(10000);
3840 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
3841 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
3842 ~0x00003f9f) | (ch->unit + 1));
3843
3844 if ((ctlr->chip->cfg2 == PRSATA) ||
3845 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
3846
3847 if (ata_sata_phy_reset(dev))
3848 ata_generic_reset(dev);
3849
3850 /* reset and enable plug/unplug intr */
3851 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
3852 }
3853 else
3854 ata_generic_reset(dev);
3855 break;
3856
3857 case PRCMBO2:
3858 case PRSATA2:
3859 if ((ctlr->chip->cfg2 == PRSATA2) ||
3860 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
3861 /* set portmultiplier port */
3862 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
3863
3864 /* mask plug/unplug intr */
3865 ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
3866 }
3867
3868 /* softreset channels ATA module */
3869 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
3870 ata_udelay(10000);
3871 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
3872 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
3873 ~0x00003f9f) | (ch->unit + 1));
3874
3875 if ((ctlr->chip->cfg2 == PRSATA2) ||
3876 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
3877
3878 /* set PHY mode to "improved" */
3879 ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
3880 (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
3881 ~0x00000003) | 0x00000001);
3882
3883 if (ata_sata_phy_reset(dev))
3884 ata_generic_reset(dev);
3885
3886 /* reset and enable plug/unplug intr */
3887 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
3888
3889 /* set portmultiplier port */
3890 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
3891 }
3892 else
3893 ata_generic_reset(dev);
3894 break;
3895
3896 }
3897 }
3898
3899 static void
3900 ata_promise_mio_dmainit(device_t dev)
3901 {
3902 struct ata_channel *ch = device_get_softc(dev);
3903
3904 /* note start and stop are not used here */
3905 ata_dmainit(dev);
3906 if (ch->dma)
3907 ch->dma->setprd = ata_promise_mio_setprd;
3908 }
3909
3910
3911 #define MAXLASTSGSIZE (32 * sizeof(u_int32_t))
3912 static void
3913 ata_promise_mio_setprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
3914 {
3915 struct ata_dmasetprd_args *args = xsc;
3916 struct ata_dma_prdentry *prd = args->dmatab;
3917 int i;
3918
3919 if ((args->error = error))
3920 return;
3921
3922 for (i = 0; i < nsegs; i++) {
3923 prd[i].addr = htole32(segs[i].ds_addr);
3924 prd[i].count = htole32(segs[i].ds_len);
3925 }
3926 if (segs[i - 1].ds_len > MAXLASTSGSIZE) {
3927 //printf("split last SG element of %u\n", segs[i - 1].ds_len);
3928 prd[i - 1].count = htole32(segs[i - 1].ds_len - MAXLASTSGSIZE);
3929 prd[i].count = htole32(MAXLASTSGSIZE);
3930 prd[i].addr = htole32(segs[i - 1].ds_addr +
3931 (segs[i - 1].ds_len - MAXLASTSGSIZE));
3932 nsegs++;
3933 i++;
3934 }
3935 prd[i - 1].count |= htole32(ATA_DMA_EOT);
3936 KASSERT(nsegs <= ATA_DMA_ENTRIES, ("too many DMA segment entries\n"));
3937 args->nsegs = nsegs;
3938 }
3939
3940 static void
3941 ata_promise_mio_setmode(device_t dev, int mode)
3942 {
3943 device_t gparent = GRANDPARENT(dev);
3944 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3945 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3946
3947 if ( (ctlr->chip->cfg2 == PRSATA) ||
3948 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2)) ||
3949 (ctlr->chip->cfg2 == PRSATA2) ||
3950 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2)))
3951 ata_sata_setmode(dev, mode);
3952 else
3953 ata_promise_setmode(dev, mode);
3954 }
3955
3956 static void
3957 ata_promise_sx4_intr(void *data)
3958 {
3959 struct ata_pci_controller *ctlr = data;
3960 struct ata_channel *ch;
3961 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
3962 int unit;
3963
3964 for (unit = 0; unit < ctlr->channels; unit++) {
3965 if (vector & (1 << (unit + 1)))
3966 if ((ch = ctlr->interrupt[unit].argument))
3967 ctlr->interrupt[unit].function(ch);
3968 if (vector & (1 << (unit + 5)))
3969 if ((ch = ctlr->interrupt[unit].argument))
3970 ata_promise_queue_hpkt(ctlr,
3971 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
3972 ATA_PDC_HPKT_OFFSET));
3973 if (vector & (1 << (unit + 9))) {
3974 ata_promise_next_hpkt(ctlr);
3975 if ((ch = ctlr->interrupt[unit].argument))
3976 ctlr->interrupt[unit].function(ch);
3977 }
3978 if (vector & (1 << (unit + 13))) {
3979 ata_promise_next_hpkt(ctlr);
3980 if ((ch = ctlr->interrupt[unit].argument))
3981 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
3982 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
3983 ATA_PDC_APKT_OFFSET));
3984 }
3985 }
3986 }
3987
3988 static int
3989 ata_promise_sx4_command(struct ata_request *request)
3990 {
3991 device_t gparent = GRANDPARENT(request->dev);
3992 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3993 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
3994 struct ata_dma_prdentry *prd = ch->dma->sg;
3995 caddr_t window = rman_get_virtual(ctlr->r_res1);
3996 u_int32_t *wordp;
3997 int i, idx, length = 0;
3998
3999 /* XXX SOS add ATAPI commands support later */
4000 switch (request->u.ata.command) {
4001
4002 default:
4003 return -1;
4004
4005 case ATA_ATA_IDENTIFY:
4006 case ATA_READ:
4007 case ATA_READ48:
4008 case ATA_READ_MUL:
4009 case ATA_READ_MUL48:
4010 case ATA_WRITE:
4011 case ATA_WRITE48:
4012 case ATA_WRITE_MUL:
4013 case ATA_WRITE_MUL48:
4014 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
4015 return ata_generic_command(request);
4016
4017 case ATA_SETFEATURES:
4018 case ATA_FLUSHCACHE:
4019 case ATA_FLUSHCACHE48:
4020 case ATA_SLEEP:
4021 case ATA_SET_MULTI:
4022 wordp = (u_int32_t *)
4023 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
4024 wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
4025 wordp[1] = 0;
4026 wordp[2] = 0;
4027 ata_promise_apkt((u_int8_t *)wordp, request);
4028 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
4029 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
4030 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
4031 htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
4032 return 0;
4033
4034 case ATA_READ_DMA:
4035 case ATA_READ_DMA48:
4036 case ATA_WRITE_DMA:
4037 case ATA_WRITE_DMA48:
4038 wordp = (u_int32_t *)
4039 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
4040 i = idx = 0;
4041 do {
4042 wordp[idx++] = prd[i].addr;
4043 wordp[idx++] = prd[i].count;
4044 length += (prd[i].count & ~ATA_DMA_EOT);
4045 } while (!(prd[i++].count & ATA_DMA_EOT));
4046
4047 wordp = (u_int32_t *)
4048 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
4049 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
4050 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
4051
4052 wordp = (u_int32_t *)
4053 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
4054 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
4055 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
4056
4057 wordp = (u_int32_t *)
4058 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
4059 if (request->flags & ATA_R_READ)
4060 wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
4061 if (request->flags & ATA_R_WRITE)
4062 wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
4063 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
4064 wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
4065 wordp[3] = 0;
4066
4067 wordp = (u_int32_t *)
4068 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
4069 if (request->flags & ATA_R_READ)
4070 wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
4071 if (request->flags & ATA_R_WRITE)
4072 wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
4073 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
4074 wordp[2] = 0;
4075 ata_promise_apkt((u_int8_t *)wordp, request);
4076 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
4077
4078 if (request->flags & ATA_R_READ) {
4079 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
4080 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
4081 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
4082 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
4083 }
4084 if (request->flags & ATA_R_WRITE) {
4085 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
4086 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
4087 ata_promise_queue_hpkt(ctlr,
4088 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
4089 }
4090 return 0;
4091 }
4092 }
4093
4094 static int
4095 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
4096 {
4097 struct ata_device *atadev = device_get_softc(request->dev);
4098 int i = 12;
4099
4100 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
4101 bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
4102 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
4103 bytep[i++] = ATA_A_4BIT;
4104
4105 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
4106 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
4107 bytep[i++] = request->u.ata.feature >> 8;
4108 bytep[i++] = request->u.ata.feature;
4109 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
4110 bytep[i++] = request->u.ata.count >> 8;
4111 bytep[i++] = request->u.ata.count;
4112 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
4113 bytep[i++] = request->u.ata.lba >> 24;
4114 bytep[i++] = request->u.ata.lba;
4115 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
4116 bytep[i++] = request->u.ata.lba >> 32;
4117 bytep[i++] = request->u.ata.lba >> 8;
4118 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
4119 bytep[i++] = request->u.ata.lba >> 40;
4120 bytep[i++] = request->u.ata.lba >> 16;
4121 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
4122 bytep[i++] = ATA_D_LBA | atadev->unit;
4123 }
4124 else {
4125 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
4126 bytep[i++] = request->u.ata.feature;
4127 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
4128 bytep[i++] = request->u.ata.count;
4129 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
4130 bytep[i++] = request->u.ata.lba;
4131 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
4132 bytep[i++] = request->u.ata.lba >> 8;
4133 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
4134 bytep[i++] = request->u.ata.lba >> 16;
4135 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
4136 bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
4137 ATA_D_IBM | atadev->unit | ((request->u.ata.lba >> 24)&0xf);
4138 }
4139 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
4140 bytep[i++] = request->u.ata.command;
4141 return i;
4142 }
4143
4144 static void
4145 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
4146 {
4147 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
4148
4149 mtx_lock(&hpktp->mtx);
4150 if (hpktp->busy) {
4151 struct host_packet *hp =
4152 malloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
4153 hp->addr = hpkt;
4154 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
4155 }
4156 else {
4157 hpktp->busy = 1;
4158 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
4159 }
4160 mtx_unlock(&hpktp->mtx);
4161 }
4162
4163 static void
4164 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
4165 {
4166 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
4167 struct host_packet *hp;
4168
4169 mtx_lock(&hpktp->mtx);
4170 if ((hp = TAILQ_FIRST(&hpktp->queue))) {
4171 TAILQ_REMOVE(&hpktp->queue, hp, chain);
4172 ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
4173 free(hp, M_TEMP);
4174 }
4175 else
4176 hpktp->busy = 0;
4177 mtx_unlock(&hpktp->mtx);
4178 }
4179
4180
4181 /*
4182 * ServerWorks chipset support functions
4183 */
4184 int
4185 ata_serverworks_ident(device_t dev)
4186 {
4187 struct ata_pci_controller *ctlr = device_get_softc(dev);
4188 static struct ata_chip_id ids[] =
4189 {{ ATA_ROSB4, 0x00, SWKS33, 0, ATA_UDMA2, "ROSB4" },
4190 { ATA_CSB5, 0x92, SWKS100, 0, ATA_UDMA5, "CSB5" },
4191 { ATA_CSB5, 0x00, SWKS66, 0, ATA_UDMA4, "CSB5" },
4192 { ATA_CSB6, 0x00, SWKS100, 0, ATA_UDMA5, "CSB6" },
4193 { ATA_CSB6_1, 0x00, SWKS66, 0, ATA_UDMA4, "CSB6" },
4194 { ATA_HT1000, 0x00, SWKS100, 0, ATA_UDMA5, "HT1000" },
4195 { ATA_HT1000_S1, 0x00, SWKSMIO, 4, ATA_SA150, "HT1000" },
4196 { ATA_HT1000_S2, 0x00, SWKSMIO, 4, ATA_SA150, "HT1000" },
4197 { ATA_K2, 0x00, SWKSMIO, 4, ATA_SA150, "K2" },
4198 { ATA_FRODO4, 0x00, SWKSMIO, 4, ATA_SA150, "Frodo4" },
4199 { ATA_FRODO8, 0x00, SWKSMIO, 8, ATA_SA150, "Frodo8" },
4200 { 0, 0, 0, 0, 0, 0}};
4201
4202 if (!(ctlr->chip = ata_match_chip(dev, ids)))
4203 return ENXIO;
4204
4205 ata_set_desc(dev);
4206 ctlr->chipinit = ata_serverworks_chipinit;
4207 return 0;
4208 }
4209
4210 static int
4211 ata_serverworks_chipinit(device_t dev)
4212 {
4213 struct ata_pci_controller *ctlr = device_get_softc(dev);
4214
4215 if (ata_setup_interrupt(dev))
4216 return ENXIO;
4217
4218 if (ctlr->chip->cfg1 == SWKSMIO) {
4219 ctlr->r_type2 = SYS_RES_MEMORY;
4220 ctlr->r_rid2 = PCIR_BAR(5);
4221 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4222 &ctlr->r_rid2, RF_ACTIVE)))
4223 return ENXIO;
4224
4225 ctlr->channels = ctlr->chip->cfg2;
4226 ctlr->allocate = ata_serverworks_allocate;
4227 ctlr->setmode = ata_sata_setmode;
4228 return 0;
4229 }
4230 else if (ctlr->chip->cfg1 == SWKS33) {
4231 device_t *children;
4232 int nchildren, i;
4233
4234 /* locate the ISA part in the southbridge and enable UDMA33 */
4235 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
4236 for (i = 0; i < nchildren; i++) {
4237 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
4238 pci_write_config(children[i], 0x64,
4239 (pci_read_config(children[i], 0x64, 4) &
4240 ~0x00002000) | 0x00004000, 4);
4241 break;
4242 }
4243 }
4244 free(children, M_TEMP);
4245 }
4246 }
4247 else {
4248 pci_write_config(dev, 0x5a,
4249 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
4250 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
4251 }
4252 ctlr->setmode = ata_serverworks_setmode;
4253 return 0;
4254 }
4255
4256 static int
4257 ata_serverworks_allocate(device_t dev)
4258 {
4259 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4260 struct ata_channel *ch = device_get_softc(dev);
4261 int ch_offset;
4262 int i;
4263
4264 ch_offset = ch->unit * 0x100;
4265
4266 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
4267 ch->r_io[i].res = ctlr->r_res2;
4268
4269 /* setup ATA registers */
4270 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
4271 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x04;
4272 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
4273 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
4274 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
4275 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
4276 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
4277 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1c;
4278 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x20;
4279 ata_default_registers(dev);
4280
4281 /* setup DMA registers */
4282 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x30;
4283 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x32;
4284 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x34;
4285
4286 /* setup SATA registers */
4287 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x40;
4288 ch->r_io[ATA_SERROR].offset = ch_offset + 0x44;
4289 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x48;
4290
4291 ch->flags |= ATA_NO_SLAVE;
4292 ata_pci_hw(dev);
4293 ch->hw.tf_read = ata_serverworks_tf_read;
4294 ch->hw.tf_write = ata_serverworks_tf_write;
4295
4296 /* chip does not reliably do 64K DMA transfers */
4297 if (ch->dma)
4298 ch->dma->max_iosize = 64 * DEV_BSIZE;
4299
4300 return 0;
4301 }
4302
4303 static void
4304 ata_serverworks_tf_read(struct ata_request *request)
4305 {
4306 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4307 struct ata_device *atadev = device_get_softc(request->dev);
4308
4309 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
4310 u_int16_t temp;
4311
4312 request->u.ata.count = ATA_IDX_INW(ch, ATA_COUNT);
4313 temp = ATA_IDX_INW(ch, ATA_SECTOR);
4314 request->u.ata.lba = (u_int64_t)(temp & 0x00ff) |
4315 ((u_int64_t)(temp & 0xff00) << 24);
4316 temp = ATA_IDX_INW(ch, ATA_CYL_LSB);
4317 request->u.ata.lba |= ((u_int64_t)(temp & 0x00ff) << 8) |
4318 ((u_int64_t)(temp & 0xff00) << 32);
4319 temp = ATA_IDX_INW(ch, ATA_CYL_MSB);
4320 request->u.ata.lba |= ((u_int64_t)(temp & 0x00ff) << 16) |
4321 ((u_int64_t)(temp & 0xff00) << 40);
4322 }
4323 else {
4324 request->u.ata.count = ATA_IDX_INW(ch, ATA_COUNT) & 0x00ff;
4325 request->u.ata.lba = (ATA_IDX_INW(ch, ATA_SECTOR) & 0x00ff) |
4326 ((ATA_IDX_INW(ch, ATA_CYL_LSB) & 0x00ff) << 8) |
4327 ((ATA_IDX_INW(ch, ATA_CYL_MSB) & 0x00ff) << 16) |
4328 ((ATA_IDX_INW(ch, ATA_DRIVE) & 0xf) << 24);
4329 }
4330 }
4331
4332 static void
4333 ata_serverworks_tf_write(struct ata_request *request)
4334 {
4335 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4336 struct ata_device *atadev = device_get_softc(request->dev);
4337
4338 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
4339 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
4340 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
4341 ATA_IDX_OUTW(ch, ATA_SECTOR, ((request->u.ata.lba >> 16) & 0xff00) |
4342 (request->u.ata.lba & 0x00ff));
4343 ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((request->u.ata.lba >> 24) & 0xff00) |
4344 ((request->u.ata.lba >> 8) & 0x00ff));
4345 ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((request->u.ata.lba >> 32) & 0xff00) |
4346 ((request->u.ata.lba >> 16) & 0x00ff));
4347 ATA_IDX_OUTW(ch, ATA_DRIVE, ATA_D_LBA | atadev->unit);
4348 }
4349 else {
4350 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
4351 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
4352 if (atadev->flags & ATA_D_USE_CHS) {
4353 int heads, sectors;
4354
4355 if (atadev->param.atavalid & ATA_FLAG_54_58) {
4356 heads = atadev->param.current_heads;
4357 sectors = atadev->param.current_sectors;
4358 }
4359 else {
4360 heads = atadev->param.heads;
4361 sectors = atadev->param.sectors;
4362 }
4363 ATA_IDX_OUTW(ch, ATA_SECTOR, (request->u.ata.lba % sectors)+1);
4364 ATA_IDX_OUTW(ch, ATA_CYL_LSB,
4365 (request->u.ata.lba / (sectors * heads)));
4366 ATA_IDX_OUTW(ch, ATA_CYL_MSB,
4367 (request->u.ata.lba / (sectors * heads)) >> 8);
4368 ATA_IDX_OUTW(ch, ATA_DRIVE, ATA_D_IBM | atadev->unit |
4369 (((request->u.ata.lba% (sectors * heads)) /
4370 sectors) & 0xf));
4371 }
4372 else {
4373 ATA_IDX_OUTW(ch, ATA_SECTOR, request->u.ata.lba);
4374 ATA_IDX_OUTW(ch, ATA_CYL_LSB, request->u.ata.lba >> 8);
4375 ATA_IDX_OUTW(ch, ATA_CYL_MSB, request->u.ata.lba >> 16);
4376 ATA_IDX_OUTW(ch, ATA_DRIVE,
4377 ATA_D_IBM | ATA_D_LBA | atadev->unit |
4378 ((request->u.ata.lba >> 24) & 0x0f));
4379 }
4380 }
4381 }
4382
4383 static void
4384 ata_serverworks_setmode(device_t dev, int mode)
4385 {
4386 device_t gparent = GRANDPARENT(dev);
4387 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4388 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4389 struct ata_device *atadev = device_get_softc(dev);
4390 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4391 int offset = (devno ^ 0x01) << 3;
4392 int error;
4393 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
4394 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
4395 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
4396
4397 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4398
4399 mode = ata_check_80pin(dev, mode);
4400
4401 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4402
4403 if (bootverbose)
4404 device_printf(dev, "%ssetting %s on %s chip\n",
4405 (error) ? "FAILURE " : "",
4406 ata_mode2str(mode), ctlr->chip->text);
4407 if (!error) {
4408 if (mode >= ATA_UDMA0) {
4409 pci_write_config(gparent, 0x56,
4410 (pci_read_config(gparent, 0x56, 2) &
4411 ~(0xf << (devno << 2))) |
4412 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
4413 pci_write_config(gparent, 0x54,
4414 pci_read_config(gparent, 0x54, 1) |
4415 (0x01 << devno), 1);
4416 pci_write_config(gparent, 0x44,
4417 (pci_read_config(gparent, 0x44, 4) &
4418 ~(0xff << offset)) |
4419 (dmatimings[2] << offset), 4);
4420 }
4421 else if (mode >= ATA_WDMA0) {
4422 pci_write_config(gparent, 0x54,
4423 pci_read_config(gparent, 0x54, 1) &
4424 ~(0x01 << devno), 1);
4425 pci_write_config(gparent, 0x44,
4426 (pci_read_config(gparent, 0x44, 4) &
4427 ~(0xff << offset)) |
4428 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
4429 }
4430 else
4431 pci_write_config(gparent, 0x54,
4432 pci_read_config(gparent, 0x54, 1) &
4433 ~(0x01 << devno), 1);
4434
4435 pci_write_config(gparent, 0x40,
4436 (pci_read_config(gparent, 0x40, 4) &
4437 ~(0xff << offset)) |
4438 (piotimings[ata_mode2idx(mode)] << offset), 4);
4439 atadev->mode = mode;
4440 }
4441 }
4442
4443
4444 /*
4445 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
4446 */
4447 int
4448 ata_sii_ident(device_t dev)
4449 {
4450 struct ata_pci_controller *ctlr = device_get_softc(dev);
4451 static struct ata_chip_id ids[] =
4452 {{ ATA_SII3114, 0x00, SIIMEMIO, SII4CH, ATA_SA150, "SiI 3114" },
4453 { ATA_SII3512, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3512" },
4454 { ATA_SII3112, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
4455 { ATA_SII3112_1, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
4456 { ATA_SII3512, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3512" },
4457 { ATA_SII3112, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
4458 { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
4459 { ATA_SII3124, 0x00, SIIPRBIO, SII4CH, ATA_SA300, "SiI 3124" },
4460 { ATA_SII3132, 0x00, SIIPRBIO, 0, ATA_SA300, "SiI 3132" },
4461 { ATA_SII0680, 0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
4462 { ATA_CMD649, 0x00, 0, SIIINTR, ATA_UDMA5, "CMD 649" },
4463 { ATA_CMD648, 0x00, 0, SIIINTR, ATA_UDMA4, "CMD 648" },
4464 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "CMD 646U2" },
4465 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "CMD 646" },
4466 { 0, 0, 0, 0, 0, 0}};
4467
4468 if (!(ctlr->chip = ata_match_chip(dev, ids)))
4469 return ENXIO;
4470
4471 ata_set_desc(dev);
4472 ctlr->chipinit = ata_sii_chipinit;
4473 return 0;
4474 }
4475
4476 static int
4477 ata_sii_chipinit(device_t dev)
4478 {
4479 struct ata_pci_controller *ctlr = device_get_softc(dev);
4480
4481 if (ata_setup_interrupt(dev))
4482 return ENXIO;
4483
4484 switch (ctlr->chip->cfg1) {
4485 case SIIPRBIO:
4486 ctlr->r_type1 = SYS_RES_MEMORY;
4487 ctlr->r_rid1 = PCIR_BAR(0);
4488 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
4489 &ctlr->r_rid1, RF_ACTIVE)))
4490 return ENXIO;
4491
4492 ctlr->r_rid2 = PCIR_BAR(2);
4493 ctlr->r_type2 = SYS_RES_MEMORY;
4494 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4495 &ctlr->r_rid2, RF_ACTIVE))){
4496 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
4497 return ENXIO;
4498 }
4499 ctlr->allocate = ata_siiprb_allocate;
4500 ctlr->reset = ata_siiprb_reset;
4501 ctlr->dmainit = ata_siiprb_dmainit;
4502 ctlr->setmode = ata_sata_setmode;
4503 ctlr->channels = (ctlr->chip->cfg2 == SII4CH) ? 4 : 2;
4504
4505 /* reset controller */
4506 ATA_OUTL(ctlr->r_res1, 0x0040, 0x80000000);
4507 DELAY(10000);
4508 ATA_OUTL(ctlr->r_res1, 0x0040, 0x0000000f);
4509
4510 /* enable PCI interrupt */
4511 pci_write_config(dev, PCIR_COMMAND,
4512 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
4513 break;
4514
4515 case SIIMEMIO:
4516 ctlr->r_type2 = SYS_RES_MEMORY;
4517 ctlr->r_rid2 = PCIR_BAR(5);
4518 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4519 &ctlr->r_rid2, RF_ACTIVE))) {
4520 if (ctlr->chip->chipid != ATA_SII0680 ||
4521 (pci_read_config(dev, 0x8a, 1) & 1))
4522 return ENXIO;
4523 }
4524
4525 if (ctlr->chip->cfg2 & SIISETCLK) {
4526 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
4527 pci_write_config(dev, 0x8a,
4528 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
4529 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
4530 device_printf(dev, "%s could not set ATA133 clock\n",
4531 ctlr->chip->text);
4532 }
4533
4534 /* if we have 4 channels enable the second set */
4535 if (ctlr->chip->cfg2 & SII4CH) {
4536 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
4537 ctlr->channels = 4;
4538 }
4539
4540 /* dont block interrupts from any channel */
4541 pci_write_config(dev, 0x48,
4542 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
4543
4544 /* enable PCI interrupt as BIOS might not */
4545 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
4546
4547 if (ctlr->r_res2)
4548 ctlr->allocate = ata_sii_allocate;
4549
4550 if (ctlr->chip->max_dma >= ATA_SA150) {
4551 ctlr->reset = ata_sii_reset;
4552 ctlr->setmode = ata_sata_setmode;
4553 }
4554 else
4555 ctlr->setmode = ata_sii_setmode;
4556 break;
4557
4558 default:
4559 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
4560 device_printf(dev, "HW has secondary channel disabled\n");
4561 ctlr->channels = 1;
4562 }
4563
4564 /* enable interrupt as BIOS might not */
4565 pci_write_config(dev, 0x71, 0x01, 1);
4566
4567 ctlr->allocate = ata_cmd_allocate;
4568 ctlr->setmode = ata_cmd_setmode;
4569 break;
4570 }
4571 return 0;
4572 }
4573
4574 static int
4575 ata_cmd_allocate(device_t dev)
4576 {
4577 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4578 struct ata_channel *ch = device_get_softc(dev);
4579
4580 /* setup the usual register normal pci style */
4581 if (ata_pci_allocate(dev))
4582 return ENXIO;
4583
4584 if (ctlr->chip->cfg2 & SIIINTR)
4585 ch->hw.status = ata_cmd_status;
4586
4587 return 0;
4588 }
4589
4590 static int
4591 ata_cmd_status(device_t dev)
4592 {
4593 struct ata_channel *ch = device_get_softc(dev);
4594 u_int8_t reg71;
4595
4596 if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
4597 (ch->unit ? 0x08 : 0x04))) {
4598 pci_write_config(device_get_parent(ch->dev), 0x71,
4599 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
4600 return ata_pci_status(dev);
4601 }
4602 return 0;
4603 }
4604
4605 static void
4606 ata_cmd_setmode(device_t dev, int mode)
4607 {
4608 device_t gparent = GRANDPARENT(dev);
4609 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4610 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4611 struct ata_device *atadev = device_get_softc(dev);
4612 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4613 int error;
4614
4615 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4616
4617 mode = ata_check_80pin(dev, mode);
4618
4619 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4620
4621 if (bootverbose)
4622 device_printf(dev, "%ssetting %s on %s chip\n",
4623 (error) ? "FAILURE " : "",
4624 ata_mode2str(mode), ctlr->chip->text);
4625 if (!error) {
4626 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
4627 int ureg = ch->unit ? 0x7b : 0x73;
4628
4629 if (mode >= ATA_UDMA0) {
4630 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
4631 { 0x11, 0x42 }, { 0x25, 0x8a },
4632 { 0x15, 0x4a }, { 0x05, 0x0a } };
4633
4634 u_int8_t umode = pci_read_config(gparent, ureg, 1);
4635
4636 umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
4637 umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
4638 pci_write_config(gparent, ureg, umode, 1);
4639 }
4640 else if (mode >= ATA_WDMA0) {
4641 int dmatimings[] = { 0x87, 0x32, 0x3f };
4642
4643 pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
4644 pci_write_config(gparent, ureg,
4645 pci_read_config(gparent, ureg, 1) &
4646 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
4647 }
4648 else {
4649 int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
4650 pci_write_config(gparent, treg,
4651 piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
4652 pci_write_config(gparent, ureg,
4653 pci_read_config(gparent, ureg, 1) &
4654 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
4655 }
4656 atadev->mode = mode;
4657 }
4658 }
4659
4660 static int
4661 ata_sii_allocate(device_t dev)
4662 {
4663 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4664 struct ata_channel *ch = device_get_softc(dev);
4665 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
4666 int i;
4667
4668 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
4669 ch->r_io[i].res = ctlr->r_res2;
4670 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
4671 }
4672 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
4673 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
4674 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
4675 ata_default_registers(dev);
4676
4677 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
4678 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
4679 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
4680 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
4681 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
4682 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
4683
4684 if (ctlr->chip->max_dma >= ATA_SA150) {
4685 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4686 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
4687 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4688 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
4689 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4690 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
4691 ch->flags |= ATA_NO_SLAVE;
4692
4693 /* enable PHY state change interrupt */
4694 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
4695 }
4696
4697 if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
4698 /* work around errata in early chips */
4699 ch->dma->boundary = 8192;
4700 ch->dma->segsize = 15 * DEV_BSIZE;
4701 }
4702
4703 ata_pci_hw(dev);
4704 ch->hw.status = ata_sii_status;
4705 return 0;
4706 }
4707
4708 static int
4709 ata_sii_status(device_t dev)
4710 {
4711 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4712 struct ata_channel *ch = device_get_softc(dev);
4713 int offset0 = ((ch->unit & 1) << 3) + ((ch->unit & 2) << 8);
4714 int offset1 = ((ch->unit & 1) << 6) + ((ch->unit & 2) << 8);
4715
4716 /* do we have any PHY events ? */
4717 if (ctlr->chip->max_dma >= ATA_SA150 &&
4718 (ATA_INL(ctlr->r_res2, 0x10 + offset0) & 0x00000010))
4719 ata_sata_phy_check_events(dev);
4720
4721 if (ATA_INL(ctlr->r_res2, 0xa0 + offset1) & 0x00000800)
4722 return ata_pci_status(dev);
4723 else
4724 return 0;
4725 }
4726
4727 static void
4728 ata_sii_reset(device_t dev)
4729 {
4730 if (ata_sata_phy_reset(dev))
4731 ata_generic_reset(dev);
4732 }
4733
4734 static void
4735 ata_sii_setmode(device_t dev, int mode)
4736 {
4737 device_t gparent = GRANDPARENT(dev);
4738 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4739 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4740 struct ata_device *atadev = device_get_softc(dev);
4741 int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
4742 int mreg = ch->unit ? 0x84 : 0x80;
4743 int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
4744 int mval = pci_read_config(gparent, mreg, 1) & ~mask;
4745 int error;
4746
4747 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4748
4749 if (ctlr->chip->cfg2 & SIISETCLK) {
4750 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
4751 (ch->unit ? 0x02 : 0x01))) {
4752 ata_print_cable(dev, "controller");
4753 mode = ATA_UDMA2;
4754 }
4755 }
4756 else
4757 mode = ata_check_80pin(dev, mode);
4758
4759 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4760
4761 if (bootverbose)
4762 device_printf(dev, "%ssetting %s on %s chip\n",
4763 (error) ? "FAILURE " : "",
4764 ata_mode2str(mode), ctlr->chip->text);
4765 if (error)
4766 return;
4767
4768 if (mode >= ATA_UDMA0) {
4769 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
4770 u_int8_t ureg = 0xac + rego;
4771
4772 pci_write_config(gparent, mreg,
4773 mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
4774 pci_write_config(gparent, ureg,
4775 (pci_read_config(gparent, ureg, 1) & ~0x3f) |
4776 udmatimings[mode & ATA_MODE_MASK], 1);
4777
4778 }
4779 else if (mode >= ATA_WDMA0) {
4780 u_int8_t dreg = 0xa8 + rego;
4781 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
4782
4783 pci_write_config(gparent, mreg,
4784 mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
4785 pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
4786
4787 }
4788 else {
4789 u_int8_t preg = 0xa4 + rego;
4790 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
4791
4792 pci_write_config(gparent, mreg,
4793 mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
4794 pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
4795 }
4796 atadev->mode = mode;
4797 }
4798
4799
4800 struct ata_siiprb_dma_prdentry {
4801 u_int64_t addr;
4802 u_int32_t count;
4803 u_int32_t control;
4804 } __packed;
4805
4806 struct ata_siiprb_ata_command {
4807 struct ata_siiprb_dma_prdentry prd[126];
4808 } __packed;
4809
4810 struct ata_siiprb_atapi_command {
4811 u_int8_t ccb[16];
4812 struct ata_siiprb_dma_prdentry prd[125];
4813 } __packed;
4814
4815 struct ata_siiprb_command {
4816 u_int16_t control;
4817 u_int16_t protocol_override;
4818 u_int32_t transfer_count;
4819 u_int8_t fis[24];
4820 union {
4821 struct ata_siiprb_ata_command ata;
4822 struct ata_siiprb_atapi_command atapi;
4823 } u;
4824 } __packed;
4825
4826 static int
4827 ata_siiprb_allocate(device_t dev)
4828 {
4829 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4830 struct ata_channel *ch = device_get_softc(dev);
4831 int offset = ch->unit * 0x2000;
4832
4833 /* set the SATA resources */
4834 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4835 ch->r_io[ATA_SSTATUS].offset = 0x1f04 + offset;
4836 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4837 ch->r_io[ATA_SERROR].offset = 0x1f08 + offset;
4838 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4839 ch->r_io[ATA_SCONTROL].offset = 0x1f00 + offset;
4840 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
4841 ch->r_io[ATA_SACTIVE].offset = 0x1f0c + offset;
4842
4843 ch->hw.begin_transaction = ata_siiprb_begin_transaction;
4844 ch->hw.end_transaction = ata_siiprb_end_transaction;
4845 ch->hw.status = ata_siiprb_status;
4846 ch->hw.command = NULL; /* not used here */
4847 return 0;
4848 }
4849
4850 static int
4851 ata_siiprb_status(device_t dev)
4852 {
4853 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4854 struct ata_channel *ch = device_get_softc(dev);
4855 u_int32_t action = ATA_INL(ctlr->r_res1, 0x0044);
4856 int offset = ch->unit * 0x2000;
4857
4858 if (action & (1 << ch->unit)) {
4859 u_int32_t istatus = ATA_INL(ctlr->r_res2, 0x1008 + offset);
4860
4861 /* do we have any PHY events ? */
4862 ata_sata_phy_check_events(dev);
4863
4864 /* clear interrupt(s) */
4865 ATA_OUTL(ctlr->r_res2, 0x1008 + offset, istatus);
4866
4867 /* do we have any device action ? */
4868 return (istatus & 0x00000003);
4869 }
4870 return 0;
4871 }
4872
4873 static int
4874 ata_siiprb_begin_transaction(struct ata_request *request)
4875 {
4876 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
4877 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4878 struct ata_siiprb_command *prb;
4879 struct ata_siiprb_dma_prdentry *prd;
4880 int offset = ch->unit * 0x2000;
4881 u_int64_t prb_bus;
4882 int tag = 0, dummy;
4883
4884 /* SOS XXX */
4885 if (request->u.ata.command == ATA_DEVICE_RESET) {
4886 request->result = 0;
4887 return ATA_OP_FINISHED;
4888 }
4889
4890 /* check for 48 bit access and convert if needed */
4891 ata_modify_if_48bit(request);
4892
4893 /* get a piece of the workspace for this request */
4894 prb = (struct ata_siiprb_command *)
4895 (ch->dma->work + (sizeof(struct ata_siiprb_command) * tag));
4896
4897 /* set basic prd options ata/atapi etc etc */
4898 bzero(prb, sizeof(struct ata_siiprb_command));
4899
4900 /* setup the FIS for this request */
4901 if (!ata_request2fis_h2d(request, &prb->fis[0])) {
4902 device_printf(request->dev, "setting up SATA FIS failed\n");
4903 request->result = EIO;
4904 return ATA_OP_FINISHED;
4905 }
4906
4907 /* setup transfer type */
4908 if (request->flags & ATA_R_ATAPI) {
4909 struct ata_device *atadev = device_get_softc(request->dev);
4910
4911 bcopy(request->u.atapi.ccb, prb->u.atapi.ccb, 16);
4912 if ((atadev->param.config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12)
4913 ATA_OUTL(ctlr->r_res2, 0x1004 + offset, 0x00000020);
4914 else
4915 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000020);
4916 if (request->flags & ATA_R_READ)
4917 prb->control = htole16(0x0010);
4918 if (request->flags & ATA_R_WRITE)
4919 prb->control = htole16(0x0020);
4920 prd = &prb->u.atapi.prd[0];
4921 }
4922 else
4923 prd = &prb->u.ata.prd[0];
4924
4925 /* if request moves data setup and load SG list */
4926 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
4927 if (ch->dma->load(ch->dev, request->data, request->bytecount,
4928 request->flags & ATA_R_READ, prd, &dummy)) {
4929 device_printf(request->dev, "setting up DMA failed\n");
4930 request->result = EIO;
4931 return ATA_OP_FINISHED;
4932 }
4933 }
4934
4935 /* activate the prb */
4936 prb_bus = ch->dma->work_bus + (sizeof(struct ata_siiprb_command) * tag);
4937 ATA_OUTL(ctlr->r_res2,
4938 0x1c00 + offset + (tag * sizeof(u_int64_t)), prb_bus);
4939 ATA_OUTL(ctlr->r_res2,
4940 0x1c04 + offset + (tag * sizeof(u_int64_t)), prb_bus>>32);
4941
4942 /* start the timeout */
4943 callout_reset(&request->callout, request->timeout * hz,
4944 (timeout_t*)ata_timeout, request);
4945 return ATA_OP_CONTINUES;
4946 }
4947
4948 static int
4949 ata_siiprb_end_transaction(struct ata_request *request)
4950 {
4951 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
4952 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4953 struct ata_siiprb_command *prb;
4954 int offset = ch->unit * 0x2000;
4955 int error, timeout, tag = 0;
4956
4957 /* kill the timeout */
4958 callout_stop(&request->callout);
4959
4960 prb = (struct ata_siiprb_command *)
4961 ((u_int8_t *)rman_get_virtual(ctlr->r_res2) + (tag << 7) + offset);
4962
4963 /* any controller errors flagged ? */
4964 if ((error = ATA_INL(ctlr->r_res2, 0x1024 + offset))) {
4965 if (bootverbose)
4966 printf("ata_siiprb_end_transaction %s error=%08x\n",
4967 ata_cmd2str(request), error);
4968
4969 /* if device error status get details */
4970 if (error == 1 || error == 2) {
4971 request->status = prb->fis[2];
4972 if (request->status & ATA_S_ERROR)
4973 request->error = prb->fis[3];
4974 }
4975
4976 /* SOS XXX handle other controller errors here */
4977
4978 /* initialize port */
4979 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000004);
4980
4981 /* poll for port ready */
4982 for (timeout = 0; timeout < 1000; timeout++) {
4983 DELAY(1000);
4984 if (ATA_INL(ctlr->r_res2, 0x1008 + offset) & 0x00040000)
4985 break;
4986 }
4987 if (bootverbose) {
4988 if (timeout >= 1000)
4989 device_printf(ch->dev, "port initialize timeout\n");
4990 else
4991 device_printf(ch->dev, "port initialize time=%dms\n", timeout);
4992 }
4993 }
4994
4995 /* update progress */
4996 if (!(request->status & ATA_S_ERROR) && !(request->flags & ATA_R_TIMEOUT)) {
4997 if (request->flags & ATA_R_READ)
4998 request->donecount = prb->transfer_count;
4999 else
5000 request->donecount = request->bytecount;
5001 }
5002
5003 /* release SG list etc */
5004 ch->dma->unload(ch->dev);
5005
5006 return ATA_OP_FINISHED;
5007 }
5008
5009 static void
5010 ata_siiprb_reset(device_t dev)
5011 {
5012 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5013 struct ata_channel *ch = device_get_softc(dev);
5014 int offset = ch->unit * 0x2000;
5015 struct ata_siiprb_command *prb;
5016 u_int64_t prb_bus;
5017 u_int32_t status, signature;
5018 int timeout, tag = 0;
5019
5020 /* reset channel HW */
5021 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000001);
5022 DELAY(1000);
5023 ATA_OUTL(ctlr->r_res2, 0x1004 + offset, 0x00000001);
5024 DELAY(10000);
5025
5026 /* poll for channel ready */
5027 for (timeout = 0; timeout < 1000; timeout++) {
5028 if ((status = ATA_INL(ctlr->r_res2, 0x1008 + offset)) & 0x00040000)
5029 break;
5030 DELAY(1000);
5031 }
5032
5033 if (bootverbose) {
5034 if (timeout >= 1000)
5035 device_printf(ch->dev, "channel HW reset timeout\n");
5036 else
5037 device_printf(ch->dev, "channel HW reset time=%dms\n", timeout);
5038 }
5039
5040 /* reset phy */
5041 if (!ata_sata_phy_reset(dev)) {
5042 if (bootverbose)
5043 device_printf(ch->dev, "phy reset found no device\n");
5044 ch->devices = 0;
5045 goto finish;
5046 }
5047
5048 /* get a piece of the workspace for a soft reset request */
5049 prb = (struct ata_siiprb_command *)
5050 (ch->dma->work + (sizeof(struct ata_siiprb_command) * tag));
5051 bzero(prb, sizeof(struct ata_siiprb_command));
5052 prb->control = htole16(0x0080);
5053
5054 /* activate the soft reset prb */
5055 prb_bus = ch->dma->work_bus + (sizeof(struct ata_siiprb_command) * tag);
5056 ATA_OUTL(ctlr->r_res2,
5057 0x1c00 + offset + (tag * sizeof(u_int64_t)), prb_bus);
5058 ATA_OUTL(ctlr->r_res2,
5059 0x1c04 + offset + (tag * sizeof(u_int64_t)), prb_bus>>32);
5060
5061 /* poll for command finished */
5062 for (timeout = 0; timeout < 10000; timeout++) {
5063 DELAY(1000);
5064 if ((status = ATA_INL(ctlr->r_res2, 0x1008 + offset)) & 0x00010000)
5065 break;
5066 }
5067 if (timeout >= 1000) {
5068 device_printf(ch->dev, "reset timeout - no device found\n");
5069 ch->devices = 0;
5070 goto finish;
5071 }
5072 if (bootverbose)
5073 device_printf(ch->dev, "soft reset exec time=%dms status=%08x\n",
5074 timeout, status);
5075
5076 /* find out whats there */
5077 prb = (struct ata_siiprb_command *)
5078 ((u_int8_t *)rman_get_virtual(ctlr->r_res2) + (tag << 7) + offset);
5079 signature =
5080 prb->fis[12]|(prb->fis[4]<<8)|(prb->fis[5]<<16)|(prb->fis[6]<<24);
5081 if (bootverbose)
5082 device_printf(ch->dev, "SIGNATURE=%08x\n", signature);
5083 switch (signature) {
5084 case 0x00000101:
5085 ch->devices = ATA_ATA_MASTER;
5086 break;
5087 case 0x96690101:
5088 ch->devices = ATA_PORTMULTIPLIER;
5089 device_printf(ch->dev, "Portmultipliers not supported yet\n");
5090 ch->devices = 0;
5091 break;
5092 case 0xeb140101:
5093 ch->devices = ATA_ATAPI_MASTER;
5094 break;
5095 default:
5096 ch->devices = 0;
5097 }
5098 if (bootverbose)
5099 device_printf(dev, "siiprb_reset devices=0x%b\n", ch->devices,
5100 "\2\4ATAPI_SLAVE\3ATAPI_MASTER\2ATA_SLAVE\1ATA_MASTER");
5101
5102 finish:
5103 /* clear interrupt(s) */
5104 ATA_OUTL(ctlr->r_res2, 0x1008 + offset, 0x000008ff);
5105
5106 /* require explicit interrupt ack */
5107 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000008);
5108
5109 /* 64bit mode */
5110 ATA_OUTL(ctlr->r_res2, 0x1004 + offset, 0x00000400);
5111
5112 /* enable interrupts wanted */
5113 ATA_OUTL(ctlr->r_res2, 0x1010 + offset, 0x000000ff);
5114 }
5115
5116 static void
5117 ata_siiprb_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
5118 {
5119 struct ata_dmasetprd_args *args = xsc;
5120 struct ata_siiprb_dma_prdentry *prd = args->dmatab;
5121 int i;
5122
5123 if ((args->error = error))
5124 return;
5125
5126 for (i = 0; i < nsegs; i++) {
5127 prd[i].addr = htole64(segs[i].ds_addr);
5128 prd[i].count = htole32(segs[i].ds_len);
5129 }
5130 prd[i - 1].control = htole32(ATA_DMA_EOT);
5131 KASSERT(nsegs <= ATA_DMA_ENTRIES, ("too many DMA segment entries\n"));
5132 args->nsegs = nsegs;
5133 }
5134
5135 static void
5136 ata_siiprb_dmainit(device_t dev)
5137 {
5138 struct ata_channel *ch = device_get_softc(dev);
5139
5140 ata_dmainit(dev);
5141 if (ch->dma) {
5142 /* note start and stop are not used here */
5143 ch->dma->setprd = ata_siiprb_dmasetprd;
5144 ch->dma->max_address = BUS_SPACE_MAXADDR;
5145 }
5146 }
5147
5148
5149 /*
5150 * Silicon Integrated Systems Corp. (SiS) chipset support functions
5151 */
5152 int
5153 ata_sis_ident(device_t dev)
5154 {
5155 struct ata_pci_controller *ctlr = device_get_softc(dev);
5156 struct ata_chip_id *idx;
5157 static struct ata_chip_id ids[] =
5158 {{ ATA_SIS182, 0x00, SISSATA, 0, ATA_SA150, "182" }, /* south */
5159 { ATA_SIS181, 0x00, SISSATA, 0, ATA_SA150, "181" }, /* south */
5160 { ATA_SIS180, 0x00, SISSATA, 0, ATA_SA150, "180" }, /* south */
5161 { ATA_SIS965, 0x00, SIS133NEW, 0, ATA_UDMA6, "965" }, /* south */
5162 { ATA_SIS964, 0x00, SIS133NEW, 0, ATA_UDMA6, "964" }, /* south */
5163 { ATA_SIS963, 0x00, SIS133NEW, 0, ATA_UDMA6, "963" }, /* south */
5164 { ATA_SIS962, 0x00, SIS133NEW, 0, ATA_UDMA6, "962" }, /* south */
5165
5166 { ATA_SIS745, 0x00, SIS100NEW, 0, ATA_UDMA5, "745" }, /* 1chip */
5167 { ATA_SIS735, 0x00, SIS100NEW, 0, ATA_UDMA5, "735" }, /* 1chip */
5168 { ATA_SIS733, 0x00, SIS100NEW, 0, ATA_UDMA5, "733" }, /* 1chip */
5169 { ATA_SIS730, 0x00, SIS100OLD, 0, ATA_UDMA5, "730" }, /* 1chip */
5170
5171 { ATA_SIS635, 0x00, SIS100NEW, 0, ATA_UDMA5, "635" }, /* 1chip */
5172 { ATA_SIS633, 0x00, SIS100NEW, 0, ATA_UDMA5, "633" }, /* unknown */
5173 { ATA_SIS630, 0x30, SIS100OLD, 0, ATA_UDMA5, "630S"}, /* 1chip */
5174 { ATA_SIS630, 0x00, SIS66, 0, ATA_UDMA4, "630" }, /* 1chip */
5175 { ATA_SIS620, 0x00, SIS66, 0, ATA_UDMA4, "620" }, /* 1chip */
5176
5177 { ATA_SIS550, 0x00, SIS66, 0, ATA_UDMA5, "550" },
5178 { ATA_SIS540, 0x00, SIS66, 0, ATA_UDMA4, "540" },
5179 { ATA_SIS530, 0x00, SIS66, 0, ATA_UDMA4, "530" },
5180
5181 { ATA_SIS5513, 0xc2, SIS33, 1, ATA_UDMA2, "5513" },
5182 { ATA_SIS5513, 0x00, SIS33, 1, ATA_WDMA2, "5513" },
5183 { 0, 0, 0, 0, 0, 0 }};
5184 char buffer[64];
5185 int found = 0;
5186
5187 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
5188 return ENXIO;
5189
5190 if (idx->cfg2 && !found) {
5191 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
5192
5193 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
5194 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
5195 found = 1;
5196 idx->cfg1 = SIS133NEW;
5197 idx->max_dma = ATA_UDMA6;
5198 sprintf(buffer, "SiS 962/963 %s controller",
5199 ata_mode2str(idx->max_dma));
5200 }
5201 pci_write_config(dev, 0x57, reg57, 1);
5202 }
5203 if (idx->cfg2 && !found) {
5204 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
5205
5206 pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
5207 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
5208 struct ata_chip_id id[] =
5209 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
5210
5211 found = 1;
5212 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
5213 idx->cfg1 = SIS133OLD;
5214 idx->max_dma = ATA_UDMA6;
5215 }
5216 else {
5217 idx->cfg1 = SIS100NEW;
5218 idx->max_dma = ATA_UDMA5;
5219 }
5220 sprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
5221 }
5222 pci_write_config(dev, 0x4a, reg4a, 1);
5223 }
5224 if (!found)
5225 sprintf(buffer,"SiS %s %s controller",
5226 idx->text, ata_mode2str(idx->max_dma));
5227
5228 device_set_desc_copy(dev, buffer);
5229 ctlr->chip = idx;
5230 ctlr->chipinit = ata_sis_chipinit;
5231 return 0;
5232 }
5233
5234 static int
5235 ata_sis_chipinit(device_t dev)
5236 {
5237 struct ata_pci_controller *ctlr = device_get_softc(dev);
5238
5239 if (ata_setup_interrupt(dev))
5240 return ENXIO;
5241
5242 switch (ctlr->chip->cfg1) {
5243 case SIS33:
5244 break;
5245 case SIS66:
5246 case SIS100OLD:
5247 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
5248 break;
5249 case SIS100NEW:
5250 case SIS133OLD:
5251 pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
5252 break;
5253 case SIS133NEW:
5254 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
5255 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
5256 break;
5257 case SISSATA:
5258 ctlr->r_type2 = SYS_RES_IOPORT;
5259 ctlr->r_rid2 = PCIR_BAR(5);
5260 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
5261 &ctlr->r_rid2, RF_ACTIVE))) {
5262 ctlr->allocate = ata_sis_allocate;
5263 ctlr->reset = ata_sis_reset;
5264
5265 /* enable PCI interrupt */
5266 pci_write_config(dev, PCIR_COMMAND,
5267 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
5268 }
5269 ctlr->setmode = ata_sata_setmode;
5270 return 0;
5271 default:
5272 return ENXIO;
5273 }
5274 ctlr->setmode = ata_sis_setmode;
5275 return 0;
5276 }
5277
5278 static int
5279 ata_sis_allocate(device_t dev)
5280 {
5281 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5282 struct ata_channel *ch = device_get_softc(dev);
5283 int offset = ch->unit << ((ctlr->chip->chipid == ATA_SIS182) ? 5 : 6);
5284
5285 /* setup the usual register normal pci style */
5286 if (ata_pci_allocate(dev))
5287 return ENXIO;
5288
5289 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
5290 ch->r_io[ATA_SSTATUS].offset = 0x00 + offset;
5291 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
5292 ch->r_io[ATA_SERROR].offset = 0x04 + offset;
5293 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
5294 ch->r_io[ATA_SCONTROL].offset = 0x08 + offset;
5295 ch->flags |= ATA_NO_SLAVE;
5296
5297 /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
5298 /* XXX SOS unknown how to enable PHY state change interrupt */
5299 return 0;
5300 }
5301
5302 static void
5303 ata_sis_reset(device_t dev)
5304 {
5305 if (ata_sata_phy_reset(dev))
5306 ata_generic_reset(dev);
5307 }
5308
5309 static void
5310 ata_sis_setmode(device_t dev, int mode)
5311 {
5312 device_t gparent = GRANDPARENT(dev);
5313 struct ata_pci_controller *ctlr = device_get_softc(gparent);
5314 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5315 struct ata_device *atadev = device_get_softc(dev);
5316 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
5317 int error;
5318
5319 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
5320
5321 if (ctlr->chip->cfg1 == SIS133NEW) {
5322 if (mode > ATA_UDMA2 &&
5323 pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
5324 ata_print_cable(dev, "controller");
5325 mode = ATA_UDMA2;
5326 }
5327 }
5328 else {
5329 if (mode > ATA_UDMA2 &&
5330 pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
5331 ata_print_cable(dev, "controller");
5332 mode = ATA_UDMA2;
5333 }
5334 }
5335
5336 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5337
5338 if (bootverbose)
5339 device_printf(dev, "%ssetting %s on %s chip\n",
5340 (error) ? "FAILURE " : "",
5341 ata_mode2str(mode), ctlr->chip->text);
5342 if (!error) {
5343 switch (ctlr->chip->cfg1) {
5344 case SIS133NEW: {
5345 u_int32_t timings[] =
5346 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
5347 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
5348 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
5349 u_int32_t reg;
5350
5351 reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
5352 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
5353 break;
5354 }
5355 case SIS133OLD: {
5356 u_int16_t timings[] =
5357 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
5358 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
5359
5360 u_int16_t reg = 0x40 + (devno << 1);
5361
5362 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
5363 break;
5364 }
5365 case SIS100NEW: {
5366 u_int16_t timings[] =
5367 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
5368 0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
5369 u_int16_t reg = 0x40 + (devno << 1);
5370
5371 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
5372 break;
5373 }
5374 case SIS100OLD:
5375 case SIS66:
5376 case SIS33: {
5377 u_int16_t timings[] =
5378 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
5379 0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
5380 u_int16_t reg = 0x40 + (devno << 1);
5381
5382 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
5383 break;
5384 }
5385 }
5386 atadev->mode = mode;
5387 }
5388 }
5389
5390
5391 /* VIA Technologies Inc. chipset support functions */
5392 int
5393 ata_via_ident(device_t dev)
5394 {
5395 struct ata_pci_controller *ctlr = device_get_softc(dev);
5396 static struct ata_chip_id ids[] =
5397 {{ ATA_VIA82C586, 0x02, VIA33, 0x00, ATA_UDMA2, "82C586B" },
5398 { ATA_VIA82C586, 0x00, VIA33, 0x00, ATA_WDMA2, "82C586" },
5399 { ATA_VIA82C596, 0x12, VIA66, VIACLK, ATA_UDMA4, "82C596B" },
5400 { ATA_VIA82C596, 0x00, VIA33, 0x00, ATA_UDMA2, "82C596" },
5401 { ATA_VIA82C686, 0x40, VIA100, VIABUG, ATA_UDMA5, "82C686B"},
5402 { ATA_VIA82C686, 0x10, VIA66, VIACLK, ATA_UDMA4, "82C686A" },
5403 { ATA_VIA82C686, 0x00, VIA33, 0x00, ATA_UDMA2, "82C686" },
5404 { ATA_VIA8231, 0x00, VIA100, VIABUG, ATA_UDMA5, "8231" },
5405 { ATA_VIA8233, 0x00, VIA100, 0x00, ATA_UDMA5, "8233" },
5406 { ATA_VIA8233C, 0x00, VIA100, 0x00, ATA_UDMA5, "8233C" },
5407 { ATA_VIA8233A, 0x00, VIA133, 0x00, ATA_UDMA6, "8233A" },
5408 { ATA_VIA8235, 0x00, VIA133, 0x00, ATA_UDMA6, "8235" },
5409 { ATA_VIA8237, 0x00, VIA133, 0x00, ATA_UDMA6, "8237" },
5410 { ATA_VIA8237A, 0x00, VIA133, 0x00, ATA_UDMA6, "8237A" },
5411 { ATA_VIA8237S, 0x00, VIA133, 0x00, ATA_UDMA6, "8237S" },
5412 { ATA_VIA8251, 0x00, VIA133, 0x00, ATA_UDMA6, "8251" },
5413 { 0, 0, 0, 0, 0, 0 }};
5414 static struct ata_chip_id new_ids[] =
5415 {{ ATA_VIA6410, 0x00, 0, 0x00, ATA_UDMA6, "6410" },
5416 { ATA_VIA6420, 0x00, 7, 0x00, ATA_SA150, "6420" },
5417 { ATA_VIA6421, 0x00, 6, VIABAR, ATA_SA150, "6421" },
5418 { ATA_VIA8237A, 0x00, 7, 0x00, ATA_SA150, "8237A" },
5419 { ATA_VIA8237S, 0x00, 7, 0x00, ATA_SA150, "8237S" },
5420 { ATA_VIA8251, 0x00, 0, VIAAHCI, ATA_SA300, "8251" },
5421 { 0, 0, 0, 0, 0, 0 }};
5422
5423 if (pci_get_devid(dev) == ATA_VIA82C571) {
5424 if (!(ctlr->chip = ata_find_chip(dev, ids, -99)))
5425 return ENXIO;
5426 }
5427 else {
5428 if (!(ctlr->chip = ata_match_chip(dev, new_ids)))
5429 return ENXIO;
5430 }
5431
5432 ata_set_desc(dev);
5433 ctlr->chipinit = ata_via_chipinit;
5434 return 0;
5435 }
5436
5437 static int
5438 ata_via_chipinit(device_t dev)
5439 {
5440 struct ata_pci_controller *ctlr = device_get_softc(dev);
5441
5442 if (ata_setup_interrupt(dev))
5443 return ENXIO;
5444
5445 if (ctlr->chip->max_dma >= ATA_SA150) {
5446 /* do we have AHCI capability ? */
5447 if ((ctlr->chip->cfg2 == VIAAHCI) && ata_ahci_chipinit(dev) != ENXIO)
5448 return 0;
5449
5450 ctlr->r_type2 = SYS_RES_IOPORT;
5451 ctlr->r_rid2 = PCIR_BAR(5);
5452 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
5453 &ctlr->r_rid2, RF_ACTIVE))) {
5454 ctlr->allocate = ata_via_allocate;
5455 ctlr->reset = ata_via_reset;
5456
5457 /* enable PCI interrupt */
5458 pci_write_config(dev, PCIR_COMMAND,
5459 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
5460 }
5461
5462 if (ctlr->chip->cfg2 & VIABAR) {
5463 ctlr->channels = 3;
5464 ctlr->setmode = ata_via_setmode;
5465 }
5466 else
5467 ctlr->setmode = ata_sata_setmode;
5468 return 0;
5469 }
5470
5471 /* prepare for ATA-66 on the 82C686a and 82C596b */
5472 if (ctlr->chip->cfg2 & VIACLK)
5473 pci_write_config(dev, 0x50, 0x030b030b, 4);
5474
5475 /* the southbridge might need the data corruption fix */
5476 if (ctlr->chip->cfg2 & VIABUG)
5477 ata_via_southbridge_fixup(dev);
5478
5479 /* set fifo configuration half'n'half */
5480 pci_write_config(dev, 0x43,
5481 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
5482
5483 /* set status register read retry */
5484 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
5485
5486 /* set DMA read & end-of-sector fifo flush */
5487 pci_write_config(dev, 0x46,
5488 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
5489
5490 /* set sector size */
5491 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
5492 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
5493
5494 ctlr->setmode = ata_via_family_setmode;
5495 return 0;
5496 }
5497
5498 static int
5499 ata_via_allocate(device_t dev)
5500 {
5501 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5502 struct ata_channel *ch = device_get_softc(dev);
5503
5504 /* newer SATA chips has resources in one BAR for each channel */
5505 if (ctlr->chip->cfg2 & VIABAR) {
5506 struct resource *r_io;
5507 int i, rid;
5508
5509 rid = PCIR_BAR(ch->unit);
5510 if (!(r_io = bus_alloc_resource_any(device_get_parent(dev),
5511 SYS_RES_IOPORT,
5512 &rid, RF_ACTIVE)))
5513 return ENXIO;
5514
5515 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
5516 ch->r_io[i].res = r_io;
5517 ch->r_io[i].offset = i;
5518 }
5519 ch->r_io[ATA_CONTROL].res = r_io;
5520 ch->r_io[ATA_CONTROL].offset = 2 + ATA_IOSIZE;
5521 ch->r_io[ATA_IDX_ADDR].res = r_io;
5522 ata_default_registers(dev);
5523 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
5524 ch->r_io[i].res = ctlr->r_res1;
5525 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
5526 }
5527 ata_pci_hw(dev);
5528 if (ch->unit >= 2)
5529 return 0;
5530 }
5531 else {
5532 /* setup the usual register normal pci style */
5533 if (ata_pci_allocate(dev))
5534 return ENXIO;
5535 }
5536
5537 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
5538 ch->r_io[ATA_SSTATUS].offset = (ch->unit << ctlr->chip->cfg1);
5539 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
5540 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << ctlr->chip->cfg1);
5541 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
5542 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << ctlr->chip->cfg1);
5543 ch->flags |= ATA_NO_SLAVE;
5544
5545 /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
5546 /* XXX SOS unknown how to enable PHY state change interrupt */
5547 return 0;
5548 }
5549
5550 static void
5551 ata_via_reset(device_t dev)
5552 {
5553 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5554 struct ata_channel *ch = device_get_softc(dev);
5555
5556 if ((ctlr->chip->cfg2 & VIABAR) && (ch->unit > 1))
5557 ata_generic_reset(dev);
5558 else
5559 if (ata_sata_phy_reset(dev))
5560 ata_generic_reset(dev);
5561 }
5562
5563 static void
5564 ata_via_setmode(device_t dev, int mode)
5565 {
5566 device_t gparent = GRANDPARENT(dev);
5567 struct ata_pci_controller *ctlr = device_get_softc(gparent);
5568 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5569 struct ata_device *atadev = device_get_softc(dev);
5570 int error;
5571
5572 if ((ctlr->chip->cfg2 & VIABAR) && (ch->unit > 1)) {
5573 u_int8_t pio_timings[] = { 0xa8, 0x65, 0x65, 0x32, 0x20,
5574 0x65, 0x32, 0x20,
5575 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
5576 u_int8_t dma_timings[] = { 0xee, 0xe8, 0xe6, 0xe4, 0xe2, 0xe1, 0xe0 };
5577
5578 mode = ata_check_80pin(dev, ata_limit_mode(dev, mode, ATA_UDMA6));
5579 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5580 if (bootverbose)
5581 device_printf(dev, "%ssetting %s on %s chip\n",
5582 (error) ? "FAILURE " : "", ata_mode2str(mode),
5583 ctlr->chip->text);
5584 if (!error) {
5585 pci_write_config(gparent, 0xab, pio_timings[ata_mode2idx(mode)], 1);
5586 if (mode >= ATA_UDMA0)
5587 pci_write_config(gparent, 0xb3,
5588 dma_timings[mode & ATA_MODE_MASK], 1);
5589 atadev->mode = mode;
5590 }
5591 }
5592 else
5593 ata_sata_setmode(dev, mode);
5594 }
5595
5596 static void
5597 ata_via_southbridge_fixup(device_t dev)
5598 {
5599 device_t *children;
5600 int nchildren, i;
5601
5602 if (device_get_children(device_get_parent(dev), &children, &nchildren))
5603 return;
5604
5605 for (i = 0; i < nchildren; i++) {
5606 if (pci_get_devid(children[i]) == ATA_VIA8363 ||
5607 pci_get_devid(children[i]) == ATA_VIA8371 ||
5608 pci_get_devid(children[i]) == ATA_VIA8662 ||
5609 pci_get_devid(children[i]) == ATA_VIA8361) {
5610 u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
5611
5612 if ((reg76 & 0xf0) != 0xd0) {
5613 device_printf(dev,
5614 "Correcting VIA config for southbridge data corruption bug\n");
5615 pci_write_config(children[i], 0x75, 0x80, 1);
5616 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
5617 }
5618 break;
5619 }
5620 }
5621 free(children, M_TEMP);
5622 }
5623
5624
5625 /* common code for VIA, AMD & nVidia */
5626 static void
5627 ata_via_family_setmode(device_t dev, int mode)
5628 {
5629 device_t gparent = GRANDPARENT(dev);
5630 struct ata_pci_controller *ctlr = device_get_softc(gparent);
5631 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5632 struct ata_device *atadev = device_get_softc(dev);
5633 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
5634 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
5635 int modes[][7] = {
5636 { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 }, /* VIA ATA33 */
5637 { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 }, /* VIA ATA66 */
5638 { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 }, /* VIA ATA100 */
5639 { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 }, /* VIA ATA133 */
5640 { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }}; /* AMD/nVIDIA */
5641 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
5642 int reg = 0x53 - devno;
5643 int error;
5644
5645 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
5646
5647 if (ctlr->chip->cfg2 & AMDCABLE) {
5648 if (mode > ATA_UDMA2 &&
5649 !(pci_read_config(gparent, 0x42, 1) & (1 << devno))) {
5650 ata_print_cable(dev, "controller");
5651 mode = ATA_UDMA2;
5652 }
5653 }
5654 else
5655 mode = ata_check_80pin(dev, mode);
5656
5657 if (ctlr->chip->cfg2 & NVIDIA)
5658 reg += 0x10;
5659
5660 if (ctlr->chip->cfg1 != VIA133)
5661 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
5662
5663 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5664
5665 if (bootverbose)
5666 device_printf(dev, "%ssetting %s on %s chip\n",
5667 (error) ? "FAILURE " : "", ata_mode2str(mode),
5668 ctlr->chip->text);
5669 if (!error) {
5670 if (mode >= ATA_UDMA0)
5671 pci_write_config(gparent, reg,
5672 modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
5673 else
5674 pci_write_config(gparent, reg, 0x8b, 1);
5675 atadev->mode = mode;
5676 }
5677 }
5678
5679
5680 /* misc functions */
5681 static void
5682 ata_set_desc(device_t dev)
5683 {
5684 struct ata_pci_controller *ctlr = device_get_softc(dev);
5685 char buffer[128];
5686
5687 sprintf(buffer, "%s %s %s controller",
5688 ata_pcivendor2str(dev), ctlr->chip->text,
5689 ata_mode2str(ctlr->chip->max_dma));
5690 device_set_desc_copy(dev, buffer);
5691 }
5692
5693 static struct ata_chip_id *
5694 ata_match_chip(device_t dev, struct ata_chip_id *index)
5695 {
5696 while (index->chipid != 0) {
5697 if (pci_get_devid(dev) == index->chipid &&
5698 pci_get_revid(dev) >= index->chiprev)
5699 return index;
5700 index++;
5701 }
5702 return NULL;
5703 }
5704
5705 static struct ata_chip_id *
5706 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
5707 {
5708 device_t *children;
5709 int nchildren, i;
5710
5711 if (device_get_children(device_get_parent(dev), &children, &nchildren))
5712 return 0;
5713
5714 while (index->chipid != 0) {
5715 for (i = 0; i < nchildren; i++) {
5716 if (((slot >= 0 && pci_get_slot(children[i]) == slot) ||
5717 (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
5718 pci_get_devid(children[i]) == index->chipid &&
5719 pci_get_revid(children[i]) >= index->chiprev) {
5720 free(children, M_TEMP);
5721 return index;
5722 }
5723 }
5724 index++;
5725 }
5726 free(children, M_TEMP);
5727 return NULL;
5728 }
5729
5730 static int
5731 ata_setup_interrupt(device_t dev)
5732 {
5733 struct ata_pci_controller *ctlr = device_get_softc(dev);
5734 int rid = ATA_IRQ_RID;
5735
5736 if (!ata_legacy(dev)) {
5737 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
5738 RF_SHAREABLE | RF_ACTIVE))) {
5739 device_printf(dev, "unable to map interrupt\n");
5740 return ENXIO;
5741 }
5742 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
5743 ata_generic_intr, ctlr, &ctlr->handle))) {
5744 /* SOS XXX release r_irq */
5745 device_printf(dev, "unable to setup interrupt\n");
5746 return ENXIO;
5747 }
5748 }
5749 return 0;
5750 }
5751
5752 struct ata_serialize {
5753 struct mtx locked_mtx;
5754 int locked_ch;
5755 int restart_ch;
5756 };
5757
5758 static int
5759 ata_serialize(device_t dev, int flags)
5760 {
5761 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5762 struct ata_channel *ch = device_get_softc(dev);
5763 struct ata_serialize *serial;
5764 static int inited = 0;
5765 int res;
5766
5767 if (!inited) {
5768 serial = malloc(sizeof(struct ata_serialize),
5769 M_TEMP, M_NOWAIT | M_ZERO);
5770 mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF);
5771 serial->locked_ch = -1;
5772 serial->restart_ch = -1;
5773 device_set_ivars(ctlr->dev, serial);
5774 inited = 1;
5775 }
5776 else
5777 serial = device_get_ivars(ctlr->dev);
5778
5779 mtx_lock(&serial->locked_mtx);
5780 switch (flags) {
5781 case ATA_LF_LOCK:
5782 if (serial->locked_ch == -1)
5783 serial->locked_ch = ch->unit;
5784 if (serial->locked_ch != ch->unit)
5785 serial->restart_ch = ch->unit;
5786 break;
5787
5788 case ATA_LF_UNLOCK:
5789 if (serial->locked_ch == ch->unit) {
5790 serial->locked_ch = -1;
5791 if (serial->restart_ch != -1) {
5792 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
5793 serial->restart_ch = -1;
5794 mtx_unlock(&serial->locked_mtx);
5795 ata_start(ch->dev);
5796 return -1;
5797 }
5798 }
5799 }
5800 break;
5801
5802 case ATA_LF_WHICH:
5803 break;
5804 }
5805 res = serial->locked_ch;
5806 mtx_unlock(&serial->locked_mtx);
5807 return res;
5808 }
5809
5810 static void
5811 ata_print_cable(device_t dev, u_int8_t *who)
5812 {
5813 device_printf(dev,
5814 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
5815 }
5816
5817 static int
5818 ata_atapi(device_t dev)
5819 {
5820 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5821 struct ata_device *atadev = device_get_softc(dev);
5822
5823 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
5824 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
5825 }
5826
5827 static int
5828 ata_check_80pin(device_t dev, int mode)
5829 {
5830 struct ata_device *atadev = device_get_softc(dev);
5831
5832 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
5833 ata_print_cable(dev, "device");
5834 mode = ATA_UDMA2;
5835 }
5836 return mode;
5837 }
5838
5839 static int
5840 ata_mode2idx(int mode)
5841 {
5842 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
5843 return (mode & ATA_MODE_MASK) + 8;
5844 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
5845 return (mode & ATA_MODE_MASK) + 5;
5846 return (mode & ATA_MODE_MASK) - ATA_PIO0;
5847 }
Cache object: d3a3897b93f32934c4e1436522782b0d
|