xref: /freebsd/sys/powerpc/psim/ata_iobus.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
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     retval += printf(": at 0x%lx", rman_get_start(ch->r_io));
128     /* irq ? */
129     retval += bus_print_child_footer(dev, child);
130 
131     return (retval);
132 }
133 
134 
135 struct resource *
136 ata_iobus_alloc_resource(device_t dev, device_t child, int type, int *rid,
137 			 u_long start, u_long end, u_long count, u_int flags)
138 {
139 	struct resource *res = NULL;
140 	int myrid;
141 	u_int *ofw_regs;
142 
143 	ofw_regs = iobus_get_regs(dev);
144 
145 	/*
146 	 * The reg array for the PSIM ata device has 6 start/size entries:
147 	 *  0 - unused
148 	 *  1/2/3 - unused
149 	 *  4/5/6 - primary command
150 	 *  7/8/9 - secondary command
151 	 *  10/11/12 - primary control
152 	 *  13/14/15 - secondary control
153 	 *  16/17/18 - primary/secondary dma registers, unimplemented
154 	 *
155 	 *  The resource values are calculated from these registers
156 	 */
157 	if (type == SYS_RES_IOPORT) {
158 		switch (*rid) {
159 		case ATA_IOADDR_RID:
160 			myrid = 0;
161 			start = ofw_regs[4];
162 			end = start + ATA_IOSIZE - 1;
163 			count = ATA_IOSIZE;
164 			res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
165 						 SYS_RES_MEMORY, &myrid,
166 						 start, end, count, flags);
167 			break;
168 
169 		case ATA_ALTADDR_RID:
170 			myrid = 0;
171 			start = ofw_regs[10];
172 			end = start + ATA_ALTIOSIZE - 1;
173 			count = ATA_ALTIOSIZE;
174 			res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
175 						 SYS_RES_MEMORY, &myrid,
176 						 start, end, count, flags);
177 			break;
178 
179 		case ATA_BMADDR_RID:
180 			/* DMA not properly supported by psim */
181 			break;
182 		}
183 		return (res);
184 
185 	} else if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
186 		/*
187 		 * Pass this on to the parent
188 		 */
189 		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
190 					   SYS_RES_IRQ, rid, 0, ~0, 1, flags));
191 
192 	} else {
193 		return (NULL);
194 	}
195 }
196 
197 
198 static int
199 ata_iobus_release_resource(device_t dev, device_t child, int type, int rid,
200 			   struct resource *r)
201 {
202 	/* no hotplug... */
203 	return (0);
204 }
205 
206 
207 /*
208  * Define the actual ATA device. This is a sub-bus to the ata-iobus layer
209  * to allow the higher layer bus to massage the resource allocation.
210  */
211 
212 static  int  ata_iobus_sub_probe(device_t dev);
213 
214 static device_method_t ata_iobus_sub_methods[] = {
215 	/* Device interface */
216 	DEVMETHOD(device_probe,     ata_iobus_sub_probe),
217 	DEVMETHOD(device_attach,    ata_attach),
218 	DEVMETHOD(device_detach,    ata_detach),
219 	DEVMETHOD(device_resume,    ata_resume),
220 
221 	{ 0, 0 }
222 };
223 
224 static driver_t ata_iobus_sub_driver = {
225 	"ata",
226 	ata_iobus_sub_methods,
227 	sizeof(struct ata_channel),
228 };
229 
230 DRIVER_MODULE(ata, ataiobus, ata_iobus_sub_driver, ata_devclass, 0, 0);
231 
232 static void
233 ata_iobus_locknoop(struct ata_channel *ch, int type)
234 {
235 }
236 
237 static void
238 ata_iobus_setmode(struct ata_device *atadev, int mode)
239 {
240 	atadev->mode = ATA_PIO;
241 }
242 
243 static int
244 ata_iobus_sub_probe(device_t dev)
245 {
246 	struct ata_channel *ch = device_get_softc(dev);
247 
248 	/* Only a single unit per controller thus far */
249 	ch->unit = 0;
250 	ch->flags = (ATA_USE_16BIT|ATA_NO_SLAVE);
251 	ch->locking = ata_iobus_locknoop;
252 	ch->device[MASTER].setmode = ata_iobus_setmode;
253 	ch->device[SLAVE].setmode = ata_iobus_setmode;
254 
255 	return ata_probe(dev);
256 }
257