xref: /freebsd/sys/powerpc/psim/ata_iobus.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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 <machine/stdarg.h>
41 #include <machine/resource.h>
42 #include <machine/bus.h>
43 #include <sys/rman.h>
44 #include <sys/ata.h>
45 #include <dev/ata/ata-all.h>
46 
47 #include <dev/ofw/openfirm.h>
48 #include <powerpc/psim/iobusvar.h>
49 
50 /*
51  * Define the iobus ata bus attachment. This creates a pseudo-bus that
52  * the ATA device can be attached to
53  */
54 static  int  ata_iobus_attach(device_t dev);
55 static  int  ata_iobus_probe(device_t dev);
56 static  int  ata_iobus_print_child(device_t dev, device_t child);
57 struct resource *ata_iobus_alloc_resource(device_t, device_t, int, int *,
58 					  u_long, u_long, u_long, u_int);
59 static int ata_iobus_release_resource(device_t, device_t, int, int,
60 				      struct resource *);
61 
62 static device_method_t ata_iobus_methods[] = {
63         /* Device interface */
64 	DEVMETHOD(device_probe,		ata_iobus_probe),
65 	DEVMETHOD(device_attach,        ata_iobus_attach),
66 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
67 	DEVMETHOD(device_suspend,       bus_generic_suspend),
68 	DEVMETHOD(device_resume,        bus_generic_resume),
69 
70 	/* Bus methods */
71 	DEVMETHOD(bus_print_child,          ata_iobus_print_child),
72 	DEVMETHOD(bus_alloc_resource,       ata_iobus_alloc_resource),
73 	DEVMETHOD(bus_release_resource,     ata_iobus_release_resource),
74 	DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
75 	DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
76 	DEVMETHOD(bus_setup_intr,           bus_generic_setup_intr),
77 	DEVMETHOD(bus_teardown_intr,        bus_generic_teardown_intr),
78 
79 	{ 0, 0 }
80 };
81 
82 static driver_t ata_iobus_driver = {
83 	"ataiobus",
84 	ata_iobus_methods,
85 	0,
86 };
87 
88 static devclass_t ata_iobus_devclass;
89 
90 DRIVER_MODULE(ataiobus, iobus, ata_iobus_driver, ata_iobus_devclass, 0, 0);
91 
92 
93 static int
94 ata_iobus_probe(device_t dev)
95 {
96 	char *type = iobus_get_name(dev);
97 
98 	if (strncmp(type, "ata", 3) != 0)
99 		return (ENXIO);
100 
101 	device_set_desc(dev, "PSIM ATA Controller");
102 	return (0);
103 }
104 
105 
106 static int
107 ata_iobus_attach(device_t dev)
108 {
109 	/*
110 	 * Add a single child per controller. Should be able
111 	 * to add two
112 	 */
113 	device_add_child(dev, "ata",
114 			 devclass_find_free_unit(ata_devclass, 0));
115 
116 	return (bus_generic_attach(dev));
117 }
118 
119 
120 static int
121 ata_iobus_print_child(device_t dev, device_t child)
122 {
123     struct ata_channel *ch = device_get_softc(child);
124     int retval = 0;
125 
126     retval += bus_print_child_header(dev, child);
127     /* irq ? */
128     retval += bus_print_child_footer(dev, child);
129 
130     return (retval);
131 }
132 
133 
134 struct resource *
135 ata_iobus_alloc_resource(device_t dev, device_t child, int type, int *rid,
136 			 u_long start, u_long end, u_long count, u_int flags)
137 {
138 	struct resource *res = NULL;
139 	int myrid;
140 	u_int *ofw_regs;
141 
142 	ofw_regs = iobus_get_regs(dev);
143 
144 	/*
145 	 * The reg array for the PSIM ata device has 6 start/size entries:
146 	 *  0 - unused
147 	 *  1/2/3 - unused
148 	 *  4/5/6 - primary command
149 	 *  7/8/9 - secondary command
150 	 *  10/11/12 - primary control
151 	 *  13/14/15 - secondary control
152 	 *  16/17/18 - primary/secondary dma registers, unimplemented
153 	 *
154 	 *  The resource values are calculated from these registers
155 	 */
156 	if (type == SYS_RES_IOPORT) {
157 		switch (*rid) {
158 		case ATA_IOADDR_RID:
159 			myrid = 0;
160 			start = ofw_regs[4];
161 			end = start + ATA_IOSIZE - 1;
162 			count = ATA_IOSIZE;
163 			res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
164 						 SYS_RES_MEMORY, &myrid,
165 						 start, end, count, flags);
166 			break;
167 
168 		case ATA_ALTADDR_RID:
169 			myrid = 0;
170 			start = ofw_regs[10];
171 			end = start + ATA_ALTIOSIZE - 1;
172 			count = ATA_ALTIOSIZE;
173 			res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
174 						 SYS_RES_MEMORY, &myrid,
175 						 start, end, count, flags);
176 			break;
177 
178 		case ATA_BMADDR_RID:
179 			/* DMA not properly supported by psim */
180 			break;
181 		}
182 		return (res);
183 
184 	} else if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
185 		/*
186 		 * Pass this on to the parent
187 		 */
188 		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
189 					   SYS_RES_IRQ, rid, 0, ~0, 1, flags));
190 
191 	} else {
192 		return (NULL);
193 	}
194 }
195 
196 
197 static int
198 ata_iobus_release_resource(device_t dev, device_t child, int type, int rid,
199 			   struct resource *r)
200 {
201 	/* no hotplug... */
202 	return (0);
203 }
204 
205 
206 /*
207  * Define the actual ATA device. This is a sub-bus to the ata-iobus layer
208  * to allow the higher layer bus to massage the resource allocation.
209  */
210 
211 static  int  ata_iobus_sub_probe(device_t dev);
212 
213 static device_method_t ata_iobus_sub_methods[] = {
214 	/* Device interface */
215 	DEVMETHOD(device_probe,     ata_iobus_sub_probe),
216 	DEVMETHOD(device_attach,    ata_attach),
217 	DEVMETHOD(device_detach,    ata_detach),
218 	DEVMETHOD(device_resume,    ata_resume),
219 
220 	{ 0, 0 }
221 };
222 
223 static driver_t ata_iobus_sub_driver = {
224 	"ata",
225 	ata_iobus_sub_methods,
226 	sizeof(struct ata_channel),
227 };
228 
229 DRIVER_MODULE(ata, ataiobus, ata_iobus_sub_driver, ata_devclass, 0, 0);
230 
231 static void
232 ata_iobus_locknoop(struct ata_channel *ch, int type)
233 {
234 }
235 
236 static void
237 ata_iobus_setmode(struct ata_device *atadev, int mode)
238 {
239 	atadev->mode = ATA_PIO;
240 }
241 
242 static int
243 ata_iobus_sub_probe(device_t dev)
244 {
245 	struct ata_channel *ch = device_get_softc(dev);
246 
247 	/* Only a single unit per controller thus far */
248 	ch->unit = 0;
249 	ch->flags = (ATA_USE_16BIT|ATA_NO_SLAVE);
250 	ch->locking = ata_iobus_locknoop;
251 	ch->device[MASTER].setmode = ata_iobus_setmode;
252 	ch->device[SLAVE].setmode = ata_iobus_setmode;
253 
254 	return ata_probe(dev);
255 }
256