fix to allow usb modules to compile
[linux-2.4.21-pre4.git] / arch / ppc / kernel / ocp.c
1 /*
2  * ocp.c
3  *
4  *      The is drived from pci.c
5  *
6  *      Current Maintainer
7  *      Armin Kuster akuster@pacbell.net
8  *      Jan, 2002
9  *
10  *
11  *
12  * This program is free software; you can redistribute  it and/or modify it
13  *  under  the terms of  the GNU General Public License as published by the
14  *  Free Software Foundation;  either version 2 of the  License, or (at your
15  *  option) any later version.
16  *
17  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR   IMPLIED
18  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
19  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
20  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT,  INDIRECT,
21  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
23  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
25  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  *  You should have received a copy of the  GNU General Public License along
29  *  with this program; if not, write  to the Free Software Foundation, Inc.,
30  *  675 Mass Ave, Cambridge, MA 02139, USA.
31  *
32  *      Version 1.0 (02/01/26) - A. Kuster
33  *      Initial version  -
34  *
35  *      Version 1.1 (04/19/02) - A. kuster
36  *      added the ability to give # of emacs and return count
37  *      added the addr & irq routines that were in *_enet.c
38  *      improved ocp_register
39  *      added ocp_alloc_dev
40  *      created ocp_dev_count & get_ocp_dev
41  *
42  *      Version 1.2 (4/22/02) - Armin
43  *       added UART,I2C USB & IDE reg support
44  *       vaddr is now ioremapped
45  *
46  *       Version 1.3 (04/25/02) - Armin/Todd
47  *         cleanups
48  *         bug fixes - ifndef's
49  *      Vesrion 1.4 (05/03/02) - Armin
50  *          fixed ghost reg when ocp_reg fails , Idea from David Mueller
51  *          added ocp_get_max used by ocp_dev_count
52  *          ocp_reg  and get_ocp_dev call ocp_dev_count
53  *          removed need for ifndefs
54  *          Added use of core_ocp[]
55  *      Version 1.5 - Armin
56  *      added api descriptions
57  *      name change to <class>_<action>_<obj>
58  *      added ocp_type_info for name, short desciption and size
59  *
60  *      Version 1.5 - Armin/David Gibson
61  *      added most of David ocp patch to cleanup some of the 
62  *      APIs
63  *      
64  */
65
66 #include <linux/list.h>
67 #include <linux/module.h>
68 #include <linux/config.h>
69 #include <linux/miscdevice.h>
70 #include <linux/slab.h>
71 #include <linux/types.h>
72 #include <asm/io.h>
73 #include <asm/ocp.h>
74 #include <asm/errno.h>
75
76 LIST_HEAD(ocp_list);
77
78 extern struct ocp_def core_ocp[];
79
80 struct type_info ocp_type_info[] = {
81         {"pci", "PCI"},
82         {"gpt", "General purpose timers"},
83         {"uart", "NS16550 compatable serial device"},
84         {"opb", "OBP"},
85         {"I2C", "I2C bus"},
86         {"gpio", "Gernal purpose I/O"},
87         {"emac", "10/100 BaseT ethernet"},
88         {"zmii", "ZMII bridge"},
89         {"ide", "Independent Disk"},
90         {"usb", "USB"},
91         {"sci", "Smart card"},
92         {"audio", "Sound interface"},
93         {"ssp", "Sync serail port"},
94         {"scp", "Serial Controller port"},
95         {"scc", "Serial controller"},
96         {"video", "General Video"},
97         {"dma", "Dynamic memory allocation"},
98         {"uic", "Universal Interrupt controller"},
99         {"rtc", "Real Time Clock"},
100         {"led", "Light Emitting diode"}
101
102 };
103
104 /**
105  * ocp_get_max - get max for ocp type 
106  * @type: OCP type such as PCI, GPT, UART, OPB, IIC, GPIO, EMAC, ZMII,
107  *  
108  * The routine returns max number of type that a given core supports
109  */
110 int
111 ocp_get_max(int type)
112 {
113         int max, i;
114         for (i = 0, max = 0; core_ocp[i].type != OCP_NULL_TYPE; i++) {
115                 if (type == core_ocp[i].type)
116                         max++;
117         }
118         return (max);
119 }
120
121 /**
122  * get_ocp_irq - get irq for ocp type and instance of it 
123  * @type: OCP type such as PCI, GPT, UART, OPB, IIC, GPIO, EMAC, ZMII,
124  * @dev_num: ocp device number whos paddr you want
125  *  
126  * The routine returns the valid irq, OCP_IRQ_NA or OCP_IRQ_MUL
127  * of type and instance requested 
128  */
129 int
130 ocp_get_irq(int type, int dev_num)
131 {
132         int a, i;
133         a = 0;
134
135         for (i = 0; core_ocp[i].type != OCP_NULL_TYPE; i++) {
136                 if (type == core_ocp[i].type) {
137                         if (a == dev_num)
138                                 return (core_ocp[i].irq);
139                         a++;
140
141                 }
142         }
143
144         return (-ENXIO);
145
146 }
147
148 /**
149  * ocp_get_paddr - get phyical address for ocp type and instance of it 
150  * @type: OCP type such as PCI, GPT, UART, OPB, IIC, GPIO, EMAC, ZMII
151  * @dev_num: ocp device number whos paddr you want
152  *  
153  * The routine returns paddr of type requested and it's instance or
154  * 0x0 on a failure
155  */
156 unsigned long
157 ocp_get_paddr(int type, int dev_num)
158 {
159         int a, i;
160         a = 0;
161
162         for (i = 0; core_ocp[i].type != OCP_NULL_TYPE; i++) {
163                 if (type == core_ocp[i].type) {
164                         if (a == dev_num)
165                                 return ((unsigned long) core_ocp[i].paddr);
166                         a++;
167
168                 }
169         }
170
171         return 0;
172 }
173 /**
174  * ocp_get_pm - get pm for ocp type and instance of it 
175  * @type: OCP type such as PCI, GPT, UART, OPB, IIC, GPIO, EMAC, ZMII,
176  * @dev_num: ocp device number whos power managment 
177  * value you want
178  *  
179  * The routine returns the power management value or OCP_CPM_NA,
180  * of type and instance requested 
181  */
182 int
183 ocp_get_pm(int type, int dev_num)
184 {
185         int a, i;
186         a = 0;
187
188         for (i = 0; core_ocp[i].type != OCP_NULL_TYPE; i++) {
189                 if (type == core_ocp[i].type) {
190                         if (a == dev_num)
191                                 return (core_ocp[i].cpm);
192                         a++;
193
194                 }
195         }
196
197         return OCP_CPM_NA;
198 }
199
200 /* this sets the irq,paddr and anme to the device */
201 static int
202 ocp_set_dev(struct ocp_dev *ocp)
203 {
204         int a, i;
205         a = 0;
206
207         strcpy(ocp->name, ocp_type_info[ocp->type].name);
208
209         for (i = 0; core_ocp[i].type != OCP_NULL_TYPE; i++) {
210                 if (ocp->type == core_ocp[i].type) {
211                         if (a == ocp->num) {
212                                 ocp->paddr = core_ocp[i].paddr;
213                                 ocp->irq = core_ocp[i].irq;
214                                 return 1;
215
216                         }
217                         a++;
218                 }
219         }
220
221         ocp->paddr = 0;
222         ocp->irq = OCP_IRQ_NA;
223         return 0;
224 }
225
226 /**
227  * ocp_get_numtypes - This determines how many OCP devices of a givrn
228  * type are registered
229  * @type: OCP type such as PCI, GPT, UART, OPB, IIC, GPIO, EMAC, ZMII,
230  *  
231  * The routine returns number of types are registered
232  */
233 static int
234 ocp_get_numtypes(int type)
235 {
236         int count = 0;
237         struct ocp_dev *ocp;
238         struct list_head *ocp_l;
239
240         list_for_each(ocp_l, &ocp_list) {
241                 ocp = list_entry(ocp_l, struct ocp_dev, ocp_list);
242                 if (type == ocp->type)
243                         count++;
244         }
245
246         return count;
247 }
248
249 /**
250  * ocp_alloc_dev - alloc space for ocp driver
251  * @sizeof_ocpdev: size of the device structure
252  *  
253  * The routine returns a valid 32 bit aligned
254  * points the sizeof ocp_struct and the 
255  * requested device structure
256  */
257 struct ocp_dev *
258 ocp_alloc_dev(int sizeof_ocpdev)
259 {
260         struct ocp_dev *drv;
261         int alloc_size;
262
263         /* ensure 32-byte alignment of the driver area */
264         alloc_size = sizeof (*drv) + sizeof_ocpdev + 31;
265
266         drv = (struct ocp_dev *) kmalloc(alloc_size, GFP_KERNEL);
267         if (drv == NULL) {
268                 printk(KERN_ERR
269                        "ocp_alloc_dev: Unable to allocate device memory.\n");
270                 return NULL;
271         }
272
273         memset(drv, 0, alloc_size);
274
275         if (sizeof_ocpdev)
276                 drv->ocpdev = (void *) (((long) (drv + 1) + 31) & ~31);
277
278         return (drv);
279 }
280
281 /**
282  * ocp_free_dev - frees alloc space for ocp driver
283  * @drv: the driver structure to register
284  * 
285  * Adds the driver structure to the list of registered drivers
286  * Returns the number of ocp type devices which were claimed by the driver
287  * during registration.  The driver remains registered even if the
288  * return value is zero.
289  */
290 /*
291  * ocp_free_dev - frees alloc space for ocp driver
292  * @dev: ocp device as allocated in ocp_alloc_dev
293  */
294 void
295 ocp_free_dev(void *dev)
296 {
297         kfree(dev);
298 }
299
300 /**
301  * ocp_register - register a new ocp driver
302  * @drv: the driver structure to register
303  * 
304  * Adds the driver structure to the list of registered drivers
305  * Returns the number of ocp type devices which were claimed by the driver
306  * during registration.  The driver remains registered even if the
307  * return value is zero.
308  */
309 int
310 ocp_register(struct ocp_dev *drv)
311 {
312         int max;
313
314         list_add(&drv->ocp_list, &ocp_list);
315         max = ocp_get_max(drv->type);
316         drv->num = ocp_get_numtypes(drv->type) - 1;
317
318         if (drv->num >= max) {
319                 list_del(&drv->ocp_list);
320                 return -ENXIO;
321         }
322
323         if (!ocp_set_dev(drv)) {
324                 list_del(&drv->ocp_list);
325                 return -ENXIO;
326         }
327 #ifdef CONFIG_OCP_PROC
328         ocp_proc_attach_device(drv);
329 #endif
330 #ifdef OCP_DEBUG
331         printk("Dev: %s Num:%d @ paddr:0x%x irq:%d\n", drv->name, drv->num,
332                drv->paddr, drv->irq);
333 #endif
334         return (drv->num);
335 }
336
337 /**
338  * ocp_unregister - unregister a ocp driver
339  * @drv: the driver structure to unregister
340  * 
341  * Deletes the driver structure from the list of registered OCP drivers,
342  * gives it a chance to clean up by calling its remove() function for
343  * each device it was responsible for, and marks those devices as
344  * driverless.
345  */
346
347 void
348 ocp_unregister(struct ocp_dev *drv)
349 {
350         list_del(&drv->ocp_list);
351 #ifdef CONFIG_OCP_PROC
352         ocp_proc_detach_device(drv);
353 #endif
354 }
355
356 /**
357  * ocp_get_dev - get ocp driver pointer for ocp type and instance of it 
358  * @type: OCP type such as PCI, GPT, UART, OPB, IIC, GPIO, EMAC, ZMII
359  * @dev_num: ocp device number whos paddr you want
360  *  
361  * The routine returns ocp device pointer 
362  * in list based on type and instance of that device
363  *
364  */
365 struct ocp_dev *
366 ocp_get_dev(int type, int dev_num)
367 {
368         struct ocp_dev *ocp;
369         struct list_head *ocp_l;
370         int count = 0;
371         list_for_each(ocp_l, &ocp_list) {
372                 ocp = list_entry(ocp_l, struct ocp_dev, ocp_list);
373                 if (type == ocp->type) {
374                         if (dev_num == count)
375                                 return ocp;
376                         count++;
377
378                 }
379         }
380         return NULL;
381 }
382
383 EXPORT_SYMBOL(ocp_get_irq);
384 EXPORT_SYMBOL(ocp_get_paddr);
385 EXPORT_SYMBOL(ocp_get_pm);
386 EXPORT_SYMBOL(ocp_get_max);
387 EXPORT_SYMBOL(ocp_get_dev);
388 EXPORT_SYMBOL(ocp_alloc_dev);
389 EXPORT_SYMBOL(ocp_free_dev);
390 EXPORT_SYMBOL(ocp_register);
391 EXPORT_SYMBOL(ocp_unregister);