1183724Ssos/*-
2230132Suqs * Copyright (c) 1998 - 2008 S��ren Schmidt <sos@FreeBSD.org>
3183724Ssos * All rights reserved.
4183724Ssos *
5183724Ssos * Redistribution and use in source and binary forms, with or without
6183724Ssos * modification, are permitted provided that the following conditions
7183724Ssos * are met:
8183724Ssos * 1. Redistributions of source code must retain the above copyright
9183724Ssos *    notice, this list of conditions and the following disclaimer,
10183724Ssos *    without modification, immediately at the beginning of the file.
11183724Ssos * 2. Redistributions in binary form must reproduce the above copyright
12183724Ssos *    notice, this list of conditions and the following disclaimer in the
13183724Ssos *    documentation and/or other materials provided with the distribution.
14183724Ssos *
15183724Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16183724Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17183724Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18183724Ssos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19183724Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20183724Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21183724Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22183724Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23183724Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24183724Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25183724Ssos */
26183724Ssos
27183724Ssos#include <sys/cdefs.h>
28183724Ssos__FBSDID("$FreeBSD$");
29183724Ssos
30183724Ssos#include <sys/param.h>
31183724Ssos#include <sys/module.h>
32183724Ssos#include <sys/systm.h>
33183724Ssos#include <sys/kernel.h>
34183724Ssos#include <sys/ata.h>
35183724Ssos#include <sys/bus.h>
36183724Ssos#include <sys/endian.h>
37183724Ssos#include <sys/malloc.h>
38183724Ssos#include <sys/lock.h>
39183724Ssos#include <sys/mutex.h>
40183724Ssos#include <sys/sema.h>
41183724Ssos#include <sys/taskqueue.h>
42183724Ssos#include <vm/uma.h>
43183724Ssos#include <machine/stdarg.h>
44183724Ssos#include <machine/resource.h>
45183724Ssos#include <machine/bus.h>
46183724Ssos#include <sys/rman.h>
47183724Ssos#include <dev/pci/pcivar.h>
48183724Ssos#include <dev/pci/pcireg.h>
49183724Ssos#include <dev/ata/ata-all.h>
50183724Ssos#include <dev/ata/ata-pci.h>
51183724Ssos#include <ata_if.h>
52183724Ssos
53183724Ssos/* local prototypes */
54200544Smariusstatic int ata_amd_ch_attach(device_t dev);
55183724Ssosstatic int ata_amd_chipinit(device_t dev);
56200171Smavstatic int ata_amd_setmode(device_t dev, int target, int mode);
57183724Ssos
58183724Ssos/* misc defines */
59183724Ssos#define AMD_BUG		0x01
60183724Ssos#define AMD_CABLE	0x02
61183724Ssos
62183724Ssos/*
63200817Smav * Advanced Micro Devices (AMD) chipset support functions
64183724Ssos */
65183724Ssosstatic int
66183724Ssosata_amd_probe(device_t dev)
67183724Ssos{
68183724Ssos    struct ata_pci_controller *ctlr = device_get_softc(dev);
69242625Sdim    static const struct ata_chip_id ids[] =
70183724Ssos    {{ ATA_AMD756,  0x00, 0x00,              0, ATA_UDMA4, "756" },
71183724Ssos     { ATA_AMD766,  0x00, AMD_CABLE|AMD_BUG, 0, ATA_UDMA5, "766" },
72183724Ssos     { ATA_AMD768,  0x00, AMD_CABLE,         0, ATA_UDMA5, "768" },
73183724Ssos     { ATA_AMD8111, 0x00, AMD_CABLE,         0, ATA_UDMA6, "8111" },
74183724Ssos     { ATA_AMD5536, 0x00, 0x00,              0, ATA_UDMA5, "CS5536" },
75183724Ssos     { 0, 0, 0, 0, 0, 0}};
76183724Ssos
77183724Ssos    if (pci_get_vendor(dev) != ATA_AMD_ID)
78183724Ssos	return ENXIO;
79183724Ssos
80183724Ssos    if (!(ctlr->chip = ata_match_chip(dev, ids)))
81183724Ssos	return ENXIO;
82183724Ssos
83183724Ssos    ata_set_desc(dev);
84183724Ssos    ctlr->chipinit = ata_amd_chipinit;
85280393Smav    return (BUS_PROBE_LOW_PRIORITY);
86183724Ssos}
87183724Ssos
88183724Ssosstatic int
89183724Ssosata_amd_chipinit(device_t dev)
90183724Ssos{
91183724Ssos    struct ata_pci_controller *ctlr = device_get_softc(dev);
92183724Ssos
93183724Ssos    if (ata_setup_interrupt(dev, ata_generic_intr))
94183724Ssos	return ENXIO;
95183724Ssos
96183724Ssos    /* disable/set prefetch, postwrite */
97183724Ssos    if (ctlr->chip->cfg1 & AMD_BUG)
98183724Ssos	pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) & 0x0f, 1);
99183724Ssos    else
100183724Ssos	pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
101183724Ssos
102200544Smarius    ctlr->ch_attach = ata_amd_ch_attach;
103183724Ssos    ctlr->setmode = ata_amd_setmode;
104183724Ssos    return 0;
105183724Ssos}
106183724Ssos
107200171Smavstatic int
108200171Smavata_amd_setmode(device_t dev, int target, int mode)
109183724Ssos{
110200171Smav	device_t parent = device_get_parent(dev);
111200171Smav	struct ata_pci_controller *ctlr = device_get_softc(parent);
112200171Smav	struct ata_channel *ch = device_get_softc(dev);
113200171Smav        int devno = (ch->unit << 1) + target;
114200171Smav	int piomode;
115233282Smarius	static const uint8_t timings[] =
116233282Smarius	    { 0xa8, 0x65, 0x42, 0x22, 0x20, 0xa8, 0x22, 0x20 };
117233282Smarius	static const uint8_t modes[] =
118233282Smarius	    { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 };
119200171Smav	int reg = 0x53 - devno;
120183724Ssos
121200171Smav	mode = min(mode, ctlr->chip->max_dma);
122200171Smav	if (ctlr->chip->cfg1 & AMD_CABLE) {
123209872Smav		if (ata_dma_check_80pin && mode > ATA_UDMA2 &&
124200171Smav		    !(pci_read_config(parent, 0x42, 1) & (1 << devno))) {
125200171Smav			ata_print_cable(dev, "controller");
126200171Smav			mode = ATA_UDMA2;
127200171Smav		}
128183724Ssos	}
129200171Smav	/* Set UDMA timings. */
130200171Smav	if (mode >= ATA_UDMA0) {
131200171Smav	    pci_write_config(parent, reg, modes[mode & ATA_MODE_MASK], 1);
132200171Smav	    piomode = ATA_PIO4;
133200171Smav	} else {
134200171Smav	    pci_write_config(parent, reg, 0x8b, 1);
135200171Smav	    piomode = mode;
136200171Smav	}
137200171Smav	/* Set WDMA/PIO timings. */
138200171Smav	pci_write_config(parent, reg - 0x08, timings[ata_mode2idx(piomode)], 1);
139200171Smav	return (mode);
140183724Ssos}
141183724Ssos
142200544Smariusstatic int
143200544Smariusata_amd_ch_attach(device_t dev)
144200544Smarius{
145200544Smarius	struct ata_pci_controller *ctlr;
146200544Smarius	struct ata_channel *ch;
147200544Smarius	int error;
148200544Smarius
149200544Smarius	ctlr = device_get_softc(device_get_parent(dev));
150200544Smarius	ch = device_get_softc(dev);
151200544Smarius	error = ata_pci_ch_attach(dev);
152200544Smarius	if (ctlr->chip->cfg1 & AMD_CABLE)
153200544Smarius		ch->flags |= ATA_CHECKS_CABLE;
154200544Smarius	return (error);
155200544Smarius}
156200544Smarius
157183724SsosATA_DECLARE_DRIVER(ata_amd);
158