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