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