xref: /freebsd/sys/dev/mvs/mvs_soc.c (revision 11bcf702f4200a351f06b549e14bc9ea4fc76dcc)
1dd48af36SAlexander Motin /*-
2dd48af36SAlexander Motin  * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
3dd48af36SAlexander Motin  * All rights reserved.
4dd48af36SAlexander Motin  *
5dd48af36SAlexander Motin  * Redistribution and use in source and binary forms, with or without
6dd48af36SAlexander Motin  * modification, are permitted provided that the following conditions
7dd48af36SAlexander Motin  * are met:
8dd48af36SAlexander Motin  * 1. Redistributions of source code must retain the above copyright
9dd48af36SAlexander Motin  *    notice, this list of conditions and the following disclaimer,
10dd48af36SAlexander Motin  *    without modification, immediately at the beginning of the file.
11dd48af36SAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
12dd48af36SAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
13dd48af36SAlexander Motin  *    documentation and/or other materials provided with the distribution.
14dd48af36SAlexander Motin  *
15dd48af36SAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16dd48af36SAlexander Motin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17dd48af36SAlexander Motin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18dd48af36SAlexander Motin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19dd48af36SAlexander Motin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20dd48af36SAlexander Motin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21dd48af36SAlexander Motin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22dd48af36SAlexander Motin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23dd48af36SAlexander Motin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24dd48af36SAlexander Motin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25dd48af36SAlexander Motin  */
26dd48af36SAlexander Motin 
27dd48af36SAlexander Motin #include <sys/cdefs.h>
28dd48af36SAlexander Motin __FBSDID("$FreeBSD$");
29dd48af36SAlexander Motin 
30dd48af36SAlexander Motin #include <sys/param.h>
31dd48af36SAlexander Motin #include <sys/module.h>
32dd48af36SAlexander Motin #include <sys/systm.h>
33dd48af36SAlexander Motin #include <sys/kernel.h>
34dd48af36SAlexander Motin #include <sys/bus.h>
35dd48af36SAlexander Motin #include <sys/endian.h>
36dd48af36SAlexander Motin #include <sys/malloc.h>
37dd48af36SAlexander Motin #include <sys/lock.h>
38dd48af36SAlexander Motin #include <sys/mutex.h>
39dd48af36SAlexander Motin #include <vm/uma.h>
40dd48af36SAlexander Motin #include <machine/stdarg.h>
41dd48af36SAlexander Motin #include <machine/resource.h>
42dd48af36SAlexander Motin #include <machine/bus.h>
43dd48af36SAlexander Motin #include <sys/rman.h>
44dd48af36SAlexander Motin #include <arm/mv/mvreg.h>
45dd48af36SAlexander Motin #include <arm/mv/mvvar.h>
4601e2aa9fSAlexander Motin #include <dev/ofw/ofw_bus.h>
4701e2aa9fSAlexander Motin #include <dev/ofw/ofw_bus_subr.h>
48dd48af36SAlexander Motin #include "mvs.h"
49dd48af36SAlexander Motin 
50dd48af36SAlexander Motin /* local prototypes */
51dd48af36SAlexander Motin static int mvs_setup_interrupt(device_t dev);
52dd48af36SAlexander Motin static void mvs_intr(void *data);
53dd48af36SAlexander Motin static int mvs_suspend(device_t dev);
54dd48af36SAlexander Motin static int mvs_resume(device_t dev);
55dd48af36SAlexander Motin static int mvs_ctlr_setup(device_t dev);
56dd48af36SAlexander Motin 
57dd48af36SAlexander Motin static struct {
58dd48af36SAlexander Motin 	uint32_t	id;
59dd48af36SAlexander Motin 	uint8_t		rev;
60dd48af36SAlexander Motin 	const char	*name;
61dd48af36SAlexander Motin 	int		ports;
62dd48af36SAlexander Motin 	int		quirks;
63dd48af36SAlexander Motin } mvs_ids[] = {
64dd48af36SAlexander Motin 	{MV_DEV_88F5182, 0x00,   "Marvell 88F5182",	2, MVS_Q_GENIIE|MVS_Q_SOC},
65dd48af36SAlexander Motin 	{MV_DEV_88F6281, 0x00,   "Marvell 88F6281",	2, MVS_Q_GENIIE|MVS_Q_SOC},
66dd48af36SAlexander Motin 	{MV_DEV_MV78100, 0x00,   "Marvell MV78100",	2, MVS_Q_GENIIE|MVS_Q_SOC},
67dd48af36SAlexander Motin 	{MV_DEV_MV78100_Z0, 0x00,"Marvell MV78100",	2, MVS_Q_GENIIE|MVS_Q_SOC},
68dd48af36SAlexander Motin 	{0,              0x00,   NULL,			0, 0}
69dd48af36SAlexander Motin };
70dd48af36SAlexander Motin 
71dd48af36SAlexander Motin static int
72dd48af36SAlexander Motin mvs_probe(device_t dev)
73dd48af36SAlexander Motin {
74dd48af36SAlexander Motin 	char buf[64];
75dd48af36SAlexander Motin 	int i;
76dd48af36SAlexander Motin 	uint32_t devid, revid;
77dd48af36SAlexander Motin 
7801e2aa9fSAlexander Motin 	if (!ofw_bus_is_compatible(dev, "mrvl,sata"))
7901e2aa9fSAlexander Motin 		return (ENXIO);
8001e2aa9fSAlexander Motin 
81dd48af36SAlexander Motin 	soc_id(&devid, &revid);
82dd48af36SAlexander Motin 	for (i = 0; mvs_ids[i].id != 0; i++) {
83dd48af36SAlexander Motin 		if (mvs_ids[i].id == devid &&
84dd48af36SAlexander Motin 		    mvs_ids[i].rev <= revid) {
85dd48af36SAlexander Motin 			snprintf(buf, sizeof(buf), "%s SATA controller",
86dd48af36SAlexander Motin 			    mvs_ids[i].name);
87dd48af36SAlexander Motin 			device_set_desc_copy(dev, buf);
88dd48af36SAlexander Motin 			return (BUS_PROBE_VENDOR);
89dd48af36SAlexander Motin 		}
90dd48af36SAlexander Motin 	}
91dd48af36SAlexander Motin 	return (ENXIO);
92dd48af36SAlexander Motin }
93dd48af36SAlexander Motin 
94dd48af36SAlexander Motin static int
95dd48af36SAlexander Motin mvs_attach(device_t dev)
96dd48af36SAlexander Motin {
97dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
98dd48af36SAlexander Motin 	device_t child;
99dd48af36SAlexander Motin 	int	error, unit, i;
100dd48af36SAlexander Motin 	uint32_t devid, revid;
101dd48af36SAlexander Motin 
102dd48af36SAlexander Motin 	soc_id(&devid, &revid);
103dd48af36SAlexander Motin 	ctlr->dev = dev;
104dd48af36SAlexander Motin 	i = 0;
105dd48af36SAlexander Motin 	while (mvs_ids[i].id != 0 &&
106dd48af36SAlexander Motin 	    (mvs_ids[i].id != devid ||
107dd48af36SAlexander Motin 	     mvs_ids[i].rev > revid))
108dd48af36SAlexander Motin 		i++;
109dd48af36SAlexander Motin 	ctlr->channels = mvs_ids[i].ports;
110dd48af36SAlexander Motin 	ctlr->quirks = mvs_ids[i].quirks;
111dd48af36SAlexander Motin 	resource_int_value(device_get_name(dev),
112dd48af36SAlexander Motin 	    device_get_unit(dev), "ccc", &ctlr->ccc);
113dd48af36SAlexander Motin 	ctlr->cccc = 8;
114dd48af36SAlexander Motin 	resource_int_value(device_get_name(dev),
115dd48af36SAlexander Motin 	    device_get_unit(dev), "cccc", &ctlr->cccc);
116dd48af36SAlexander Motin 	if (ctlr->ccc == 0 || ctlr->cccc == 0) {
117dd48af36SAlexander Motin 		ctlr->ccc = 0;
118dd48af36SAlexander Motin 		ctlr->cccc = 0;
119dd48af36SAlexander Motin 	}
120dd48af36SAlexander Motin 	if (ctlr->ccc > 100000)
121dd48af36SAlexander Motin 		ctlr->ccc = 100000;
122dd48af36SAlexander Motin 	device_printf(dev,
123dd48af36SAlexander Motin 	    "Gen-%s, %d %sGbps ports, Port Multiplier %s%s\n",
124dd48af36SAlexander Motin 	    ((ctlr->quirks & MVS_Q_GENI) ? "I" :
125dd48af36SAlexander Motin 	     ((ctlr->quirks & MVS_Q_GENII) ? "II" : "IIe")),
126dd48af36SAlexander Motin 	    ctlr->channels,
127dd48af36SAlexander Motin 	    ((ctlr->quirks & MVS_Q_GENI) ? "1.5" : "3"),
128dd48af36SAlexander Motin 	    ((ctlr->quirks & MVS_Q_GENI) ?
129dd48af36SAlexander Motin 	    "not supported" : "supported"),
130dd48af36SAlexander Motin 	    ((ctlr->quirks & MVS_Q_GENIIE) ?
131dd48af36SAlexander Motin 	    " with FBS" : ""));
132dd48af36SAlexander Motin 	mtx_init(&ctlr->mtx, "MVS controller lock", NULL, MTX_DEF);
133dd48af36SAlexander Motin 	/* We should have a memory BAR(0). */
134dd48af36SAlexander Motin 	ctlr->r_rid = 0;
135dd48af36SAlexander Motin 	if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
136dd48af36SAlexander Motin 	    &ctlr->r_rid, RF_ACTIVE)))
137dd48af36SAlexander Motin 		return ENXIO;
138dd48af36SAlexander Motin 	/* Setup our own memory management for channels. */
139cc6b610bSAlexander Motin 	ctlr->sc_iomem.rm_start = rman_get_start(ctlr->r_mem);
140cc6b610bSAlexander Motin 	ctlr->sc_iomem.rm_end = rman_get_end(ctlr->r_mem);
141dd48af36SAlexander Motin 	ctlr->sc_iomem.rm_type = RMAN_ARRAY;
142dd48af36SAlexander Motin 	ctlr->sc_iomem.rm_descr = "I/O memory addresses";
143dd48af36SAlexander Motin 	if ((error = rman_init(&ctlr->sc_iomem)) != 0) {
144dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
145dd48af36SAlexander Motin 		return (error);
146dd48af36SAlexander Motin 	}
147dd48af36SAlexander Motin 	if ((error = rman_manage_region(&ctlr->sc_iomem,
148dd48af36SAlexander Motin 	    rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) {
149dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
150dd48af36SAlexander Motin 		rman_fini(&ctlr->sc_iomem);
151dd48af36SAlexander Motin 		return (error);
152dd48af36SAlexander Motin 	}
153dd48af36SAlexander Motin 	mvs_ctlr_setup(dev);
154dd48af36SAlexander Motin 	/* Setup interrupts. */
155dd48af36SAlexander Motin 	if (mvs_setup_interrupt(dev)) {
156dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
157dd48af36SAlexander Motin 		rman_fini(&ctlr->sc_iomem);
158dd48af36SAlexander Motin 		return ENXIO;
159dd48af36SAlexander Motin 	}
160dd48af36SAlexander Motin 	/* Attach all channels on this controller */
161dd48af36SAlexander Motin 	for (unit = 0; unit < ctlr->channels; unit++) {
162dd48af36SAlexander Motin 		child = device_add_child(dev, "mvsch", -1);
163dd48af36SAlexander Motin 		if (child == NULL)
164dd48af36SAlexander Motin 			device_printf(dev, "failed to add channel device\n");
165dd48af36SAlexander Motin 		else
166dd48af36SAlexander Motin 			device_set_ivars(child, (void *)(intptr_t)unit);
167dd48af36SAlexander Motin 	}
168dd48af36SAlexander Motin 	bus_generic_attach(dev);
169dd48af36SAlexander Motin 	return 0;
170dd48af36SAlexander Motin }
171dd48af36SAlexander Motin 
172dd48af36SAlexander Motin static int
173dd48af36SAlexander Motin mvs_detach(device_t dev)
174dd48af36SAlexander Motin {
175dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
176dd48af36SAlexander Motin 
177dd48af36SAlexander Motin 	/* Detach & delete all children */
178*11bcf702SHans Petter Selasky 	device_delete_all_children(dev);
179*11bcf702SHans Petter Selasky 
180dd48af36SAlexander Motin 	/* Free interrupt. */
181dd48af36SAlexander Motin 	if (ctlr->irq.r_irq) {
182dd48af36SAlexander Motin 		bus_teardown_intr(dev, ctlr->irq.r_irq,
183dd48af36SAlexander Motin 		    ctlr->irq.handle);
184dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_IRQ,
185dd48af36SAlexander Motin 		    ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
186dd48af36SAlexander Motin 	}
187dd48af36SAlexander Motin 	/* Free memory. */
188dd48af36SAlexander Motin 	rman_fini(&ctlr->sc_iomem);
189dd48af36SAlexander Motin 	if (ctlr->r_mem)
190dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
191dd48af36SAlexander Motin 	mtx_destroy(&ctlr->mtx);
192dd48af36SAlexander Motin 	return (0);
193dd48af36SAlexander Motin }
194dd48af36SAlexander Motin 
195dd48af36SAlexander Motin static int
196dd48af36SAlexander Motin mvs_ctlr_setup(device_t dev)
197dd48af36SAlexander Motin {
198dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
199dd48af36SAlexander Motin 	int ccc = ctlr->ccc, cccc = ctlr->cccc, ccim = 0;
200dd48af36SAlexander Motin 
201dd48af36SAlexander Motin 	/* Mask chip interrupts */
202dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIM, 0x00000000);
203dd48af36SAlexander Motin 	/* Clear HC interrupts */
204dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, HC_IC, 0x00000000);
205dd48af36SAlexander Motin 	/* Clear chip interrupts */
206dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIC, 0);
207dd48af36SAlexander Motin 	/* Configure per-HC CCC */
208dd48af36SAlexander Motin 	if (ccc && bootverbose) {
209dd48af36SAlexander Motin 		device_printf(dev,
210dd48af36SAlexander Motin 		    "CCC with %dus/%dcmd enabled\n",
211dd48af36SAlexander Motin 		    ctlr->ccc, ctlr->cccc);
212dd48af36SAlexander Motin 	}
213dd48af36SAlexander Motin 	ccc *= 150;
214dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, HC_ICT, cccc);
215dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, HC_ITT, ccc);
216dd48af36SAlexander Motin 	if (ccc)
217dd48af36SAlexander Motin 		ccim |= IC_HC0_COAL_DONE;
218dd48af36SAlexander Motin 	/* Enable chip interrupts */
219dd48af36SAlexander Motin 	ctlr->gmim = (ccc ? IC_HC0_COAL_DONE : IC_DONE_HC0) | IC_ERR_HC0;
220dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIM, ctlr->gmim | ctlr->pmim);
221dd48af36SAlexander Motin 	return (0);
222dd48af36SAlexander Motin }
223dd48af36SAlexander Motin 
224dd48af36SAlexander Motin static void
225dd48af36SAlexander Motin mvs_edma(device_t dev, device_t child, int mode)
226dd48af36SAlexander Motin {
227dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
228dd48af36SAlexander Motin 	int unit = ((struct mvs_channel *)device_get_softc(child))->unit;
229dd48af36SAlexander Motin 	int bit = IC_DONE_IRQ << (unit * 2);
230dd48af36SAlexander Motin 
231dd48af36SAlexander Motin 	if (ctlr->ccc == 0)
232dd48af36SAlexander Motin 		return;
233dd48af36SAlexander Motin 	/* CCC is not working for non-EDMA mode. Unmask device interrupts. */
234dd48af36SAlexander Motin 	mtx_lock(&ctlr->mtx);
235dd48af36SAlexander Motin 	if (mode == MVS_EDMA_OFF)
236dd48af36SAlexander Motin 		ctlr->pmim |= bit;
237dd48af36SAlexander Motin 	else
238dd48af36SAlexander Motin 		ctlr->pmim &= ~bit;
239dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIM, ctlr->gmim | ctlr->pmim);
240dd48af36SAlexander Motin 	mtx_unlock(&ctlr->mtx);
241dd48af36SAlexander Motin }
242dd48af36SAlexander Motin 
243dd48af36SAlexander Motin static int
244dd48af36SAlexander Motin mvs_suspend(device_t dev)
245dd48af36SAlexander Motin {
246dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
247dd48af36SAlexander Motin 
248dd48af36SAlexander Motin 	bus_generic_suspend(dev);
249dd48af36SAlexander Motin 	/* Mask chip interrupts */
250dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIM, 0x00000000);
251dd48af36SAlexander Motin 	return 0;
252dd48af36SAlexander Motin }
253dd48af36SAlexander Motin 
254dd48af36SAlexander Motin static int
255dd48af36SAlexander Motin mvs_resume(device_t dev)
256dd48af36SAlexander Motin {
257dd48af36SAlexander Motin 
258dd48af36SAlexander Motin 	mvs_ctlr_setup(dev);
259dd48af36SAlexander Motin 	return (bus_generic_resume(dev));
260dd48af36SAlexander Motin }
261dd48af36SAlexander Motin 
262dd48af36SAlexander Motin static int
263dd48af36SAlexander Motin mvs_setup_interrupt(device_t dev)
264dd48af36SAlexander Motin {
265dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
266dd48af36SAlexander Motin 
267dd48af36SAlexander Motin 	/* Allocate all IRQs. */
268dd48af36SAlexander Motin 	ctlr->irq.r_irq_rid = 0;
269dd48af36SAlexander Motin 	if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
270dd48af36SAlexander Motin 	    &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
271dd48af36SAlexander Motin 		device_printf(dev, "unable to map interrupt\n");
272dd48af36SAlexander Motin 		return (ENXIO);
273dd48af36SAlexander Motin 	}
274dd48af36SAlexander Motin 	if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL,
275dd48af36SAlexander Motin 	    mvs_intr, ctlr, &ctlr->irq.handle))) {
276dd48af36SAlexander Motin 		device_printf(dev, "unable to setup interrupt\n");
277dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_IRQ,
278dd48af36SAlexander Motin 		    ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
279dd48af36SAlexander Motin 		ctlr->irq.r_irq = 0;
280dd48af36SAlexander Motin 		return (ENXIO);
281dd48af36SAlexander Motin 	}
282dd48af36SAlexander Motin 	return (0);
283dd48af36SAlexander Motin }
284dd48af36SAlexander Motin 
285dd48af36SAlexander Motin /*
286dd48af36SAlexander Motin  * Common case interrupt handler.
287dd48af36SAlexander Motin  */
288dd48af36SAlexander Motin static void
289dd48af36SAlexander Motin mvs_intr(void *data)
290dd48af36SAlexander Motin {
291dd48af36SAlexander Motin 	struct mvs_controller *ctlr = data;
292dd48af36SAlexander Motin 	struct mvs_intr_arg arg;
293dd48af36SAlexander Motin 	void (*function)(void *);
294dd48af36SAlexander Motin 	int p;
295dd48af36SAlexander Motin 	u_int32_t ic, aic;
296dd48af36SAlexander Motin 
297dd48af36SAlexander Motin 	ic = ATA_INL(ctlr->r_mem, CHIP_SOC_MIC);
298dd48af36SAlexander Motin 	if ((ic & IC_HC0) == 0)
299dd48af36SAlexander Motin 		return;
300dd48af36SAlexander Motin 	/* Acknowledge interrupts of this HC. */
301dd48af36SAlexander Motin 	aic = 0;
302dd48af36SAlexander Motin 	if (ic & (IC_DONE_IRQ << 0))
303dd48af36SAlexander Motin 		aic |= HC_IC_DONE(0) | HC_IC_DEV(0);
304dd48af36SAlexander Motin 	if (ic & (IC_DONE_IRQ << 2))
305dd48af36SAlexander Motin 		aic |= HC_IC_DONE(1) | HC_IC_DEV(1);
306dd48af36SAlexander Motin 	if (ic & (IC_DONE_IRQ << 4))
307dd48af36SAlexander Motin 		aic |= HC_IC_DONE(2) | HC_IC_DEV(2);
308dd48af36SAlexander Motin 	if (ic & (IC_DONE_IRQ << 6))
309dd48af36SAlexander Motin 		aic |= HC_IC_DONE(3) | HC_IC_DEV(3);
310dd48af36SAlexander Motin 	if (ic & IC_HC0_COAL_DONE)
311dd48af36SAlexander Motin 		aic |= HC_IC_COAL;
312dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, HC_IC, ~aic);
313dd48af36SAlexander Motin 	/* Call per-port interrupt handler. */
314dd48af36SAlexander Motin 	for (p = 0; p < ctlr->channels; p++) {
315dd48af36SAlexander Motin 		arg.cause = ic & (IC_ERR_IRQ|IC_DONE_IRQ);
316dd48af36SAlexander Motin 		if ((arg.cause != 0) &&
317dd48af36SAlexander Motin 		    (function = ctlr->interrupt[p].function)) {
318dd48af36SAlexander Motin 			arg.arg = ctlr->interrupt[p].argument;
319dd48af36SAlexander Motin 			function(&arg);
320dd48af36SAlexander Motin 		}
321dd48af36SAlexander Motin 		ic >>= 2;
322dd48af36SAlexander Motin 	}
323dd48af36SAlexander Motin }
324dd48af36SAlexander Motin 
325dd48af36SAlexander Motin static struct resource *
326dd48af36SAlexander Motin mvs_alloc_resource(device_t dev, device_t child, int type, int *rid,
327dd48af36SAlexander Motin 		       u_long start, u_long end, u_long count, u_int flags)
328dd48af36SAlexander Motin {
329dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
330dd48af36SAlexander Motin 	int unit = ((struct mvs_channel *)device_get_softc(child))->unit;
331dd48af36SAlexander Motin 	struct resource *res = NULL;
332dd48af36SAlexander Motin 	int offset = PORT_BASE(unit & 0x03);
333dd48af36SAlexander Motin 	long st;
334dd48af36SAlexander Motin 
335dd48af36SAlexander Motin 	switch (type) {
336dd48af36SAlexander Motin 	case SYS_RES_MEMORY:
337dd48af36SAlexander Motin 		st = rman_get_start(ctlr->r_mem);
338dd48af36SAlexander Motin 		res = rman_reserve_resource(&ctlr->sc_iomem, st + offset,
339dd48af36SAlexander Motin 		    st + offset + PORT_SIZE - 1, PORT_SIZE, RF_ACTIVE, child);
340dd48af36SAlexander Motin 		if (res) {
341dd48af36SAlexander Motin 			bus_space_handle_t bsh;
342dd48af36SAlexander Motin 			bus_space_tag_t bst;
343dd48af36SAlexander Motin 			bsh = rman_get_bushandle(ctlr->r_mem);
344dd48af36SAlexander Motin 			bst = rman_get_bustag(ctlr->r_mem);
345dd48af36SAlexander Motin 			bus_space_subregion(bst, bsh, offset, PORT_SIZE, &bsh);
346dd48af36SAlexander Motin 			rman_set_bushandle(res, bsh);
347dd48af36SAlexander Motin 			rman_set_bustag(res, bst);
348dd48af36SAlexander Motin 		}
349dd48af36SAlexander Motin 		break;
350dd48af36SAlexander Motin 	case SYS_RES_IRQ:
351dd48af36SAlexander Motin 		if (*rid == ATA_IRQ_RID)
352dd48af36SAlexander Motin 			res = ctlr->irq.r_irq;
353dd48af36SAlexander Motin 		break;
354dd48af36SAlexander Motin 	}
355dd48af36SAlexander Motin 	return (res);
356dd48af36SAlexander Motin }
357dd48af36SAlexander Motin 
358dd48af36SAlexander Motin static int
359dd48af36SAlexander Motin mvs_release_resource(device_t dev, device_t child, int type, int rid,
360dd48af36SAlexander Motin 			 struct resource *r)
361dd48af36SAlexander Motin {
362dd48af36SAlexander Motin 
363dd48af36SAlexander Motin 	switch (type) {
364dd48af36SAlexander Motin 	case SYS_RES_MEMORY:
365dd48af36SAlexander Motin 		rman_release_resource(r);
366dd48af36SAlexander Motin 		return (0);
367dd48af36SAlexander Motin 	case SYS_RES_IRQ:
368dd48af36SAlexander Motin 		if (rid != ATA_IRQ_RID)
369dd48af36SAlexander Motin 			return ENOENT;
370dd48af36SAlexander Motin 		return (0);
371dd48af36SAlexander Motin 	}
372dd48af36SAlexander Motin 	return (EINVAL);
373dd48af36SAlexander Motin }
374dd48af36SAlexander Motin 
375dd48af36SAlexander Motin static int
376dd48af36SAlexander Motin mvs_setup_intr(device_t dev, device_t child, struct resource *irq,
377dd48af36SAlexander Motin 		   int flags, driver_filter_t *filter, driver_intr_t *function,
378dd48af36SAlexander Motin 		   void *argument, void **cookiep)
379dd48af36SAlexander Motin {
380dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
381dd48af36SAlexander Motin 	int unit = (intptr_t)device_get_ivars(child);
382dd48af36SAlexander Motin 
383dd48af36SAlexander Motin 	if (filter != NULL) {
384dd48af36SAlexander Motin 		printf("mvs.c: we cannot use a filter here\n");
385dd48af36SAlexander Motin 		return (EINVAL);
386dd48af36SAlexander Motin 	}
387dd48af36SAlexander Motin 	ctlr->interrupt[unit].function = function;
388dd48af36SAlexander Motin 	ctlr->interrupt[unit].argument = argument;
389dd48af36SAlexander Motin 	return (0);
390dd48af36SAlexander Motin }
391dd48af36SAlexander Motin 
392dd48af36SAlexander Motin static int
393dd48af36SAlexander Motin mvs_teardown_intr(device_t dev, device_t child, struct resource *irq,
394dd48af36SAlexander Motin 		      void *cookie)
395dd48af36SAlexander Motin {
396dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
397dd48af36SAlexander Motin 	int unit = (intptr_t)device_get_ivars(child);
398dd48af36SAlexander Motin 
399dd48af36SAlexander Motin 	ctlr->interrupt[unit].function = NULL;
400dd48af36SAlexander Motin 	ctlr->interrupt[unit].argument = NULL;
401dd48af36SAlexander Motin 	return (0);
402dd48af36SAlexander Motin }
403dd48af36SAlexander Motin 
404dd48af36SAlexander Motin static int
405dd48af36SAlexander Motin mvs_print_child(device_t dev, device_t child)
406dd48af36SAlexander Motin {
407dd48af36SAlexander Motin 	int retval;
408dd48af36SAlexander Motin 
409dd48af36SAlexander Motin 	retval = bus_print_child_header(dev, child);
410dd48af36SAlexander Motin 	retval += printf(" at channel %d",
411dd48af36SAlexander Motin 	    (int)(intptr_t)device_get_ivars(child));
412dd48af36SAlexander Motin 	retval += bus_print_child_footer(dev, child);
413dd48af36SAlexander Motin 
414dd48af36SAlexander Motin 	return (retval);
415dd48af36SAlexander Motin }
416dd48af36SAlexander Motin 
417445cc79cSAlexander Motin static int
418445cc79cSAlexander Motin mvs_child_location_str(device_t dev, device_t child, char *buf,
419445cc79cSAlexander Motin     size_t buflen)
420445cc79cSAlexander Motin {
421445cc79cSAlexander Motin 
422445cc79cSAlexander Motin 	snprintf(buf, buflen, "channel=%d",
423445cc79cSAlexander Motin 	    (int)(intptr_t)device_get_ivars(child));
424445cc79cSAlexander Motin 	return (0);
425445cc79cSAlexander Motin }
426445cc79cSAlexander Motin 
427dd48af36SAlexander Motin static device_method_t mvs_methods[] = {
428dd48af36SAlexander Motin 	DEVMETHOD(device_probe,     mvs_probe),
429dd48af36SAlexander Motin 	DEVMETHOD(device_attach,    mvs_attach),
430dd48af36SAlexander Motin 	DEVMETHOD(device_detach,    mvs_detach),
431dd48af36SAlexander Motin 	DEVMETHOD(device_suspend,   mvs_suspend),
432dd48af36SAlexander Motin 	DEVMETHOD(device_resume,    mvs_resume),
433dd48af36SAlexander Motin 	DEVMETHOD(bus_print_child,  mvs_print_child),
434dd48af36SAlexander Motin 	DEVMETHOD(bus_alloc_resource,       mvs_alloc_resource),
435dd48af36SAlexander Motin 	DEVMETHOD(bus_release_resource,     mvs_release_resource),
436dd48af36SAlexander Motin 	DEVMETHOD(bus_setup_intr,   mvs_setup_intr),
437dd48af36SAlexander Motin 	DEVMETHOD(bus_teardown_intr,mvs_teardown_intr),
438dd48af36SAlexander Motin 	DEVMETHOD(mvs_edma,         mvs_edma),
439445cc79cSAlexander Motin 	DEVMETHOD(bus_child_location_str, mvs_child_location_str),
440dd48af36SAlexander Motin 	{ 0, 0 }
441dd48af36SAlexander Motin };
442dd48af36SAlexander Motin static driver_t mvs_driver = {
44301e2aa9fSAlexander Motin         "mvs",
444dd48af36SAlexander Motin         mvs_methods,
445dd48af36SAlexander Motin         sizeof(struct mvs_controller)
446dd48af36SAlexander Motin };
44701e2aa9fSAlexander Motin DRIVER_MODULE(mvs, simplebus, mvs_driver, mvs_devclass, 0, 0);
44801e2aa9fSAlexander Motin MODULE_VERSION(mvs, 1);
44901e2aa9fSAlexander Motin MODULE_DEPEND(mvs, cam, 1, 1, 1);
450