4 * Author: Martyn Welch <martyn.welch@ge.com>
5 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
7 * Based on work by Tom Armistead and Ajit Prem
8 * Copyright 2004 Motorola Inc.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/pci.h>
23 #include <linux/poll.h>
24 #include <linux/highmem.h>
25 #include <linux/interrupt.h>
26 #include <linux/pagemap.h>
27 #include <linux/device.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/syscalls.h>
30 #include <linux/mutex.h>
31 #include <linux/spinlock.h>
32 #include <linux/slab.h>
35 #include "vme_bridge.h"
37 /* Bitmask and list of registered buses both protected by common mutex */
38 static unsigned int vme_bus_numbers
;
39 static LIST_HEAD(vme_bus_list
);
40 static DEFINE_MUTEX(vme_buses_lock
);
42 static void __exit
vme_exit(void);
43 static int __init
vme_init(void);
45 static struct vme_dev
*dev_to_vme_dev(struct device
*dev
)
47 return container_of(dev
, struct vme_dev
, dev
);
51 * Find the bridge that the resource is associated with.
53 static struct vme_bridge
*find_bridge(struct vme_resource
*resource
)
55 /* Get list to search */
56 switch (resource
->type
) {
58 return list_entry(resource
->entry
, struct vme_master_resource
,
62 return list_entry(resource
->entry
, struct vme_slave_resource
,
66 return list_entry(resource
->entry
, struct vme_dma_resource
,
70 return list_entry(resource
->entry
, struct vme_lm_resource
,
74 printk(KERN_ERR
"Unknown resource type\n");
81 * Allocate a contiguous block of memory for use by the driver. This is used to
82 * create the buffers for the slave windows.
84 void *vme_alloc_consistent(struct vme_resource
*resource
, size_t size
,
87 struct vme_bridge
*bridge
;
89 if (resource
== NULL
) {
90 printk(KERN_ERR
"No resource\n");
94 bridge
= find_bridge(resource
);
96 printk(KERN_ERR
"Can't find bridge\n");
100 if (bridge
->parent
== NULL
) {
101 printk(KERN_ERR
"Dev entry NULL for"
102 " bridge %s\n", bridge
->name
);
106 if (bridge
->alloc_consistent
== NULL
) {
107 printk(KERN_ERR
"alloc_consistent not supported by"
108 " bridge %s\n", bridge
->name
);
112 return bridge
->alloc_consistent(bridge
->parent
, size
, dma
);
114 EXPORT_SYMBOL(vme_alloc_consistent
);
117 * Free previously allocated contiguous block of memory.
119 void vme_free_consistent(struct vme_resource
*resource
, size_t size
,
120 void *vaddr
, dma_addr_t dma
)
122 struct vme_bridge
*bridge
;
124 if (resource
== NULL
) {
125 printk(KERN_ERR
"No resource\n");
129 bridge
= find_bridge(resource
);
130 if (bridge
== NULL
) {
131 printk(KERN_ERR
"Can't find bridge\n");
135 if (bridge
->parent
== NULL
) {
136 printk(KERN_ERR
"Dev entry NULL for"
137 " bridge %s\n", bridge
->name
);
141 if (bridge
->free_consistent
== NULL
) {
142 printk(KERN_ERR
"free_consistent not supported by"
143 " bridge %s\n", bridge
->name
);
147 bridge
->free_consistent(bridge
->parent
, size
, vaddr
, dma
);
149 EXPORT_SYMBOL(vme_free_consistent
);
151 size_t vme_get_size(struct vme_resource
*resource
)
154 unsigned long long base
, size
;
156 u32 aspace
, cycle
, dwidth
;
158 switch (resource
->type
) {
160 retval
= vme_master_get(resource
, &enabled
, &base
, &size
,
161 &aspace
, &cycle
, &dwidth
);
166 retval
= vme_slave_get(resource
, &enabled
, &base
, &size
,
167 &buf_base
, &aspace
, &cycle
);
175 printk(KERN_ERR
"Unknown resource type\n");
180 EXPORT_SYMBOL(vme_get_size
);
182 static int vme_check_window(u32 aspace
, unsigned long long vme_base
,
183 unsigned long long size
)
189 if (((vme_base
+ size
) > VME_A16_MAX
) ||
190 (vme_base
> VME_A16_MAX
))
194 if (((vme_base
+ size
) > VME_A24_MAX
) ||
195 (vme_base
> VME_A24_MAX
))
199 if (((vme_base
+ size
) > VME_A32_MAX
) ||
200 (vme_base
> VME_A32_MAX
))
205 * Any value held in an unsigned long long can be used as the
210 if (((vme_base
+ size
) > VME_CRCSR_MAX
) ||
211 (vme_base
> VME_CRCSR_MAX
))
221 printk(KERN_ERR
"Invalid address space\n");
230 * Request a slave image with specific attributes, return some unique
233 struct vme_resource
*vme_slave_request(struct vme_dev
*vdev
, u32 address
,
236 struct vme_bridge
*bridge
;
237 struct list_head
*slave_pos
= NULL
;
238 struct vme_slave_resource
*allocated_image
= NULL
;
239 struct vme_slave_resource
*slave_image
= NULL
;
240 struct vme_resource
*resource
= NULL
;
242 bridge
= vdev
->bridge
;
243 if (bridge
== NULL
) {
244 printk(KERN_ERR
"Can't find VME bus\n");
248 /* Loop through slave resources */
249 list_for_each(slave_pos
, &bridge
->slave_resources
) {
250 slave_image
= list_entry(slave_pos
,
251 struct vme_slave_resource
, list
);
253 if (slave_image
== NULL
) {
254 printk(KERN_ERR
"Registered NULL Slave resource\n");
258 /* Find an unlocked and compatible image */
259 mutex_lock(&slave_image
->mtx
);
260 if (((slave_image
->address_attr
& address
) == address
) &&
261 ((slave_image
->cycle_attr
& cycle
) == cycle
) &&
262 (slave_image
->locked
== 0)) {
264 slave_image
->locked
= 1;
265 mutex_unlock(&slave_image
->mtx
);
266 allocated_image
= slave_image
;
269 mutex_unlock(&slave_image
->mtx
);
273 if (allocated_image
== NULL
)
276 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
277 if (resource
== NULL
) {
278 printk(KERN_WARNING
"Unable to allocate resource structure\n");
281 resource
->type
= VME_SLAVE
;
282 resource
->entry
= &allocated_image
->list
;
288 mutex_lock(&slave_image
->mtx
);
289 slave_image
->locked
= 0;
290 mutex_unlock(&slave_image
->mtx
);
295 EXPORT_SYMBOL(vme_slave_request
);
297 int vme_slave_set(struct vme_resource
*resource
, int enabled
,
298 unsigned long long vme_base
, unsigned long long size
,
299 dma_addr_t buf_base
, u32 aspace
, u32 cycle
)
301 struct vme_bridge
*bridge
= find_bridge(resource
);
302 struct vme_slave_resource
*image
;
305 if (resource
->type
!= VME_SLAVE
) {
306 printk(KERN_ERR
"Not a slave resource\n");
310 image
= list_entry(resource
->entry
, struct vme_slave_resource
, list
);
312 if (bridge
->slave_set
== NULL
) {
313 printk(KERN_ERR
"Function not supported\n");
317 if (!(((image
->address_attr
& aspace
) == aspace
) &&
318 ((image
->cycle_attr
& cycle
) == cycle
))) {
319 printk(KERN_ERR
"Invalid attributes\n");
323 retval
= vme_check_window(aspace
, vme_base
, size
);
327 return bridge
->slave_set(image
, enabled
, vme_base
, size
, buf_base
,
330 EXPORT_SYMBOL(vme_slave_set
);
332 int vme_slave_get(struct vme_resource
*resource
, int *enabled
,
333 unsigned long long *vme_base
, unsigned long long *size
,
334 dma_addr_t
*buf_base
, u32
*aspace
, u32
*cycle
)
336 struct vme_bridge
*bridge
= find_bridge(resource
);
337 struct vme_slave_resource
*image
;
339 if (resource
->type
!= VME_SLAVE
) {
340 printk(KERN_ERR
"Not a slave resource\n");
344 image
= list_entry(resource
->entry
, struct vme_slave_resource
, list
);
346 if (bridge
->slave_get
== NULL
) {
347 printk(KERN_ERR
"vme_slave_get not supported\n");
351 return bridge
->slave_get(image
, enabled
, vme_base
, size
, buf_base
,
354 EXPORT_SYMBOL(vme_slave_get
);
356 void vme_slave_free(struct vme_resource
*resource
)
358 struct vme_slave_resource
*slave_image
;
360 if (resource
->type
!= VME_SLAVE
) {
361 printk(KERN_ERR
"Not a slave resource\n");
365 slave_image
= list_entry(resource
->entry
, struct vme_slave_resource
,
367 if (slave_image
== NULL
) {
368 printk(KERN_ERR
"Can't find slave resource\n");
373 mutex_lock(&slave_image
->mtx
);
374 if (slave_image
->locked
== 0)
375 printk(KERN_ERR
"Image is already free\n");
377 slave_image
->locked
= 0;
378 mutex_unlock(&slave_image
->mtx
);
380 /* Free up resource memory */
383 EXPORT_SYMBOL(vme_slave_free
);
386 * Request a master image with specific attributes, return some unique
389 struct vme_resource
*vme_master_request(struct vme_dev
*vdev
, u32 address
,
390 u32 cycle
, u32 dwidth
)
392 struct vme_bridge
*bridge
;
393 struct list_head
*master_pos
= NULL
;
394 struct vme_master_resource
*allocated_image
= NULL
;
395 struct vme_master_resource
*master_image
= NULL
;
396 struct vme_resource
*resource
= NULL
;
398 bridge
= vdev
->bridge
;
399 if (bridge
== NULL
) {
400 printk(KERN_ERR
"Can't find VME bus\n");
404 /* Loop through master resources */
405 list_for_each(master_pos
, &bridge
->master_resources
) {
406 master_image
= list_entry(master_pos
,
407 struct vme_master_resource
, list
);
409 if (master_image
== NULL
) {
410 printk(KERN_WARNING
"Registered NULL master resource\n");
414 /* Find an unlocked and compatible image */
415 spin_lock(&master_image
->lock
);
416 if (((master_image
->address_attr
& address
) == address
) &&
417 ((master_image
->cycle_attr
& cycle
) == cycle
) &&
418 ((master_image
->width_attr
& dwidth
) == dwidth
) &&
419 (master_image
->locked
== 0)) {
421 master_image
->locked
= 1;
422 spin_unlock(&master_image
->lock
);
423 allocated_image
= master_image
;
426 spin_unlock(&master_image
->lock
);
429 /* Check to see if we found a resource */
430 if (allocated_image
== NULL
) {
431 printk(KERN_ERR
"Can't find a suitable resource\n");
435 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
436 if (resource
== NULL
) {
437 printk(KERN_ERR
"Unable to allocate resource structure\n");
440 resource
->type
= VME_MASTER
;
441 resource
->entry
= &allocated_image
->list
;
447 spin_lock(&master_image
->lock
);
448 master_image
->locked
= 0;
449 spin_unlock(&master_image
->lock
);
454 EXPORT_SYMBOL(vme_master_request
);
456 int vme_master_set(struct vme_resource
*resource
, int enabled
,
457 unsigned long long vme_base
, unsigned long long size
, u32 aspace
,
458 u32 cycle
, u32 dwidth
)
460 struct vme_bridge
*bridge
= find_bridge(resource
);
461 struct vme_master_resource
*image
;
464 if (resource
->type
!= VME_MASTER
) {
465 printk(KERN_ERR
"Not a master resource\n");
469 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
471 if (bridge
->master_set
== NULL
) {
472 printk(KERN_WARNING
"vme_master_set not supported\n");
476 if (!(((image
->address_attr
& aspace
) == aspace
) &&
477 ((image
->cycle_attr
& cycle
) == cycle
) &&
478 ((image
->width_attr
& dwidth
) == dwidth
))) {
479 printk(KERN_WARNING
"Invalid attributes\n");
483 retval
= vme_check_window(aspace
, vme_base
, size
);
487 return bridge
->master_set(image
, enabled
, vme_base
, size
, aspace
,
490 EXPORT_SYMBOL(vme_master_set
);
492 int vme_master_get(struct vme_resource
*resource
, int *enabled
,
493 unsigned long long *vme_base
, unsigned long long *size
, u32
*aspace
,
494 u32
*cycle
, u32
*dwidth
)
496 struct vme_bridge
*bridge
= find_bridge(resource
);
497 struct vme_master_resource
*image
;
499 if (resource
->type
!= VME_MASTER
) {
500 printk(KERN_ERR
"Not a master resource\n");
504 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
506 if (bridge
->master_get
== NULL
) {
507 printk(KERN_WARNING
"vme_master_set not supported\n");
511 return bridge
->master_get(image
, enabled
, vme_base
, size
, aspace
,
514 EXPORT_SYMBOL(vme_master_get
);
517 * Read data out of VME space into a buffer.
519 ssize_t
vme_master_read(struct vme_resource
*resource
, void *buf
, size_t count
,
522 struct vme_bridge
*bridge
= find_bridge(resource
);
523 struct vme_master_resource
*image
;
526 if (bridge
->master_read
== NULL
) {
527 printk(KERN_WARNING
"Reading from resource not supported\n");
531 if (resource
->type
!= VME_MASTER
) {
532 printk(KERN_ERR
"Not a master resource\n");
536 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
538 length
= vme_get_size(resource
);
540 if (offset
> length
) {
541 printk(KERN_WARNING
"Invalid Offset\n");
545 if ((offset
+ count
) > length
)
546 count
= length
- offset
;
548 return bridge
->master_read(image
, buf
, count
, offset
);
551 EXPORT_SYMBOL(vme_master_read
);
554 * Write data out to VME space from a buffer.
556 ssize_t
vme_master_write(struct vme_resource
*resource
, void *buf
,
557 size_t count
, loff_t offset
)
559 struct vme_bridge
*bridge
= find_bridge(resource
);
560 struct vme_master_resource
*image
;
563 if (bridge
->master_write
== NULL
) {
564 printk(KERN_WARNING
"Writing to resource not supported\n");
568 if (resource
->type
!= VME_MASTER
) {
569 printk(KERN_ERR
"Not a master resource\n");
573 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
575 length
= vme_get_size(resource
);
577 if (offset
> length
) {
578 printk(KERN_WARNING
"Invalid Offset\n");
582 if ((offset
+ count
) > length
)
583 count
= length
- offset
;
585 return bridge
->master_write(image
, buf
, count
, offset
);
587 EXPORT_SYMBOL(vme_master_write
);
590 * Perform RMW cycle to provided location.
592 unsigned int vme_master_rmw(struct vme_resource
*resource
, unsigned int mask
,
593 unsigned int compare
, unsigned int swap
, loff_t offset
)
595 struct vme_bridge
*bridge
= find_bridge(resource
);
596 struct vme_master_resource
*image
;
598 if (bridge
->master_rmw
== NULL
) {
599 printk(KERN_WARNING
"Writing to resource not supported\n");
603 if (resource
->type
!= VME_MASTER
) {
604 printk(KERN_ERR
"Not a master resource\n");
608 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
610 return bridge
->master_rmw(image
, mask
, compare
, swap
, offset
);
612 EXPORT_SYMBOL(vme_master_rmw
);
614 void vme_master_free(struct vme_resource
*resource
)
616 struct vme_master_resource
*master_image
;
618 if (resource
->type
!= VME_MASTER
) {
619 printk(KERN_ERR
"Not a master resource\n");
623 master_image
= list_entry(resource
->entry
, struct vme_master_resource
,
625 if (master_image
== NULL
) {
626 printk(KERN_ERR
"Can't find master resource\n");
631 spin_lock(&master_image
->lock
);
632 if (master_image
->locked
== 0)
633 printk(KERN_ERR
"Image is already free\n");
635 master_image
->locked
= 0;
636 spin_unlock(&master_image
->lock
);
638 /* Free up resource memory */
641 EXPORT_SYMBOL(vme_master_free
);
644 * Request a DMA controller with specific attributes, return some unique
647 struct vme_resource
*vme_dma_request(struct vme_dev
*vdev
, u32 route
)
649 struct vme_bridge
*bridge
;
650 struct list_head
*dma_pos
= NULL
;
651 struct vme_dma_resource
*allocated_ctrlr
= NULL
;
652 struct vme_dma_resource
*dma_ctrlr
= NULL
;
653 struct vme_resource
*resource
= NULL
;
655 /* XXX Not checking resource attributes */
656 printk(KERN_ERR
"No VME resource Attribute tests done\n");
658 bridge
= vdev
->bridge
;
659 if (bridge
== NULL
) {
660 printk(KERN_ERR
"Can't find VME bus\n");
664 /* Loop through DMA resources */
665 list_for_each(dma_pos
, &bridge
->dma_resources
) {
666 dma_ctrlr
= list_entry(dma_pos
,
667 struct vme_dma_resource
, list
);
669 if (dma_ctrlr
== NULL
) {
670 printk(KERN_ERR
"Registered NULL DMA resource\n");
674 /* Find an unlocked and compatible controller */
675 mutex_lock(&dma_ctrlr
->mtx
);
676 if (((dma_ctrlr
->route_attr
& route
) == route
) &&
677 (dma_ctrlr
->locked
== 0)) {
679 dma_ctrlr
->locked
= 1;
680 mutex_unlock(&dma_ctrlr
->mtx
);
681 allocated_ctrlr
= dma_ctrlr
;
684 mutex_unlock(&dma_ctrlr
->mtx
);
687 /* Check to see if we found a resource */
688 if (allocated_ctrlr
== NULL
)
691 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
692 if (resource
== NULL
) {
693 printk(KERN_WARNING
"Unable to allocate resource structure\n");
696 resource
->type
= VME_DMA
;
697 resource
->entry
= &allocated_ctrlr
->list
;
703 mutex_lock(&dma_ctrlr
->mtx
);
704 dma_ctrlr
->locked
= 0;
705 mutex_unlock(&dma_ctrlr
->mtx
);
710 EXPORT_SYMBOL(vme_dma_request
);
715 struct vme_dma_list
*vme_new_dma_list(struct vme_resource
*resource
)
717 struct vme_dma_resource
*ctrlr
;
718 struct vme_dma_list
*dma_list
;
720 if (resource
->type
!= VME_DMA
) {
721 printk(KERN_ERR
"Not a DMA resource\n");
725 ctrlr
= list_entry(resource
->entry
, struct vme_dma_resource
, list
);
727 dma_list
= kmalloc(sizeof(struct vme_dma_list
), GFP_KERNEL
);
728 if (dma_list
== NULL
) {
729 printk(KERN_ERR
"Unable to allocate memory for new dma list\n");
732 INIT_LIST_HEAD(&dma_list
->entries
);
733 dma_list
->parent
= ctrlr
;
734 mutex_init(&dma_list
->mtx
);
738 EXPORT_SYMBOL(vme_new_dma_list
);
741 * Create "Pattern" type attributes
743 struct vme_dma_attr
*vme_dma_pattern_attribute(u32 pattern
, u32 type
)
745 struct vme_dma_attr
*attributes
;
746 struct vme_dma_pattern
*pattern_attr
;
748 attributes
= kmalloc(sizeof(struct vme_dma_attr
), GFP_KERNEL
);
749 if (attributes
== NULL
) {
750 printk(KERN_ERR
"Unable to allocate memory for attributes "
755 pattern_attr
= kmalloc(sizeof(struct vme_dma_pattern
), GFP_KERNEL
);
756 if (pattern_attr
== NULL
) {
757 printk(KERN_ERR
"Unable to allocate memory for pattern "
762 attributes
->type
= VME_DMA_PATTERN
;
763 attributes
->private = (void *)pattern_attr
;
765 pattern_attr
->pattern
= pattern
;
766 pattern_attr
->type
= type
;
775 EXPORT_SYMBOL(vme_dma_pattern_attribute
);
778 * Create "PCI" type attributes
780 struct vme_dma_attr
*vme_dma_pci_attribute(dma_addr_t address
)
782 struct vme_dma_attr
*attributes
;
783 struct vme_dma_pci
*pci_attr
;
785 /* XXX Run some sanity checks here */
787 attributes
= kmalloc(sizeof(struct vme_dma_attr
), GFP_KERNEL
);
788 if (attributes
== NULL
) {
789 printk(KERN_ERR
"Unable to allocate memory for attributes "
794 pci_attr
= kmalloc(sizeof(struct vme_dma_pci
), GFP_KERNEL
);
795 if (pci_attr
== NULL
) {
796 printk(KERN_ERR
"Unable to allocate memory for pci "
803 attributes
->type
= VME_DMA_PCI
;
804 attributes
->private = (void *)pci_attr
;
806 pci_attr
->address
= address
;
815 EXPORT_SYMBOL(vme_dma_pci_attribute
);
818 * Create "VME" type attributes
820 struct vme_dma_attr
*vme_dma_vme_attribute(unsigned long long address
,
821 u32 aspace
, u32 cycle
, u32 dwidth
)
823 struct vme_dma_attr
*attributes
;
824 struct vme_dma_vme
*vme_attr
;
826 attributes
= kmalloc(
827 sizeof(struct vme_dma_attr
), GFP_KERNEL
);
828 if (attributes
== NULL
) {
829 printk(KERN_ERR
"Unable to allocate memory for attributes "
834 vme_attr
= kmalloc(sizeof(struct vme_dma_vme
), GFP_KERNEL
);
835 if (vme_attr
== NULL
) {
836 printk(KERN_ERR
"Unable to allocate memory for vme "
841 attributes
->type
= VME_DMA_VME
;
842 attributes
->private = (void *)vme_attr
;
844 vme_attr
->address
= address
;
845 vme_attr
->aspace
= aspace
;
846 vme_attr
->cycle
= cycle
;
847 vme_attr
->dwidth
= dwidth
;
856 EXPORT_SYMBOL(vme_dma_vme_attribute
);
861 void vme_dma_free_attribute(struct vme_dma_attr
*attributes
)
863 kfree(attributes
->private);
866 EXPORT_SYMBOL(vme_dma_free_attribute
);
868 int vme_dma_list_add(struct vme_dma_list
*list
, struct vme_dma_attr
*src
,
869 struct vme_dma_attr
*dest
, size_t count
)
871 struct vme_bridge
*bridge
= list
->parent
->parent
;
874 if (bridge
->dma_list_add
== NULL
) {
875 printk(KERN_WARNING
"Link List DMA generation not supported\n");
879 if (!mutex_trylock(&list
->mtx
)) {
880 printk(KERN_ERR
"Link List already submitted\n");
884 retval
= bridge
->dma_list_add(list
, src
, dest
, count
);
886 mutex_unlock(&list
->mtx
);
890 EXPORT_SYMBOL(vme_dma_list_add
);
892 int vme_dma_list_exec(struct vme_dma_list
*list
)
894 struct vme_bridge
*bridge
= list
->parent
->parent
;
897 if (bridge
->dma_list_exec
== NULL
) {
898 printk(KERN_ERR
"Link List DMA execution not supported\n");
902 mutex_lock(&list
->mtx
);
904 retval
= bridge
->dma_list_exec(list
);
906 mutex_unlock(&list
->mtx
);
910 EXPORT_SYMBOL(vme_dma_list_exec
);
912 int vme_dma_list_free(struct vme_dma_list
*list
)
914 struct vme_bridge
*bridge
= list
->parent
->parent
;
917 if (bridge
->dma_list_empty
== NULL
) {
918 printk(KERN_WARNING
"Emptying of Link Lists not supported\n");
922 if (!mutex_trylock(&list
->mtx
)) {
923 printk(KERN_ERR
"Link List in use\n");
928 * Empty out all of the entries from the dma list. We need to go to the
929 * low level driver as dma entries are driver specific.
931 retval
= bridge
->dma_list_empty(list
);
933 printk(KERN_ERR
"Unable to empty link-list entries\n");
934 mutex_unlock(&list
->mtx
);
937 mutex_unlock(&list
->mtx
);
942 EXPORT_SYMBOL(vme_dma_list_free
);
944 int vme_dma_free(struct vme_resource
*resource
)
946 struct vme_dma_resource
*ctrlr
;
948 if (resource
->type
!= VME_DMA
) {
949 printk(KERN_ERR
"Not a DMA resource\n");
953 ctrlr
= list_entry(resource
->entry
, struct vme_dma_resource
, list
);
955 if (!mutex_trylock(&ctrlr
->mtx
)) {
956 printk(KERN_ERR
"Resource busy, can't free\n");
960 if (!(list_empty(&ctrlr
->pending
) && list_empty(&ctrlr
->running
))) {
961 printk(KERN_WARNING
"Resource still processing transfers\n");
962 mutex_unlock(&ctrlr
->mtx
);
968 mutex_unlock(&ctrlr
->mtx
);
972 EXPORT_SYMBOL(vme_dma_free
);
974 void vme_irq_handler(struct vme_bridge
*bridge
, int level
, int statid
)
976 void (*call
)(int, int, void *);
979 call
= bridge
->irq
[level
- 1].callback
[statid
].func
;
980 priv_data
= bridge
->irq
[level
- 1].callback
[statid
].priv_data
;
983 call(level
, statid
, priv_data
);
985 printk(KERN_WARNING
"Spurilous VME interrupt, level:%x, "
986 "vector:%x\n", level
, statid
);
988 EXPORT_SYMBOL(vme_irq_handler
);
990 int vme_irq_request(struct vme_dev
*vdev
, int level
, int statid
,
991 void (*callback
)(int, int, void *),
994 struct vme_bridge
*bridge
;
996 bridge
= vdev
->bridge
;
997 if (bridge
== NULL
) {
998 printk(KERN_ERR
"Can't find VME bus\n");
1002 if ((level
< 1) || (level
> 7)) {
1003 printk(KERN_ERR
"Invalid interrupt level\n");
1007 if (bridge
->irq_set
== NULL
) {
1008 printk(KERN_ERR
"Configuring interrupts not supported\n");
1012 mutex_lock(&bridge
->irq_mtx
);
1014 if (bridge
->irq
[level
- 1].callback
[statid
].func
) {
1015 mutex_unlock(&bridge
->irq_mtx
);
1016 printk(KERN_WARNING
"VME Interrupt already taken\n");
1020 bridge
->irq
[level
- 1].count
++;
1021 bridge
->irq
[level
- 1].callback
[statid
].priv_data
= priv_data
;
1022 bridge
->irq
[level
- 1].callback
[statid
].func
= callback
;
1024 /* Enable IRQ level */
1025 bridge
->irq_set(bridge
, level
, 1, 1);
1027 mutex_unlock(&bridge
->irq_mtx
);
1031 EXPORT_SYMBOL(vme_irq_request
);
1033 void vme_irq_free(struct vme_dev
*vdev
, int level
, int statid
)
1035 struct vme_bridge
*bridge
;
1037 bridge
= vdev
->bridge
;
1038 if (bridge
== NULL
) {
1039 printk(KERN_ERR
"Can't find VME bus\n");
1043 if ((level
< 1) || (level
> 7)) {
1044 printk(KERN_ERR
"Invalid interrupt level\n");
1048 if (bridge
->irq_set
== NULL
) {
1049 printk(KERN_ERR
"Configuring interrupts not supported\n");
1053 mutex_lock(&bridge
->irq_mtx
);
1055 bridge
->irq
[level
- 1].count
--;
1057 /* Disable IRQ level if no more interrupts attached at this level*/
1058 if (bridge
->irq
[level
- 1].count
== 0)
1059 bridge
->irq_set(bridge
, level
, 0, 1);
1061 bridge
->irq
[level
- 1].callback
[statid
].func
= NULL
;
1062 bridge
->irq
[level
- 1].callback
[statid
].priv_data
= NULL
;
1064 mutex_unlock(&bridge
->irq_mtx
);
1066 EXPORT_SYMBOL(vme_irq_free
);
1068 int vme_irq_generate(struct vme_dev
*vdev
, int level
, int statid
)
1070 struct vme_bridge
*bridge
;
1072 bridge
= vdev
->bridge
;
1073 if (bridge
== NULL
) {
1074 printk(KERN_ERR
"Can't find VME bus\n");
1078 if ((level
< 1) || (level
> 7)) {
1079 printk(KERN_WARNING
"Invalid interrupt level\n");
1083 if (bridge
->irq_generate
== NULL
) {
1084 printk(KERN_WARNING
"Interrupt generation not supported\n");
1088 return bridge
->irq_generate(bridge
, level
, statid
);
1090 EXPORT_SYMBOL(vme_irq_generate
);
1093 * Request the location monitor, return resource or NULL
1095 struct vme_resource
*vme_lm_request(struct vme_dev
*vdev
)
1097 struct vme_bridge
*bridge
;
1098 struct list_head
*lm_pos
= NULL
;
1099 struct vme_lm_resource
*allocated_lm
= NULL
;
1100 struct vme_lm_resource
*lm
= NULL
;
1101 struct vme_resource
*resource
= NULL
;
1103 bridge
= vdev
->bridge
;
1104 if (bridge
== NULL
) {
1105 printk(KERN_ERR
"Can't find VME bus\n");
1109 /* Loop through DMA resources */
1110 list_for_each(lm_pos
, &bridge
->lm_resources
) {
1111 lm
= list_entry(lm_pos
,
1112 struct vme_lm_resource
, list
);
1115 printk(KERN_ERR
"Registered NULL Location Monitor "
1120 /* Find an unlocked controller */
1121 mutex_lock(&lm
->mtx
);
1122 if (lm
->locked
== 0) {
1124 mutex_unlock(&lm
->mtx
);
1128 mutex_unlock(&lm
->mtx
);
1131 /* Check to see if we found a resource */
1132 if (allocated_lm
== NULL
)
1135 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
1136 if (resource
== NULL
) {
1137 printk(KERN_ERR
"Unable to allocate resource structure\n");
1140 resource
->type
= VME_LM
;
1141 resource
->entry
= &allocated_lm
->list
;
1147 mutex_lock(&lm
->mtx
);
1149 mutex_unlock(&lm
->mtx
);
1154 EXPORT_SYMBOL(vme_lm_request
);
1156 int vme_lm_count(struct vme_resource
*resource
)
1158 struct vme_lm_resource
*lm
;
1160 if (resource
->type
!= VME_LM
) {
1161 printk(KERN_ERR
"Not a Location Monitor resource\n");
1165 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1167 return lm
->monitors
;
1169 EXPORT_SYMBOL(vme_lm_count
);
1171 int vme_lm_set(struct vme_resource
*resource
, unsigned long long lm_base
,
1172 u32 aspace
, u32 cycle
)
1174 struct vme_bridge
*bridge
= find_bridge(resource
);
1175 struct vme_lm_resource
*lm
;
1177 if (resource
->type
!= VME_LM
) {
1178 printk(KERN_ERR
"Not a Location Monitor resource\n");
1182 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1184 if (bridge
->lm_set
== NULL
) {
1185 printk(KERN_ERR
"vme_lm_set not supported\n");
1189 return bridge
->lm_set(lm
, lm_base
, aspace
, cycle
);
1191 EXPORT_SYMBOL(vme_lm_set
);
1193 int vme_lm_get(struct vme_resource
*resource
, unsigned long long *lm_base
,
1194 u32
*aspace
, u32
*cycle
)
1196 struct vme_bridge
*bridge
= find_bridge(resource
);
1197 struct vme_lm_resource
*lm
;
1199 if (resource
->type
!= VME_LM
) {
1200 printk(KERN_ERR
"Not a Location Monitor resource\n");
1204 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1206 if (bridge
->lm_get
== NULL
) {
1207 printk(KERN_ERR
"vme_lm_get not supported\n");
1211 return bridge
->lm_get(lm
, lm_base
, aspace
, cycle
);
1213 EXPORT_SYMBOL(vme_lm_get
);
1215 int vme_lm_attach(struct vme_resource
*resource
, int monitor
,
1216 void (*callback
)(int))
1218 struct vme_bridge
*bridge
= find_bridge(resource
);
1219 struct vme_lm_resource
*lm
;
1221 if (resource
->type
!= VME_LM
) {
1222 printk(KERN_ERR
"Not a Location Monitor resource\n");
1226 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1228 if (bridge
->lm_attach
== NULL
) {
1229 printk(KERN_ERR
"vme_lm_attach not supported\n");
1233 return bridge
->lm_attach(lm
, monitor
, callback
);
1235 EXPORT_SYMBOL(vme_lm_attach
);
1237 int vme_lm_detach(struct vme_resource
*resource
, int monitor
)
1239 struct vme_bridge
*bridge
= find_bridge(resource
);
1240 struct vme_lm_resource
*lm
;
1242 if (resource
->type
!= VME_LM
) {
1243 printk(KERN_ERR
"Not a Location Monitor resource\n");
1247 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1249 if (bridge
->lm_detach
== NULL
) {
1250 printk(KERN_ERR
"vme_lm_detach not supported\n");
1254 return bridge
->lm_detach(lm
, monitor
);
1256 EXPORT_SYMBOL(vme_lm_detach
);
1258 void vme_lm_free(struct vme_resource
*resource
)
1260 struct vme_lm_resource
*lm
;
1262 if (resource
->type
!= VME_LM
) {
1263 printk(KERN_ERR
"Not a Location Monitor resource\n");
1267 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1269 mutex_lock(&lm
->mtx
);
1272 * Check to see that there aren't any callbacks still attached, if
1273 * there are we should probably be detaching them!
1278 mutex_unlock(&lm
->mtx
);
1282 EXPORT_SYMBOL(vme_lm_free
);
1284 int vme_slot_get(struct vme_dev
*vdev
)
1286 struct vme_bridge
*bridge
;
1288 bridge
= vdev
->bridge
;
1289 if (bridge
== NULL
) {
1290 printk(KERN_ERR
"Can't find VME bus\n");
1294 if (bridge
->slot_get
== NULL
) {
1295 printk(KERN_WARNING
"vme_slot_get not supported\n");
1299 return bridge
->slot_get(bridge
);
1301 EXPORT_SYMBOL(vme_slot_get
);
1304 /* - Bridge Registration --------------------------------------------------- */
1306 static void vme_dev_release(struct device
*dev
)
1308 kfree(dev_to_vme_dev(dev
));
1311 int vme_register_bridge(struct vme_bridge
*bridge
)
1316 mutex_lock(&vme_buses_lock
);
1317 for (i
= 0; i
< sizeof(vme_bus_numbers
) * 8; i
++) {
1318 if ((vme_bus_numbers
& (1 << i
)) == 0) {
1319 vme_bus_numbers
|= (1 << i
);
1321 INIT_LIST_HEAD(&bridge
->devices
);
1322 list_add_tail(&bridge
->bus_list
, &vme_bus_list
);
1327 mutex_unlock(&vme_buses_lock
);
1331 EXPORT_SYMBOL(vme_register_bridge
);
1333 void vme_unregister_bridge(struct vme_bridge
*bridge
)
1335 struct vme_dev
*vdev
;
1336 struct vme_dev
*tmp
;
1338 mutex_lock(&vme_buses_lock
);
1339 vme_bus_numbers
&= ~(1 << bridge
->num
);
1340 list_for_each_entry_safe(vdev
, tmp
, &bridge
->devices
, bridge_list
) {
1341 list_del(&vdev
->drv_list
);
1342 list_del(&vdev
->bridge_list
);
1343 device_unregister(&vdev
->dev
);
1345 list_del(&bridge
->bus_list
);
1346 mutex_unlock(&vme_buses_lock
);
1348 EXPORT_SYMBOL(vme_unregister_bridge
);
1350 /* - Driver Registration --------------------------------------------------- */
1352 static int __vme_register_driver_bus(struct vme_driver
*drv
,
1353 struct vme_bridge
*bridge
, unsigned int ndevs
)
1357 struct vme_dev
*vdev
;
1358 struct vme_dev
*tmp
;
1360 for (i
= 0; i
< ndevs
; i
++) {
1361 vdev
= kzalloc(sizeof(struct vme_dev
), GFP_KERNEL
);
1367 vdev
->bridge
= bridge
;
1368 vdev
->dev
.platform_data
= drv
;
1369 vdev
->dev
.release
= vme_dev_release
;
1370 vdev
->dev
.parent
= bridge
->parent
;
1371 vdev
->dev
.bus
= &vme_bus_type
;
1372 dev_set_name(&vdev
->dev
, "%s.%u-%u", drv
->name
, bridge
->num
,
1375 err
= device_register(&vdev
->dev
);
1379 if (vdev
->dev
.platform_data
) {
1380 list_add_tail(&vdev
->drv_list
, &drv
->devices
);
1381 list_add_tail(&vdev
->bridge_list
, &bridge
->devices
);
1383 device_unregister(&vdev
->dev
);
1390 list_for_each_entry_safe(vdev
, tmp
, &drv
->devices
, drv_list
) {
1391 list_del(&vdev
->drv_list
);
1392 list_del(&vdev
->bridge_list
);
1393 device_unregister(&vdev
->dev
);
1398 static int __vme_register_driver(struct vme_driver
*drv
, unsigned int ndevs
)
1400 struct vme_bridge
*bridge
;
1403 mutex_lock(&vme_buses_lock
);
1404 list_for_each_entry(bridge
, &vme_bus_list
, bus_list
) {
1406 * This cannot cause trouble as we already have vme_buses_lock
1407 * and if the bridge is removed, it will have to go through
1408 * vme_unregister_bridge() to do it (which calls remove() on
1409 * the bridge which in turn tries to acquire vme_buses_lock and
1410 * will have to wait).
1412 err
= __vme_register_driver_bus(drv
, bridge
, ndevs
);
1416 mutex_unlock(&vme_buses_lock
);
1420 int vme_register_driver(struct vme_driver
*drv
, unsigned int ndevs
)
1424 drv
->driver
.name
= drv
->name
;
1425 drv
->driver
.bus
= &vme_bus_type
;
1426 INIT_LIST_HEAD(&drv
->devices
);
1428 err
= driver_register(&drv
->driver
);
1432 err
= __vme_register_driver(drv
, ndevs
);
1434 driver_unregister(&drv
->driver
);
1438 EXPORT_SYMBOL(vme_register_driver
);
1440 void vme_unregister_driver(struct vme_driver
*drv
)
1442 struct vme_dev
*dev
, *dev_tmp
;
1444 mutex_lock(&vme_buses_lock
);
1445 list_for_each_entry_safe(dev
, dev_tmp
, &drv
->devices
, drv_list
) {
1446 list_del(&dev
->drv_list
);
1447 list_del(&dev
->bridge_list
);
1448 device_unregister(&dev
->dev
);
1450 mutex_unlock(&vme_buses_lock
);
1452 driver_unregister(&drv
->driver
);
1454 EXPORT_SYMBOL(vme_unregister_driver
);
1456 /* - Bus Registration ------------------------------------------------------ */
1458 static int vme_bus_match(struct device
*dev
, struct device_driver
*drv
)
1460 struct vme_driver
*vme_drv
;
1462 vme_drv
= container_of(drv
, struct vme_driver
, driver
);
1464 if (dev
->platform_data
== vme_drv
) {
1465 struct vme_dev
*vdev
= dev_to_vme_dev(dev
);
1467 if (vme_drv
->match
&& vme_drv
->match(vdev
))
1470 dev
->platform_data
= NULL
;
1475 static int vme_bus_probe(struct device
*dev
)
1477 int retval
= -ENODEV
;
1478 struct vme_driver
*driver
;
1479 struct vme_dev
*vdev
= dev_to_vme_dev(dev
);
1481 driver
= dev
->platform_data
;
1483 if (driver
->probe
!= NULL
)
1484 retval
= driver
->probe(vdev
);
1489 static int vme_bus_remove(struct device
*dev
)
1491 int retval
= -ENODEV
;
1492 struct vme_driver
*driver
;
1493 struct vme_dev
*vdev
= dev_to_vme_dev(dev
);
1495 driver
= dev
->platform_data
;
1497 if (driver
->remove
!= NULL
)
1498 retval
= driver
->remove(vdev
);
1503 struct bus_type vme_bus_type
= {
1505 .match
= vme_bus_match
,
1506 .probe
= vme_bus_probe
,
1507 .remove
= vme_bus_remove
,
1509 EXPORT_SYMBOL(vme_bus_type
);
1511 static int __init
vme_init(void)
1513 return bus_register(&vme_bus_type
);
1516 static void __exit
vme_exit(void)
1518 bus_unregister(&vme_bus_type
);
1521 MODULE_DESCRIPTION("VME bridge driver framework");
1522 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
1523 MODULE_LICENSE("GPL");
1525 module_init(vme_init
);
1526 module_exit(vme_exit
);