ata-marvell.c revision 287016
1/*-
2 * Copyright (c) 1998 - 2008 S��ren Schmidt <sos@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer,
10 *    without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: stable/10/sys/dev/ata/chipsets/ata-marvell.c 287016 2015-08-22 07:32:47Z mav $");
29
30#include <sys/param.h>
31#include <sys/module.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 */
54static int ata_marvell_chipinit(device_t dev);
55static int ata_marvell_ch_attach(device_t dev);
56static int ata_marvell_setmode(device_t dev, int target, int mode);
57static int ata_marvell_dummy_chipinit(device_t dev);
58
59/* misc defines */
60#define MV_61XX		61
61#define MV_91XX		91
62
63/*
64 * Marvell chipset support functions
65 */
66#define ATA_MV_HOST_BASE(ch) \
67	((ch->unit & 3) * 0x0100) + (ch->unit > 3 ? 0x30000 : 0x20000)
68#define ATA_MV_EDMA_BASE(ch) \
69	((ch->unit & 3) * 0x2000) + (ch->unit > 3 ? 0x30000 : 0x20000)
70
71struct ata_marvell_response {
72    u_int16_t   tag;
73    u_int8_t    edma_status;
74    u_int8_t    dev_status;
75    u_int32_t   timestamp;
76};
77
78struct ata_marvell_dma_prdentry {
79    u_int32_t addrlo;
80    u_int32_t count;
81    u_int32_t addrhi;
82    u_int32_t reserved;
83};
84
85static int
86ata_marvell_probe(device_t dev)
87{
88    struct ata_pci_controller *ctlr = device_get_softc(dev);
89    static const struct ata_chip_id ids[] =
90    {{ ATA_M88SE6101, 0, 0, MV_61XX, ATA_UDMA6, "88SE6101" },
91     { ATA_M88SE6102, 0, 0, MV_61XX, ATA_UDMA6, "88SE6102" },
92     { ATA_M88SE6111, 0, 1, MV_61XX, ATA_UDMA6, "88SE6111" },
93     { ATA_M88SE6121, 0, 2, MV_61XX, ATA_UDMA6, "88SE6121" },
94     { ATA_M88SE6141, 0, 4, MV_61XX, ATA_UDMA6, "88SE6141" },
95     { ATA_M88SE6145, 0, 4, MV_61XX, ATA_UDMA6, "88SE6145" },
96     { 0x91a41b4b,    0, 0, MV_91XX, ATA_UDMA6, "88SE912x" },
97     { 0, 0, 0, 0, 0, 0}};
98
99    if (pci_get_vendor(dev) != ATA_MARVELL_ID &&
100	pci_get_vendor(dev) != ATA_MARVELL2_ID)
101	return ENXIO;
102
103    if (!(ctlr->chip = ata_match_chip(dev, ids)))
104	return ENXIO;
105
106    ata_set_desc(dev);
107
108    switch (ctlr->chip->cfg2) {
109    case MV_61XX:
110	ctlr->chipinit = ata_marvell_chipinit;
111	break;
112    case MV_91XX:
113	ctlr->chipinit = ata_marvell_dummy_chipinit;
114	break;
115    }
116    return (BUS_PROBE_LOW_PRIORITY);
117}
118
119static int
120ata_marvell_chipinit(device_t dev)
121{
122	struct ata_pci_controller *ctlr = device_get_softc(dev);
123	device_t child;
124
125	if (ata_setup_interrupt(dev, ata_generic_intr))
126		return ENXIO;
127	/* Create AHCI subdevice if AHCI part present. */
128	if (ctlr->chip->cfg1) {
129	    	child = device_add_child(dev, NULL, -1);
130		if (child != NULL) {
131		    device_set_ivars(child, (void *)(intptr_t)-1);
132		    bus_generic_attach(dev);
133		}
134	}
135        ctlr->ch_attach = ata_marvell_ch_attach;
136	ctlr->ch_detach = ata_pci_ch_detach;
137	ctlr->reset = ata_generic_reset;
138        ctlr->setmode = ata_marvell_setmode;
139        ctlr->channels = 1;
140        return (0);
141}
142
143static int
144ata_marvell_ch_attach(device_t dev)
145{
146	struct ata_channel *ch = device_get_softc(dev);
147	int error;
148
149	error = ata_pci_ch_attach(dev);
150    	/* dont use 32 bit PIO transfers */
151	ch->flags |= ATA_USE_16BIT;
152	ch->flags |= ATA_CHECKS_CABLE;
153	return (error);
154}
155
156static int
157ata_marvell_setmode(device_t dev, int target, int mode)
158{
159	struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
160	struct ata_channel *ch = device_get_softc(dev);
161
162	mode = min(mode, ctlr->chip->max_dma);
163	/* Check for 80pin cable present. */
164	if (ata_dma_check_80pin && mode > ATA_UDMA2 &&
165	    ATA_IDX_INB(ch, ATA_BMDEVSPEC_0) & 0x01) {
166		ata_print_cable(dev, "controller");
167		mode = ATA_UDMA2;
168	}
169	/* Nothing to do to setup mode, the controller snoop SET_FEATURE cmd. */
170	return (mode);
171}
172
173static int
174ata_marvell_dummy_chipinit(device_t dev)
175{
176	struct ata_pci_controller *ctlr = device_get_softc(dev);
177
178        ctlr->channels = 0;
179        return (0);
180}
181
182ATA_DECLARE_DRIVER(ata_marvell);
183