xref: /freebsd/sys/dev/mvs/mvs_soc.c (revision 5b56413d04e608379c9a306373554a8e4d321bc0)
1dd48af36SAlexander Motin /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4dd48af36SAlexander Motin  * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
5dd48af36SAlexander Motin  * All rights reserved.
6dd48af36SAlexander Motin  *
7dd48af36SAlexander Motin  * Redistribution and use in source and binary forms, with or without
8dd48af36SAlexander Motin  * modification, are permitted provided that the following conditions
9dd48af36SAlexander Motin  * are met:
10dd48af36SAlexander Motin  * 1. Redistributions of source code must retain the above copyright
11dd48af36SAlexander Motin  *    notice, this list of conditions and the following disclaimer,
12dd48af36SAlexander Motin  *    without modification, immediately at the beginning of the file.
13dd48af36SAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
14dd48af36SAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
15dd48af36SAlexander Motin  *    documentation and/or other materials provided with the distribution.
16dd48af36SAlexander Motin  *
17dd48af36SAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18dd48af36SAlexander Motin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19dd48af36SAlexander Motin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20dd48af36SAlexander Motin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21dd48af36SAlexander Motin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22dd48af36SAlexander Motin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23dd48af36SAlexander Motin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24dd48af36SAlexander Motin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25dd48af36SAlexander Motin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26dd48af36SAlexander Motin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27dd48af36SAlexander Motin  */
28dd48af36SAlexander Motin 
29dd48af36SAlexander Motin #include <sys/param.h>
30dd48af36SAlexander Motin #include <sys/module.h>
31dd48af36SAlexander Motin #include <sys/systm.h>
32dd48af36SAlexander Motin #include <sys/kernel.h>
33dd48af36SAlexander Motin #include <sys/bus.h>
34dd48af36SAlexander Motin #include <sys/endian.h>
35dd48af36SAlexander Motin #include <sys/malloc.h>
36dd48af36SAlexander Motin #include <sys/lock.h>
37dd48af36SAlexander Motin #include <sys/mutex.h>
38dd48af36SAlexander Motin #include <vm/uma.h>
39dd48af36SAlexander Motin #include <machine/stdarg.h>
40dd48af36SAlexander Motin #include <machine/resource.h>
41dd48af36SAlexander Motin #include <machine/bus.h>
42dd48af36SAlexander Motin #include <sys/rman.h>
43ddfc9c4cSWarner Losh #include <sys/sbuf.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 	int i;
78dd48af36SAlexander Motin 	uint32_t devid, revid;
79dd48af36SAlexander Motin 
80add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
81add35ed5SIan Lepore 		return (ENXIO);
82add35ed5SIan Lepore 
8301e2aa9fSAlexander Motin 	if (!ofw_bus_is_compatible(dev, "mrvl,sata"))
8401e2aa9fSAlexander Motin 		return (ENXIO);
8501e2aa9fSAlexander Motin 
86dd48af36SAlexander Motin 	soc_id(&devid, &revid);
87dd48af36SAlexander Motin 	for (i = 0; mvs_ids[i].id != 0; i++) {
88dd48af36SAlexander Motin 		if (mvs_ids[i].id == devid &&
89dd48af36SAlexander Motin 		    mvs_ids[i].rev <= revid) {
90a3b460d4SMark Johnston 			device_set_descf(dev, "%s SATA controller",
91dd48af36SAlexander Motin 			    mvs_ids[i].name);
923036de3cSAlexander Motin 			return (BUS_PROBE_DEFAULT);
93dd48af36SAlexander Motin 		}
94dd48af36SAlexander Motin 	}
95dd48af36SAlexander Motin 	return (ENXIO);
96dd48af36SAlexander Motin }
97dd48af36SAlexander Motin 
98dd48af36SAlexander Motin static int
99dd48af36SAlexander Motin mvs_attach(device_t dev)
100dd48af36SAlexander Motin {
101dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
102dd48af36SAlexander Motin 	device_t child;
103dd48af36SAlexander Motin 	int	error, unit, i;
104dd48af36SAlexander Motin 	uint32_t devid, revid;
105dd48af36SAlexander Motin 
106dd48af36SAlexander Motin 	soc_id(&devid, &revid);
107dd48af36SAlexander Motin 	ctlr->dev = dev;
108dd48af36SAlexander Motin 	i = 0;
109dd48af36SAlexander Motin 	while (mvs_ids[i].id != 0 &&
110dd48af36SAlexander Motin 	    (mvs_ids[i].id != devid ||
111dd48af36SAlexander Motin 	     mvs_ids[i].rev > revid))
112dd48af36SAlexander Motin 		i++;
113dd48af36SAlexander Motin 	ctlr->channels = mvs_ids[i].ports;
114dd48af36SAlexander Motin 	ctlr->quirks = mvs_ids[i].quirks;
115200b4021SAlexander Motin 	ctlr->ccc = 0;
116dd48af36SAlexander Motin 	resource_int_value(device_get_name(dev),
117dd48af36SAlexander Motin 	    device_get_unit(dev), "ccc", &ctlr->ccc);
118dd48af36SAlexander Motin 	ctlr->cccc = 8;
119dd48af36SAlexander Motin 	resource_int_value(device_get_name(dev),
120dd48af36SAlexander Motin 	    device_get_unit(dev), "cccc", &ctlr->cccc);
121dd48af36SAlexander Motin 	if (ctlr->ccc == 0 || ctlr->cccc == 0) {
122dd48af36SAlexander Motin 		ctlr->ccc = 0;
123dd48af36SAlexander Motin 		ctlr->cccc = 0;
124dd48af36SAlexander Motin 	}
125dd48af36SAlexander Motin 	if (ctlr->ccc > 100000)
126dd48af36SAlexander Motin 		ctlr->ccc = 100000;
127dd48af36SAlexander Motin 	device_printf(dev,
128dd48af36SAlexander Motin 	    "Gen-%s, %d %sGbps ports, Port Multiplier %s%s\n",
129dd48af36SAlexander Motin 	    ((ctlr->quirks & MVS_Q_GENI) ? "I" :
130dd48af36SAlexander Motin 	     ((ctlr->quirks & MVS_Q_GENII) ? "II" : "IIe")),
131dd48af36SAlexander Motin 	    ctlr->channels,
132dd48af36SAlexander Motin 	    ((ctlr->quirks & MVS_Q_GENI) ? "1.5" : "3"),
133dd48af36SAlexander Motin 	    ((ctlr->quirks & MVS_Q_GENI) ?
134dd48af36SAlexander Motin 	    "not supported" : "supported"),
135dd48af36SAlexander Motin 	    ((ctlr->quirks & MVS_Q_GENIIE) ?
136dd48af36SAlexander Motin 	    " with FBS" : ""));
137dd48af36SAlexander Motin 	mtx_init(&ctlr->mtx, "MVS controller lock", NULL, MTX_DEF);
138dd48af36SAlexander Motin 	/* We should have a memory BAR(0). */
139dd48af36SAlexander Motin 	ctlr->r_rid = 0;
140dd48af36SAlexander Motin 	if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
141dd48af36SAlexander Motin 	    &ctlr->r_rid, RF_ACTIVE)))
142dd48af36SAlexander Motin 		return ENXIO;
143b30c7d51SAlexander Motin 	if (ATA_INL(ctlr->r_mem, PORT_BASE(0) + SATA_PHYCFG_OFS) != 0)
144b30c7d51SAlexander Motin 		ctlr->quirks |= MVS_Q_SOC65;
145dd48af36SAlexander Motin 	/* Setup our own memory management for channels. */
146cc6b610bSAlexander Motin 	ctlr->sc_iomem.rm_start = rman_get_start(ctlr->r_mem);
147cc6b610bSAlexander Motin 	ctlr->sc_iomem.rm_end = rman_get_end(ctlr->r_mem);
148dd48af36SAlexander Motin 	ctlr->sc_iomem.rm_type = RMAN_ARRAY;
149dd48af36SAlexander Motin 	ctlr->sc_iomem.rm_descr = "I/O memory addresses";
150dd48af36SAlexander Motin 	if ((error = rman_init(&ctlr->sc_iomem)) != 0) {
151dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
152dd48af36SAlexander Motin 		return (error);
153dd48af36SAlexander Motin 	}
154dd48af36SAlexander Motin 	if ((error = rman_manage_region(&ctlr->sc_iomem,
155dd48af36SAlexander Motin 	    rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) {
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 (error);
159dd48af36SAlexander Motin 	}
160dd48af36SAlexander Motin 	mvs_ctlr_setup(dev);
161dd48af36SAlexander Motin 	/* Setup interrupts. */
162dd48af36SAlexander Motin 	if (mvs_setup_interrupt(dev)) {
163dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
164dd48af36SAlexander Motin 		rman_fini(&ctlr->sc_iomem);
165dd48af36SAlexander Motin 		return ENXIO;
166dd48af36SAlexander Motin 	}
167dd48af36SAlexander Motin 	/* Attach all channels on this controller */
168dd48af36SAlexander Motin 	for (unit = 0; unit < ctlr->channels; unit++) {
169*5b56413dSWarner Losh 		child = device_add_child(dev, "mvsch", DEVICE_UNIT_ANY);
170dd48af36SAlexander Motin 		if (child == NULL)
171dd48af36SAlexander Motin 			device_printf(dev, "failed to add channel device\n");
172dd48af36SAlexander Motin 		else
173dd48af36SAlexander Motin 			device_set_ivars(child, (void *)(intptr_t)unit);
174dd48af36SAlexander Motin 	}
175dd48af36SAlexander Motin 	bus_generic_attach(dev);
176dd48af36SAlexander Motin 	return 0;
177dd48af36SAlexander Motin }
178dd48af36SAlexander Motin 
179dd48af36SAlexander Motin static int
180dd48af36SAlexander Motin mvs_detach(device_t dev)
181dd48af36SAlexander Motin {
182dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
183dd48af36SAlexander Motin 
184dd48af36SAlexander Motin 	/* Detach & delete all children */
1853b12bdb5SHans Petter Selasky 	device_delete_children(dev);
18611bcf702SHans Petter Selasky 
187dd48af36SAlexander Motin 	/* Free interrupt. */
188dd48af36SAlexander Motin 	if (ctlr->irq.r_irq) {
189dd48af36SAlexander Motin 		bus_teardown_intr(dev, ctlr->irq.r_irq,
190dd48af36SAlexander Motin 		    ctlr->irq.handle);
191dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_IRQ,
192dd48af36SAlexander Motin 		    ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
193dd48af36SAlexander Motin 	}
194dd48af36SAlexander Motin 	/* Free memory. */
195dd48af36SAlexander Motin 	rman_fini(&ctlr->sc_iomem);
196dd48af36SAlexander Motin 	if (ctlr->r_mem)
197dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
198dd48af36SAlexander Motin 	mtx_destroy(&ctlr->mtx);
199dd48af36SAlexander Motin 	return (0);
200dd48af36SAlexander Motin }
201dd48af36SAlexander Motin 
202dd48af36SAlexander Motin static int
203dd48af36SAlexander Motin mvs_ctlr_setup(device_t dev)
204dd48af36SAlexander Motin {
205dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
20617dce737SWarner Losh 	int ccc = ctlr->ccc, cccc = ctlr->cccc;
207dd48af36SAlexander Motin 
208dd48af36SAlexander Motin 	/* Mask chip interrupts */
209dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIM, 0x00000000);
210dd48af36SAlexander Motin 	/* Clear HC interrupts */
211dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, HC_IC, 0x00000000);
212dd48af36SAlexander Motin 	/* Clear chip interrupts */
213dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIC, 0);
214dd48af36SAlexander Motin 	/* Configure per-HC CCC */
215dd48af36SAlexander Motin 	if (ccc && bootverbose) {
216dd48af36SAlexander Motin 		device_printf(dev,
217dd48af36SAlexander Motin 		    "CCC with %dus/%dcmd enabled\n",
218dd48af36SAlexander Motin 		    ctlr->ccc, ctlr->cccc);
219dd48af36SAlexander Motin 	}
220dd48af36SAlexander Motin 	ccc *= 150;
221dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, HC_ICT, cccc);
222dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, HC_ITT, ccc);
223dd48af36SAlexander Motin 	/* Enable chip interrupts */
224c72ef339SRafal Jaworowski 	ctlr->gmim = ((ccc ? IC_HC0_COAL_DONE :
225c72ef339SRafal Jaworowski 	    (IC_DONE_HC0 & CHIP_SOC_HC0_MASK(ctlr->channels))) |
226c72ef339SRafal Jaworowski 	    (IC_ERR_HC0 & CHIP_SOC_HC0_MASK(ctlr->channels)));
227dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIM, ctlr->gmim | ctlr->pmim);
228dd48af36SAlexander Motin 	return (0);
229dd48af36SAlexander Motin }
230dd48af36SAlexander Motin 
231dd48af36SAlexander Motin static void
232dd48af36SAlexander Motin mvs_edma(device_t dev, device_t child, int mode)
233dd48af36SAlexander Motin {
234dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
235dd48af36SAlexander Motin 	int unit = ((struct mvs_channel *)device_get_softc(child))->unit;
236dd48af36SAlexander Motin 	int bit = IC_DONE_IRQ << (unit * 2);
237dd48af36SAlexander Motin 
238dd48af36SAlexander Motin 	if (ctlr->ccc == 0)
239dd48af36SAlexander Motin 		return;
240dd48af36SAlexander Motin 	/* CCC is not working for non-EDMA mode. Unmask device interrupts. */
241dd48af36SAlexander Motin 	mtx_lock(&ctlr->mtx);
242dd48af36SAlexander Motin 	if (mode == MVS_EDMA_OFF)
243dd48af36SAlexander Motin 		ctlr->pmim |= bit;
244dd48af36SAlexander Motin 	else
245dd48af36SAlexander Motin 		ctlr->pmim &= ~bit;
246dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIM, ctlr->gmim | ctlr->pmim);
247dd48af36SAlexander Motin 	mtx_unlock(&ctlr->mtx);
248dd48af36SAlexander Motin }
249dd48af36SAlexander Motin 
250dd48af36SAlexander Motin static int
251dd48af36SAlexander Motin mvs_suspend(device_t dev)
252dd48af36SAlexander Motin {
253dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
254dd48af36SAlexander Motin 
255dd48af36SAlexander Motin 	bus_generic_suspend(dev);
256dd48af36SAlexander Motin 	/* Mask chip interrupts */
257dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIM, 0x00000000);
258dd48af36SAlexander Motin 	return 0;
259dd48af36SAlexander Motin }
260dd48af36SAlexander Motin 
261dd48af36SAlexander Motin static int
262dd48af36SAlexander Motin mvs_resume(device_t dev)
263dd48af36SAlexander Motin {
264dd48af36SAlexander Motin 
265dd48af36SAlexander Motin 	mvs_ctlr_setup(dev);
266dd48af36SAlexander Motin 	return (bus_generic_resume(dev));
267dd48af36SAlexander Motin }
268dd48af36SAlexander Motin 
269dd48af36SAlexander Motin static int
270dd48af36SAlexander Motin mvs_setup_interrupt(device_t dev)
271dd48af36SAlexander Motin {
272dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
273dd48af36SAlexander Motin 
274dd48af36SAlexander Motin 	/* Allocate all IRQs. */
275dd48af36SAlexander Motin 	ctlr->irq.r_irq_rid = 0;
276dd48af36SAlexander Motin 	if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
277dd48af36SAlexander Motin 	    &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
278dd48af36SAlexander Motin 		device_printf(dev, "unable to map interrupt\n");
279dd48af36SAlexander Motin 		return (ENXIO);
280dd48af36SAlexander Motin 	}
281dd48af36SAlexander Motin 	if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL,
282dd48af36SAlexander Motin 	    mvs_intr, ctlr, &ctlr->irq.handle))) {
283dd48af36SAlexander Motin 		device_printf(dev, "unable to setup interrupt\n");
284dd48af36SAlexander Motin 		bus_release_resource(dev, SYS_RES_IRQ,
285dd48af36SAlexander Motin 		    ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
28687d8fcc8SPedro F. Giffuni 		ctlr->irq.r_irq = NULL;
287dd48af36SAlexander Motin 		return (ENXIO);
288dd48af36SAlexander Motin 	}
289dd48af36SAlexander Motin 	return (0);
290dd48af36SAlexander Motin }
291dd48af36SAlexander Motin 
292dd48af36SAlexander Motin /*
293dd48af36SAlexander Motin  * Common case interrupt handler.
294dd48af36SAlexander Motin  */
295dd48af36SAlexander Motin static void
296dd48af36SAlexander Motin mvs_intr(void *data)
297dd48af36SAlexander Motin {
298dd48af36SAlexander Motin 	struct mvs_controller *ctlr = data;
299dd48af36SAlexander Motin 	struct mvs_intr_arg arg;
300dd48af36SAlexander Motin 	void (*function)(void *);
301c72ef339SRafal Jaworowski 	int p, chan_num;
302dd48af36SAlexander Motin 	u_int32_t ic, aic;
303dd48af36SAlexander Motin 
304dd48af36SAlexander Motin 	ic = ATA_INL(ctlr->r_mem, CHIP_SOC_MIC);
305dd48af36SAlexander Motin 	if ((ic & IC_HC0) == 0)
306dd48af36SAlexander Motin 		return;
307c72ef339SRafal Jaworowski 
308dd48af36SAlexander Motin 	/* Acknowledge interrupts of this HC. */
309dd48af36SAlexander Motin 	aic = 0;
310c72ef339SRafal Jaworowski 
311c72ef339SRafal Jaworowski 	/* Processing interrupts from each initialized channel */
312c72ef339SRafal Jaworowski 	for (chan_num = 0; chan_num < ctlr->channels; chan_num++) {
313c72ef339SRafal Jaworowski 		if (ic & (IC_DONE_IRQ << (chan_num * 2)))
314c72ef339SRafal Jaworowski 			aic |= HC_IC_DONE(chan_num) | HC_IC_DEV(chan_num);
315c72ef339SRafal Jaworowski 	}
316c72ef339SRafal Jaworowski 
317dd48af36SAlexander Motin 	if (ic & IC_HC0_COAL_DONE)
318dd48af36SAlexander Motin 		aic |= HC_IC_COAL;
319dd48af36SAlexander Motin 	ATA_OUTL(ctlr->r_mem, HC_IC, ~aic);
320c72ef339SRafal Jaworowski 
321dd48af36SAlexander Motin 	/* Call per-port interrupt handler. */
322dd48af36SAlexander Motin 	for (p = 0; p < ctlr->channels; p++) {
323dd48af36SAlexander Motin 		arg.cause = ic & (IC_ERR_IRQ|IC_DONE_IRQ);
324dd48af36SAlexander Motin 		if ((arg.cause != 0) &&
325dd48af36SAlexander Motin 		    (function = ctlr->interrupt[p].function)) {
326dd48af36SAlexander Motin 			arg.arg = ctlr->interrupt[p].argument;
327dd48af36SAlexander Motin 			function(&arg);
328dd48af36SAlexander Motin 		}
329dd48af36SAlexander Motin 		ic >>= 2;
330dd48af36SAlexander Motin 	}
331dd48af36SAlexander Motin }
332dd48af36SAlexander Motin 
333dd48af36SAlexander Motin static struct resource *
334dd48af36SAlexander Motin mvs_alloc_resource(device_t dev, device_t child, int type, int *rid,
3352dd1bdf1SJustin Hibbits 		   rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
336dd48af36SAlexander Motin {
337dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
338dd48af36SAlexander Motin 	int unit = ((struct mvs_channel *)device_get_softc(child))->unit;
339dd48af36SAlexander Motin 	struct resource *res = NULL;
340dd48af36SAlexander Motin 	int offset = PORT_BASE(unit & 0x03);
341d8d7c6b1SJustin Hibbits 	rman_res_t st;
342dd48af36SAlexander Motin 
343dd48af36SAlexander Motin 	switch (type) {
344dd48af36SAlexander Motin 	case SYS_RES_MEMORY:
345dd48af36SAlexander Motin 		st = rman_get_start(ctlr->r_mem);
346dd48af36SAlexander Motin 		res = rman_reserve_resource(&ctlr->sc_iomem, st + offset,
347dd48af36SAlexander Motin 		    st + offset + PORT_SIZE - 1, PORT_SIZE, RF_ACTIVE, child);
348dd48af36SAlexander Motin 		if (res) {
349dd48af36SAlexander Motin 			bus_space_handle_t bsh;
350dd48af36SAlexander Motin 			bus_space_tag_t bst;
351dd48af36SAlexander Motin 			bsh = rman_get_bushandle(ctlr->r_mem);
352dd48af36SAlexander Motin 			bst = rman_get_bustag(ctlr->r_mem);
353dd48af36SAlexander Motin 			bus_space_subregion(bst, bsh, offset, PORT_SIZE, &bsh);
354dd48af36SAlexander Motin 			rman_set_bushandle(res, bsh);
355dd48af36SAlexander Motin 			rman_set_bustag(res, bst);
356dd48af36SAlexander Motin 		}
357dd48af36SAlexander Motin 		break;
358dd48af36SAlexander Motin 	case SYS_RES_IRQ:
359dd48af36SAlexander Motin 		if (*rid == ATA_IRQ_RID)
360dd48af36SAlexander Motin 			res = ctlr->irq.r_irq;
361dd48af36SAlexander Motin 		break;
362dd48af36SAlexander Motin 	}
363dd48af36SAlexander Motin 	return (res);
364dd48af36SAlexander Motin }
365dd48af36SAlexander Motin 
366dd48af36SAlexander Motin static int
3679dbf5b0eSJohn Baldwin mvs_release_resource(device_t dev, device_t child, struct resource *r)
368dd48af36SAlexander Motin {
369dd48af36SAlexander Motin 
3709dbf5b0eSJohn Baldwin 	switch (rman_get_type(r)) {
371dd48af36SAlexander Motin 	case SYS_RES_MEMORY:
372dd48af36SAlexander Motin 		rman_release_resource(r);
373dd48af36SAlexander Motin 		return (0);
374dd48af36SAlexander Motin 	case SYS_RES_IRQ:
375c24e5fccSJessica Clarke 		if (rman_get_rid(r) != ATA_IRQ_RID)
376dd48af36SAlexander Motin 			return ENOENT;
377dd48af36SAlexander Motin 		return (0);
378dd48af36SAlexander Motin 	}
379dd48af36SAlexander Motin 	return (EINVAL);
380dd48af36SAlexander Motin }
381dd48af36SAlexander Motin 
382dd48af36SAlexander Motin static int
383dd48af36SAlexander Motin mvs_setup_intr(device_t dev, device_t child, struct resource *irq,
384dd48af36SAlexander Motin 		   int flags, driver_filter_t *filter, driver_intr_t *function,
385dd48af36SAlexander Motin 		   void *argument, void **cookiep)
386dd48af36SAlexander Motin {
387dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
388dd48af36SAlexander Motin 	int unit = (intptr_t)device_get_ivars(child);
389dd48af36SAlexander Motin 
390dd48af36SAlexander Motin 	if (filter != NULL) {
391dd48af36SAlexander Motin 		printf("mvs.c: we cannot use a filter here\n");
392dd48af36SAlexander Motin 		return (EINVAL);
393dd48af36SAlexander Motin 	}
394dd48af36SAlexander Motin 	ctlr->interrupt[unit].function = function;
395dd48af36SAlexander Motin 	ctlr->interrupt[unit].argument = argument;
396dd48af36SAlexander Motin 	return (0);
397dd48af36SAlexander Motin }
398dd48af36SAlexander Motin 
399dd48af36SAlexander Motin static int
400dd48af36SAlexander Motin mvs_teardown_intr(device_t dev, device_t child, struct resource *irq,
401dd48af36SAlexander Motin 		      void *cookie)
402dd48af36SAlexander Motin {
403dd48af36SAlexander Motin 	struct mvs_controller *ctlr = device_get_softc(dev);
404dd48af36SAlexander Motin 	int unit = (intptr_t)device_get_ivars(child);
405dd48af36SAlexander Motin 
406dd48af36SAlexander Motin 	ctlr->interrupt[unit].function = NULL;
407dd48af36SAlexander Motin 	ctlr->interrupt[unit].argument = NULL;
408dd48af36SAlexander Motin 	return (0);
409dd48af36SAlexander Motin }
410dd48af36SAlexander Motin 
411dd48af36SAlexander Motin static int
412dd48af36SAlexander Motin mvs_print_child(device_t dev, device_t child)
413dd48af36SAlexander Motin {
414dd48af36SAlexander Motin 	int retval;
415dd48af36SAlexander Motin 
416dd48af36SAlexander Motin 	retval = bus_print_child_header(dev, child);
417dd48af36SAlexander Motin 	retval += printf(" at channel %d",
418dd48af36SAlexander Motin 	    (int)(intptr_t)device_get_ivars(child));
419dd48af36SAlexander Motin 	retval += bus_print_child_footer(dev, child);
420dd48af36SAlexander Motin 
421dd48af36SAlexander Motin 	return (retval);
422dd48af36SAlexander Motin }
423dd48af36SAlexander Motin 
424445cc79cSAlexander Motin static int
425ddfc9c4cSWarner Losh mvs_child_location(device_t dev, device_t child, struct sbuf *sb)
426445cc79cSAlexander Motin {
427445cc79cSAlexander Motin 
428ddfc9c4cSWarner Losh 	sbuf_printf(sb, "channel=%d", (int)(intptr_t)device_get_ivars(child));
429445cc79cSAlexander Motin 	return (0);
430445cc79cSAlexander Motin }
431445cc79cSAlexander Motin 
432ca114192SAlexander Motin static bus_dma_tag_t
433ca114192SAlexander Motin mvs_get_dma_tag(device_t bus, device_t child)
434ca114192SAlexander Motin {
435ca114192SAlexander Motin 
436ca114192SAlexander Motin 	return (bus_get_dma_tag(bus));
437ca114192SAlexander Motin }
438ca114192SAlexander Motin 
439dd48af36SAlexander Motin static device_method_t mvs_methods[] = {
440dd48af36SAlexander Motin 	DEVMETHOD(device_probe,     mvs_probe),
441dd48af36SAlexander Motin 	DEVMETHOD(device_attach,    mvs_attach),
442dd48af36SAlexander Motin 	DEVMETHOD(device_detach,    mvs_detach),
443dd48af36SAlexander Motin 	DEVMETHOD(device_suspend,   mvs_suspend),
444dd48af36SAlexander Motin 	DEVMETHOD(device_resume,    mvs_resume),
445dd48af36SAlexander Motin 	DEVMETHOD(bus_print_child,  mvs_print_child),
446dd48af36SAlexander Motin 	DEVMETHOD(bus_alloc_resource,       mvs_alloc_resource),
447dd48af36SAlexander Motin 	DEVMETHOD(bus_release_resource,     mvs_release_resource),
448dd48af36SAlexander Motin 	DEVMETHOD(bus_setup_intr,   mvs_setup_intr),
449dd48af36SAlexander Motin 	DEVMETHOD(bus_teardown_intr,mvs_teardown_intr),
450ddfc9c4cSWarner Losh 	DEVMETHOD(bus_child_location, mvs_child_location),
451ca114192SAlexander Motin 	DEVMETHOD(bus_get_dma_tag,  mvs_get_dma_tag),
452ca114192SAlexander Motin 	DEVMETHOD(mvs_edma,         mvs_edma),
453dd48af36SAlexander Motin 	{ 0, 0 }
454dd48af36SAlexander Motin };
455dd48af36SAlexander Motin static driver_t mvs_driver = {
45601e2aa9fSAlexander Motin         "mvs",
457dd48af36SAlexander Motin         mvs_methods,
458dd48af36SAlexander Motin         sizeof(struct mvs_controller)
459dd48af36SAlexander Motin };
460827252eeSJohn Baldwin DRIVER_MODULE(mvs, simplebus, mvs_driver, 0, 0);
46101e2aa9fSAlexander Motin MODULE_VERSION(mvs, 1);
46201e2aa9fSAlexander Motin MODULE_DEPEND(mvs, cam, 1, 1, 1);
463