xref: /freebsd/sys/dev/mvs/mvs_soc.c (revision ca11419237a6458a92855e838724ef8c71225f0f)
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},
66c8953e12SHiroki Sato 	{MV_DEV_88F6282, 0x00,   "Marvell 88F6282",	2, MVS_Q_GENIIE|MVS_Q_SOC},
67dd48af36SAlexander Motin 	{MV_DEV_MV78100, 0x00,   "Marvell MV78100",	2, MVS_Q_GENIIE|MVS_Q_SOC},
68dd48af36SAlexander Motin 	{MV_DEV_MV78100_Z0, 0x00,"Marvell MV78100",	2, MVS_Q_GENIIE|MVS_Q_SOC},
69dd48af36SAlexander Motin 	{0,              0x00,   NULL,			0, 0}
70dd48af36SAlexander Motin };
71dd48af36SAlexander Motin 
72dd48af36SAlexander Motin static int
73dd48af36SAlexander Motin mvs_probe(device_t dev)
74dd48af36SAlexander Motin {
75dd48af36SAlexander Motin 	char buf[64];
76dd48af36SAlexander Motin 	int i;
77dd48af36SAlexander Motin 	uint32_t devid, revid;
78dd48af36SAlexander Motin 
7901e2aa9fSAlexander Motin 	if (!ofw_bus_is_compatible(dev, "mrvl,sata"))
8001e2aa9fSAlexander Motin 		return (ENXIO);
8101e2aa9fSAlexander Motin 
82dd48af36SAlexander Motin 	soc_id(&devid, &revid);
83dd48af36SAlexander Motin 	for (i = 0; mvs_ids[i].id != 0; i++) {
84dd48af36SAlexander Motin 		if (mvs_ids[i].id == devid &&
85dd48af36SAlexander Motin 		    mvs_ids[i].rev <= revid) {
86dd48af36SAlexander Motin 			snprintf(buf, sizeof(buf), "%s SATA controller",
87dd48af36SAlexander Motin 			    mvs_ids[i].name);
88dd48af36SAlexander Motin 			device_set_desc_copy(dev, buf);
89dd48af36SAlexander Motin 			return (BUS_PROBE_VENDOR);
90dd48af36SAlexander Motin 		}
91dd48af36SAlexander Motin 	}
92dd48af36SAlexander Motin 	return (ENXIO);
93dd48af36SAlexander Motin }
94dd48af36SAlexander Motin 
95dd48af36SAlexander Motin static int
96dd48af36SAlexander Motin mvs_attach(device_t dev)
97dd48af36SAlexander Motin {
98dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
99dd48af36SAlexander Motin 	device_t child;
100dd48af36SAlexander Motin 	int	error, unit, i;
101dd48af36SAlexander Motin 	uint32_t devid, revid;
102dd48af36SAlexander Motin 
103dd48af36SAlexander Motin 	soc_id(&devid, &revid);
104dd48af36SAlexander Motin 	ctlr->dev = dev;
105dd48af36SAlexander Motin 	i = 0;
106dd48af36SAlexander Motin 	while (mvs_ids[i].id != 0 &&
107dd48af36SAlexander Motin 	    (mvs_ids[i].id != devid ||
108dd48af36SAlexander Motin 	     mvs_ids[i].rev > revid))
109dd48af36SAlexander Motin 		i++;
110dd48af36SAlexander Motin 	ctlr->channels = mvs_ids[i].ports;
111dd48af36SAlexander Motin 	ctlr->quirks = mvs_ids[i].quirks;
112dd48af36SAlexander Motin 	resource_int_value(device_get_name(dev),
113dd48af36SAlexander Motin 	    device_get_unit(dev), "ccc", &ctlr->ccc);
114dd48af36SAlexander Motin 	ctlr->cccc = 8;
115dd48af36SAlexander Motin 	resource_int_value(device_get_name(dev),
116dd48af36SAlexander Motin 	    device_get_unit(dev), "cccc", &ctlr->cccc);
117dd48af36SAlexander Motin 	if (ctlr->ccc == 0 || ctlr->cccc == 0) {
118dd48af36SAlexander Motin 		ctlr->ccc = 0;
119dd48af36SAlexander Motin 		ctlr->cccc = 0;
120dd48af36SAlexander Motin 	}
121dd48af36SAlexander Motin 	if (ctlr->ccc > 100000)
122dd48af36SAlexander Motin 		ctlr->ccc = 100000;
123dd48af36SAlexander Motin 	device_printf(dev,
124dd48af36SAlexander Motin 	    "Gen-%s, %d %sGbps ports, Port Multiplier %s%s\n",
125dd48af36SAlexander Motin 	    ((ctlr->quirks & MVS_Q_GENI) ? "I" :
126dd48af36SAlexander Motin 	     ((ctlr->quirks & MVS_Q_GENII) ? "II" : "IIe")),
127dd48af36SAlexander Motin 	    ctlr->channels,
128dd48af36SAlexander Motin 	    ((ctlr->quirks & MVS_Q_GENI) ? "1.5" : "3"),
129dd48af36SAlexander Motin 	    ((ctlr->quirks & MVS_Q_GENI) ?
130dd48af36SAlexander Motin 	    "not supported" : "supported"),
131dd48af36SAlexander Motin 	    ((ctlr->quirks & MVS_Q_GENIIE) ?
132dd48af36SAlexander Motin 	    " with FBS" : ""));
133dd48af36SAlexander Motin 	mtx_init(&ctlr->mtx, "MVS controller lock", NULL, MTX_DEF);
134dd48af36SAlexander Motin 	/* We should have a memory BAR(0). */
135dd48af36SAlexander Motin 	ctlr->r_rid = 0;
136dd48af36SAlexander Motin 	if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
137dd48af36SAlexander Motin 	    &ctlr->r_rid, RF_ACTIVE)))
138dd48af36SAlexander Motin 		return ENXIO;
139b30c7d51SAlexander Motin 	if (ATA_INL(ctlr->r_mem, PORT_BASE(0) + SATA_PHYCFG_OFS) != 0)
140b30c7d51SAlexander Motin 		ctlr->quirks |= MVS_Q_SOC65;
141dd48af36SAlexander Motin 	/* Setup our own memory management for channels. */
142cc6b610bSAlexander Motin 	ctlr->sc_iomem.rm_start = rman_get_start(ctlr->r_mem);
143cc6b610bSAlexander Motin 	ctlr->sc_iomem.rm_end = rman_get_end(ctlr->r_mem);
144dd48af36SAlexander Motin 	ctlr->sc_iomem.rm_type = RMAN_ARRAY;
145dd48af36SAlexander Motin 	ctlr->sc_iomem.rm_descr = "I/O memory addresses";
146dd48af36SAlexander Motin 	if ((error = rman_init(&ctlr->sc_iomem)) != 0) {
147dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
148dd48af36SAlexander Motin 		return (error);
149dd48af36SAlexander Motin 	}
150dd48af36SAlexander Motin 	if ((error = rman_manage_region(&ctlr->sc_iomem,
151dd48af36SAlexander Motin 	    rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) {
152dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
153dd48af36SAlexander Motin 		rman_fini(&ctlr->sc_iomem);
154dd48af36SAlexander Motin 		return (error);
155dd48af36SAlexander Motin 	}
156dd48af36SAlexander Motin 	mvs_ctlr_setup(dev);
157dd48af36SAlexander Motin 	/* Setup interrupts. */
158dd48af36SAlexander Motin 	if (mvs_setup_interrupt(dev)) {
159dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
160dd48af36SAlexander Motin 		rman_fini(&ctlr->sc_iomem);
161dd48af36SAlexander Motin 		return ENXIO;
162dd48af36SAlexander Motin 	}
163dd48af36SAlexander Motin 	/* Attach all channels on this controller */
164dd48af36SAlexander Motin 	for (unit = 0; unit < ctlr->channels; unit++) {
165dd48af36SAlexander Motin 		child = device_add_child(dev, "mvsch", -1);
166dd48af36SAlexander Motin 		if (child == NULL)
167dd48af36SAlexander Motin 			device_printf(dev, "failed to add channel device\n");
168dd48af36SAlexander Motin 		else
169dd48af36SAlexander Motin 			device_set_ivars(child, (void *)(intptr_t)unit);
170dd48af36SAlexander Motin 	}
171dd48af36SAlexander Motin 	bus_generic_attach(dev);
172dd48af36SAlexander Motin 	return 0;
173dd48af36SAlexander Motin }
174dd48af36SAlexander Motin 
175dd48af36SAlexander Motin static int
176dd48af36SAlexander Motin mvs_detach(device_t dev)
177dd48af36SAlexander Motin {
178dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
179dd48af36SAlexander Motin 
180dd48af36SAlexander Motin 	/* Detach & delete all children */
1813b12bdb5SHans Petter Selasky 	device_delete_children(dev);
18211bcf702SHans Petter Selasky 
183dd48af36SAlexander Motin 	/* Free interrupt. */
184dd48af36SAlexander Motin 	if (ctlr->irq.r_irq) {
185dd48af36SAlexander Motin 		bus_teardown_intr(dev, ctlr->irq.r_irq,
186dd48af36SAlexander Motin 		    ctlr->irq.handle);
187dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_IRQ,
188dd48af36SAlexander Motin 		    ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
189dd48af36SAlexander Motin 	}
190dd48af36SAlexander Motin 	/* Free memory. */
191dd48af36SAlexander Motin 	rman_fini(&ctlr->sc_iomem);
192dd48af36SAlexander Motin 	if (ctlr->r_mem)
193dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
194dd48af36SAlexander Motin 	mtx_destroy(&ctlr->mtx);
195dd48af36SAlexander Motin 	return (0);
196dd48af36SAlexander Motin }
197dd48af36SAlexander Motin 
198dd48af36SAlexander Motin static int
199dd48af36SAlexander Motin mvs_ctlr_setup(device_t dev)
200dd48af36SAlexander Motin {
201dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
202dd48af36SAlexander Motin 	int ccc = ctlr->ccc, cccc = ctlr->cccc, ccim = 0;
203dd48af36SAlexander Motin 
204dd48af36SAlexander Motin 	/* Mask chip interrupts */
205dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIM, 0x00000000);
206dd48af36SAlexander Motin 	/* Clear HC interrupts */
207dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, HC_IC, 0x00000000);
208dd48af36SAlexander Motin 	/* Clear chip interrupts */
209dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIC, 0);
210dd48af36SAlexander Motin 	/* Configure per-HC CCC */
211dd48af36SAlexander Motin 	if (ccc && bootverbose) {
212dd48af36SAlexander Motin 		device_printf(dev,
213dd48af36SAlexander Motin 		    "CCC with %dus/%dcmd enabled\n",
214dd48af36SAlexander Motin 		    ctlr->ccc, ctlr->cccc);
215dd48af36SAlexander Motin 	}
216dd48af36SAlexander Motin 	ccc *= 150;
217dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, HC_ICT, cccc);
218dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, HC_ITT, ccc);
219dd48af36SAlexander Motin 	if (ccc)
220dd48af36SAlexander Motin 		ccim |= IC_HC0_COAL_DONE;
221dd48af36SAlexander Motin 	/* Enable chip interrupts */
222c72ef339SRafal Jaworowski 	ctlr->gmim = ((ccc ? IC_HC0_COAL_DONE :
223c72ef339SRafal Jaworowski 	    (IC_DONE_HC0 & CHIP_SOC_HC0_MASK(ctlr->channels))) |
224c72ef339SRafal Jaworowski 	    (IC_ERR_HC0 & CHIP_SOC_HC0_MASK(ctlr->channels)));
225dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIM, ctlr->gmim | ctlr->pmim);
226dd48af36SAlexander Motin 	return (0);
227dd48af36SAlexander Motin }
228dd48af36SAlexander Motin 
229dd48af36SAlexander Motin static void
230dd48af36SAlexander Motin mvs_edma(device_t dev, device_t child, int mode)
231dd48af36SAlexander Motin {
232dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
233dd48af36SAlexander Motin 	int unit = ((struct mvs_channel *)device_get_softc(child))->unit;
234dd48af36SAlexander Motin 	int bit = IC_DONE_IRQ << (unit * 2);
235dd48af36SAlexander Motin 
236dd48af36SAlexander Motin 	if (ctlr->ccc == 0)
237dd48af36SAlexander Motin 		return;
238dd48af36SAlexander Motin 	/* CCC is not working for non-EDMA mode. Unmask device interrupts. */
239dd48af36SAlexander Motin 	mtx_lock(&ctlr->mtx);
240dd48af36SAlexander Motin 	if (mode == MVS_EDMA_OFF)
241dd48af36SAlexander Motin 		ctlr->pmim |= bit;
242dd48af36SAlexander Motin 	else
243dd48af36SAlexander Motin 		ctlr->pmim &= ~bit;
244dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIM, ctlr->gmim | ctlr->pmim);
245dd48af36SAlexander Motin 	mtx_unlock(&ctlr->mtx);
246dd48af36SAlexander Motin }
247dd48af36SAlexander Motin 
248dd48af36SAlexander Motin static int
249dd48af36SAlexander Motin mvs_suspend(device_t dev)
250dd48af36SAlexander Motin {
251dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
252dd48af36SAlexander Motin 
253dd48af36SAlexander Motin 	bus_generic_suspend(dev);
254dd48af36SAlexander Motin 	/* Mask chip interrupts */
255dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIM, 0x00000000);
256dd48af36SAlexander Motin 	return 0;
257dd48af36SAlexander Motin }
258dd48af36SAlexander Motin 
259dd48af36SAlexander Motin static int
260dd48af36SAlexander Motin mvs_resume(device_t dev)
261dd48af36SAlexander Motin {
262dd48af36SAlexander Motin 
263dd48af36SAlexander Motin 	mvs_ctlr_setup(dev);
264dd48af36SAlexander Motin 	return (bus_generic_resume(dev));
265dd48af36SAlexander Motin }
266dd48af36SAlexander Motin 
267dd48af36SAlexander Motin static int
268dd48af36SAlexander Motin mvs_setup_interrupt(device_t dev)
269dd48af36SAlexander Motin {
270dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
271dd48af36SAlexander Motin 
272dd48af36SAlexander Motin 	/* Allocate all IRQs. */
273dd48af36SAlexander Motin 	ctlr->irq.r_irq_rid = 0;
274dd48af36SAlexander Motin 	if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
275dd48af36SAlexander Motin 	    &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
276dd48af36SAlexander Motin 		device_printf(dev, "unable to map interrupt\n");
277dd48af36SAlexander Motin 		return (ENXIO);
278dd48af36SAlexander Motin 	}
279dd48af36SAlexander Motin 	if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL,
280dd48af36SAlexander Motin 	    mvs_intr, ctlr, &ctlr->irq.handle))) {
281dd48af36SAlexander Motin 		device_printf(dev, "unable to setup interrupt\n");
282dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_IRQ,
283dd48af36SAlexander Motin 		    ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
284dd48af36SAlexander Motin 		ctlr->irq.r_irq = 0;
285dd48af36SAlexander Motin 		return (ENXIO);
286dd48af36SAlexander Motin 	}
287dd48af36SAlexander Motin 	return (0);
288dd48af36SAlexander Motin }
289dd48af36SAlexander Motin 
290dd48af36SAlexander Motin /*
291dd48af36SAlexander Motin  * Common case interrupt handler.
292dd48af36SAlexander Motin  */
293dd48af36SAlexander Motin static void
294dd48af36SAlexander Motin mvs_intr(void *data)
295dd48af36SAlexander Motin {
296dd48af36SAlexander Motin 	struct mvs_controller *ctlr = data;
297dd48af36SAlexander Motin 	struct mvs_intr_arg arg;
298dd48af36SAlexander Motin 	void (*function)(void *);
299c72ef339SRafal Jaworowski 	int p, chan_num;
300dd48af36SAlexander Motin 	u_int32_t ic, aic;
301dd48af36SAlexander Motin 
302dd48af36SAlexander Motin 	ic = ATA_INL(ctlr->r_mem, CHIP_SOC_MIC);
303dd48af36SAlexander Motin 	if ((ic & IC_HC0) == 0)
304dd48af36SAlexander Motin 		return;
305c72ef339SRafal Jaworowski 
306dd48af36SAlexander Motin 	/* Acknowledge interrupts of this HC. */
307dd48af36SAlexander Motin 	aic = 0;
308c72ef339SRafal Jaworowski 
309c72ef339SRafal Jaworowski 	/* Processing interrupts from each initialized channel */
310c72ef339SRafal Jaworowski 	for (chan_num = 0; chan_num < ctlr->channels; chan_num++) {
311c72ef339SRafal Jaworowski 		if (ic & (IC_DONE_IRQ << (chan_num * 2)))
312c72ef339SRafal Jaworowski 			aic |= HC_IC_DONE(chan_num) | HC_IC_DEV(chan_num);
313c72ef339SRafal Jaworowski 	}
314c72ef339SRafal Jaworowski 
315dd48af36SAlexander Motin 	if (ic & IC_HC0_COAL_DONE)
316dd48af36SAlexander Motin 		aic |= HC_IC_COAL;
317dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, HC_IC, ~aic);
318c72ef339SRafal Jaworowski 
319dd48af36SAlexander Motin 	/* Call per-port interrupt handler. */
320dd48af36SAlexander Motin 	for (p = 0; p < ctlr->channels; p++) {
321dd48af36SAlexander Motin 		arg.cause = ic & (IC_ERR_IRQ|IC_DONE_IRQ);
322dd48af36SAlexander Motin 		if ((arg.cause != 0) &&
323dd48af36SAlexander Motin 		    (function = ctlr->interrupt[p].function)) {
324dd48af36SAlexander Motin 			arg.arg = ctlr->interrupt[p].argument;
325dd48af36SAlexander Motin 			function(&arg);
326dd48af36SAlexander Motin 		}
327dd48af36SAlexander Motin 		ic >>= 2;
328dd48af36SAlexander Motin 	}
329dd48af36SAlexander Motin }
330dd48af36SAlexander Motin 
331dd48af36SAlexander Motin static struct resource *
332dd48af36SAlexander Motin mvs_alloc_resource(device_t dev, device_t child, int type, int *rid,
333dd48af36SAlexander Motin 		       u_long start, u_long end, u_long count, u_int flags)
334dd48af36SAlexander Motin {
335dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
336dd48af36SAlexander Motin 	int unit = ((struct mvs_channel *)device_get_softc(child))->unit;
337dd48af36SAlexander Motin 	struct resource *res = NULL;
338dd48af36SAlexander Motin 	int offset = PORT_BASE(unit & 0x03);
339dd48af36SAlexander Motin 	long st;
340dd48af36SAlexander Motin 
341dd48af36SAlexander Motin 	switch (type) {
342dd48af36SAlexander Motin 	case SYS_RES_MEMORY:
343dd48af36SAlexander Motin 		st = rman_get_start(ctlr->r_mem);
344dd48af36SAlexander Motin 		res = rman_reserve_resource(&ctlr->sc_iomem, st + offset,
345dd48af36SAlexander Motin 		    st + offset + PORT_SIZE - 1, PORT_SIZE, RF_ACTIVE, child);
346dd48af36SAlexander Motin 		if (res) {
347dd48af36SAlexander Motin 			bus_space_handle_t bsh;
348dd48af36SAlexander Motin 			bus_space_tag_t bst;
349dd48af36SAlexander Motin 			bsh = rman_get_bushandle(ctlr->r_mem);
350dd48af36SAlexander Motin 			bst = rman_get_bustag(ctlr->r_mem);
351dd48af36SAlexander Motin 			bus_space_subregion(bst, bsh, offset, PORT_SIZE, &bsh);
352dd48af36SAlexander Motin 			rman_set_bushandle(res, bsh);
353dd48af36SAlexander Motin 			rman_set_bustag(res, bst);
354dd48af36SAlexander Motin 		}
355dd48af36SAlexander Motin 		break;
356dd48af36SAlexander Motin 	case SYS_RES_IRQ:
357dd48af36SAlexander Motin 		if (*rid == ATA_IRQ_RID)
358dd48af36SAlexander Motin 			res = ctlr->irq.r_irq;
359dd48af36SAlexander Motin 		break;
360dd48af36SAlexander Motin 	}
361dd48af36SAlexander Motin 	return (res);
362dd48af36SAlexander Motin }
363dd48af36SAlexander Motin 
364dd48af36SAlexander Motin static int
365dd48af36SAlexander Motin mvs_release_resource(device_t dev, device_t child, int type, int rid,
366dd48af36SAlexander Motin 			 struct resource *r)
367dd48af36SAlexander Motin {
368dd48af36SAlexander Motin 
369dd48af36SAlexander Motin 	switch (type) {
370dd48af36SAlexander Motin 	case SYS_RES_MEMORY:
371dd48af36SAlexander Motin 		rman_release_resource(r);
372dd48af36SAlexander Motin 		return (0);
373dd48af36SAlexander Motin 	case SYS_RES_IRQ:
374dd48af36SAlexander Motin 		if (rid != ATA_IRQ_RID)
375dd48af36SAlexander Motin 			return ENOENT;
376dd48af36SAlexander Motin 		return (0);
377dd48af36SAlexander Motin 	}
378dd48af36SAlexander Motin 	return (EINVAL);
379dd48af36SAlexander Motin }
380dd48af36SAlexander Motin 
381dd48af36SAlexander Motin static int
382dd48af36SAlexander Motin mvs_setup_intr(device_t dev, device_t child, struct resource *irq,
383dd48af36SAlexander Motin 		   int flags, driver_filter_t *filter, driver_intr_t *function,
384dd48af36SAlexander Motin 		   void *argument, void **cookiep)
385dd48af36SAlexander Motin {
386dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
387dd48af36SAlexander Motin 	int unit = (intptr_t)device_get_ivars(child);
388dd48af36SAlexander Motin 
389dd48af36SAlexander Motin 	if (filter != NULL) {
390dd48af36SAlexander Motin 		printf("mvs.c: we cannot use a filter here\n");
391dd48af36SAlexander Motin 		return (EINVAL);
392dd48af36SAlexander Motin 	}
393dd48af36SAlexander Motin 	ctlr->interrupt[unit].function = function;
394dd48af36SAlexander Motin 	ctlr->interrupt[unit].argument = argument;
395dd48af36SAlexander Motin 	return (0);
396dd48af36SAlexander Motin }
397dd48af36SAlexander Motin 
398dd48af36SAlexander Motin static int
399dd48af36SAlexander Motin mvs_teardown_intr(device_t dev, device_t child, struct resource *irq,
400dd48af36SAlexander Motin 		      void *cookie)
401dd48af36SAlexander Motin {
402dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
403dd48af36SAlexander Motin 	int unit = (intptr_t)device_get_ivars(child);
404dd48af36SAlexander Motin 
405dd48af36SAlexander Motin 	ctlr->interrupt[unit].function = NULL;
406dd48af36SAlexander Motin 	ctlr->interrupt[unit].argument = NULL;
407dd48af36SAlexander Motin 	return (0);
408dd48af36SAlexander Motin }
409dd48af36SAlexander Motin 
410dd48af36SAlexander Motin static int
411dd48af36SAlexander Motin mvs_print_child(device_t dev, device_t child)
412dd48af36SAlexander Motin {
413dd48af36SAlexander Motin 	int retval;
414dd48af36SAlexander Motin 
415dd48af36SAlexander Motin 	retval = bus_print_child_header(dev, child);
416dd48af36SAlexander Motin 	retval += printf(" at channel %d",
417dd48af36SAlexander Motin 	    (int)(intptr_t)device_get_ivars(child));
418dd48af36SAlexander Motin 	retval += bus_print_child_footer(dev, child);
419dd48af36SAlexander Motin 
420dd48af36SAlexander Motin 	return (retval);
421dd48af36SAlexander Motin }
422dd48af36SAlexander Motin 
423445cc79cSAlexander Motin static int
424445cc79cSAlexander Motin mvs_child_location_str(device_t dev, device_t child, char *buf,
425445cc79cSAlexander Motin     size_t buflen)
426445cc79cSAlexander Motin {
427445cc79cSAlexander Motin 
428445cc79cSAlexander Motin 	snprintf(buf, buflen, "channel=%d",
429445cc79cSAlexander Motin 	    (int)(intptr_t)device_get_ivars(child));
430445cc79cSAlexander Motin 	return (0);
431445cc79cSAlexander Motin }
432445cc79cSAlexander Motin 
433*ca114192SAlexander Motin static bus_dma_tag_t
434*ca114192SAlexander Motin mvs_get_dma_tag(device_t bus, device_t child)
435*ca114192SAlexander Motin {
436*ca114192SAlexander Motin 
437*ca114192SAlexander Motin 	return (bus_get_dma_tag(bus));
438*ca114192SAlexander Motin }
439*ca114192SAlexander Motin 
440dd48af36SAlexander Motin static device_method_t mvs_methods[] = {
441dd48af36SAlexander Motin 	DEVMETHOD(device_probe,     mvs_probe),
442dd48af36SAlexander Motin 	DEVMETHOD(device_attach,    mvs_attach),
443dd48af36SAlexander Motin 	DEVMETHOD(device_detach,    mvs_detach),
444dd48af36SAlexander Motin 	DEVMETHOD(device_suspend,   mvs_suspend),
445dd48af36SAlexander Motin 	DEVMETHOD(device_resume,    mvs_resume),
446dd48af36SAlexander Motin 	DEVMETHOD(bus_print_child,  mvs_print_child),
447dd48af36SAlexander Motin 	DEVMETHOD(bus_alloc_resource,       mvs_alloc_resource),
448dd48af36SAlexander Motin 	DEVMETHOD(bus_release_resource,     mvs_release_resource),
449dd48af36SAlexander Motin 	DEVMETHOD(bus_setup_intr,   mvs_setup_intr),
450dd48af36SAlexander Motin 	DEVMETHOD(bus_teardown_intr,mvs_teardown_intr),
451445cc79cSAlexander Motin 	DEVMETHOD(bus_child_location_str, mvs_child_location_str),
452*ca114192SAlexander Motin 	DEVMETHOD(bus_get_dma_tag,  mvs_get_dma_tag),
453*ca114192SAlexander Motin 	DEVMETHOD(mvs_edma,         mvs_edma),
454dd48af36SAlexander Motin 	{ 0, 0 }
455dd48af36SAlexander Motin };
456dd48af36SAlexander Motin static driver_t mvs_driver = {
45701e2aa9fSAlexander Motin         "mvs",
458dd48af36SAlexander Motin         mvs_methods,
459dd48af36SAlexander Motin         sizeof(struct mvs_controller)
460dd48af36SAlexander Motin };
46101e2aa9fSAlexander Motin DRIVER_MODULE(mvs, simplebus, mvs_driver, mvs_devclass, 0, 0);
46201e2aa9fSAlexander Motin MODULE_VERSION(mvs, 1);
46301e2aa9fSAlexander Motin MODULE_DEPEND(mvs, cam, 1, 1, 1);
464