xref: /freebsd/sys/powerpc/psim/ata_iobus.c (revision 06c3fb27)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright 2002 by Peter Grehan. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 /*
32  * PSIM local bus ATA controller
33  */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/malloc.h>
40 #include <sys/sema.h>
41 #include <sys/taskqueue.h>
42 #include <machine/stdarg.h>
43 #include <vm/uma.h>
44 #include <machine/resource.h>
45 #include <machine/bus.h>
46 #include <sys/rman.h>
47 #include <sys/ata.h>
48 #include <dev/ata/ata-all.h>
49 #include <ata_if.h>
50 
51 #include <dev/ofw/openfirm.h>
52 #include <powerpc/psim/iobusvar.h>
53 
54 /*
55  * Define the iobus ata bus attachment. This creates a pseudo-bus that
56  * the ATA device can be attached to
57  */
58 static  int  ata_iobus_attach(device_t dev);
59 static  int  ata_iobus_probe(device_t dev);
60 static  int  ata_iobus_print_child(device_t dev, device_t child);
61 struct resource *ata_iobus_alloc_resource(device_t, device_t, int, int *,
62 					  rman_res_t, rman_res_t, rman_res_t,
63 					  u_int);
64 static int ata_iobus_release_resource(device_t, device_t, int, int,
65 				      struct resource *);
66 
67 static device_method_t ata_iobus_methods[] = {
68         /* Device interface */
69 	DEVMETHOD(device_probe,		ata_iobus_probe),
70 	DEVMETHOD(device_attach,        ata_iobus_attach),
71 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
72 	DEVMETHOD(device_suspend,       bus_generic_suspend),
73 	DEVMETHOD(device_resume,        bus_generic_resume),
74 
75 	/* Bus methods */
76 	DEVMETHOD(bus_print_child,          ata_iobus_print_child),
77 	DEVMETHOD(bus_alloc_resource,       ata_iobus_alloc_resource),
78 	DEVMETHOD(bus_release_resource,     ata_iobus_release_resource),
79 	DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
80 	DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
81 	DEVMETHOD(bus_setup_intr,           bus_generic_setup_intr),
82 	DEVMETHOD(bus_teardown_intr,        bus_generic_teardown_intr),
83 
84 	DEVMETHOD_END
85 };
86 
87 static driver_t ata_iobus_driver = {
88 	"ataiobus",
89 	ata_iobus_methods,
90 	0,
91 };
92 
93 DRIVER_MODULE(ataiobus, iobus, ata_iobus_driver, NULL, NULL);
94 MODULE_DEPEND(ata, ata, 1, 1, 1);
95 
96 static int
97 ata_iobus_probe(device_t dev)
98 {
99 	char *type = iobus_get_name(dev);
100 
101 	if (strncmp(type, "ata", 3) != 0)
102 		return (ENXIO);
103 
104 	device_set_desc(dev, "PSIM ATA Controller");
105 	return (0);
106 }
107 
108 static int
109 ata_iobus_attach(device_t dev)
110 {
111 	/*
112 	 * Add a single child per controller. Should be able
113 	 * to add two
114 	 */
115 	device_add_child(dev, "ata", -1);
116 	return (bus_generic_attach(dev));
117 }
118 
119 static int
120 ata_iobus_print_child(device_t dev, device_t child)
121 {
122 	int retval = 0;
123 
124 	retval += bus_print_child_header(dev, child);
125 	/* irq ? */
126 	retval += bus_print_child_footer(dev, child);
127 
128 	return (retval);
129 }
130 
131 struct resource *
132 ata_iobus_alloc_resource(device_t dev, device_t child, int type, int *rid,
133 			 rman_res_t start, rman_res_t end, rman_res_t count,
134 			 u_int flags)
135 {
136 	struct resource *res = NULL;
137 	int myrid;
138 	u_int *ofw_regs;
139 
140 	ofw_regs = iobus_get_regs(dev);
141 
142 	/*
143 	 * The reg array for the PSIM ata device has 6 start/size entries:
144 	 *  0 - unused
145 	 *  1/2/3 - unused
146 	 *  4/5/6 - primary command
147 	 *  7/8/9 - secondary command
148 	 *  10/11/12 - primary control
149 	 *  13/14/15 - secondary control
150 	 *  16/17/18 - primary/secondary dma registers, unimplemented
151 	 *
152 	 *  The resource values are calculated from these registers
153 	 */
154 	if (type == SYS_RES_IOPORT) {
155 		switch (*rid) {
156 		case ATA_IOADDR_RID:
157 			myrid = 0;
158 			start = ofw_regs[4];
159 			end = start + ATA_IOSIZE - 1;
160 			count = ATA_IOSIZE;
161 			res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
162 						 SYS_RES_MEMORY, &myrid,
163 						 start, end, count, flags);
164 			break;
165 
166 		case ATA_CTLADDR_RID:
167 			myrid = 0;
168 			start = ofw_regs[10];
169 			end = start + ATA_CTLIOSIZE - 1;
170 			count = ATA_CTLIOSIZE;
171 			res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
172 						 SYS_RES_MEMORY, &myrid,
173 						 start, end, count, flags);
174 			break;
175 
176 		case ATA_BMADDR_RID:
177 			/* DMA not properly supported by psim */
178 			break;
179 		}
180 		return (res);
181 
182 	} else if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
183 		/*
184 		 * Pass this on to the parent
185 		 */
186 		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
187 					   SYS_RES_IRQ, rid, 0, ~0, 1, flags));
188 
189 	} else {
190 		return (NULL);
191 	}
192 }
193 
194 static int
195 ata_iobus_release_resource(device_t dev, device_t child, int type, int rid,
196 			   struct resource *r)
197 {
198 	/* no hotplug... */
199 	return (0);
200 }
201 
202 /*
203  * Define the actual ATA device. This is a sub-bus to the ata-iobus layer
204  * to allow the higher layer bus to massage the resource allocation.
205  */
206 
207 static  int  ata_iobus_sub_probe(device_t dev);
208 static  int  ata_iobus_sub_setmode(device_t dev, int target, int mode);
209 
210 static device_method_t ata_iobus_sub_methods[] = {
211 	/* Device interface */
212 	DEVMETHOD(device_probe,     ata_iobus_sub_probe),
213 	DEVMETHOD(device_attach,    ata_attach),
214 	DEVMETHOD(device_detach,    ata_detach),
215 	DEVMETHOD(device_resume,    ata_resume),
216 
217 	/* ATA interface */
218 	DEVMETHOD(ata_setmode,	    ata_iobus_sub_setmode),
219 	DEVMETHOD_END
220 };
221 
222 static driver_t ata_iobus_sub_driver = {
223 	"ata",
224 	ata_iobus_sub_methods,
225 	sizeof(struct ata_channel),
226 };
227 
228 DRIVER_MODULE(ata, ataiobus, ata_iobus_sub_driver, NULL, NULL);
229 
230 static int
231 ata_iobus_sub_probe(device_t dev)
232 {
233 	struct ata_channel *ch = device_get_softc(dev);
234 
235 	/* Only a single unit per controller thus far */
236 	ch->unit = 0;
237 	ch->flags = (ATA_USE_16BIT|ATA_NO_SLAVE);
238 	ata_generic_hw(dev);
239 
240 	return ata_probe(dev);
241 }
242 
243 static int
244 ata_iobus_sub_setmode(device_t parent, int target, int mode)
245 {
246 	/* Only ever PIO mode here... */
247 	return (ATA_PIO);
248 }
249