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>
33 #include <linux/vme.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 bridge %s\n", bridge
->name
);
105 if (bridge
->alloc_consistent
== NULL
) {
106 printk(KERN_ERR
"alloc_consistent not supported by bridge %s\n",
111 return bridge
->alloc_consistent(bridge
->parent
, size
, dma
);
113 EXPORT_SYMBOL(vme_alloc_consistent
);
116 * Free previously allocated contiguous block of memory.
118 void vme_free_consistent(struct vme_resource
*resource
, size_t size
,
119 void *vaddr
, dma_addr_t dma
)
121 struct vme_bridge
*bridge
;
123 if (resource
== NULL
) {
124 printk(KERN_ERR
"No resource\n");
128 bridge
= find_bridge(resource
);
129 if (bridge
== NULL
) {
130 printk(KERN_ERR
"Can't find bridge\n");
134 if (bridge
->parent
== NULL
) {
135 printk(KERN_ERR
"Dev entry NULL for bridge %s\n", bridge
->name
);
139 if (bridge
->free_consistent
== NULL
) {
140 printk(KERN_ERR
"free_consistent not supported by bridge %s\n",
145 bridge
->free_consistent(bridge
->parent
, size
, vaddr
, dma
);
147 EXPORT_SYMBOL(vme_free_consistent
);
149 size_t vme_get_size(struct vme_resource
*resource
)
152 unsigned long long base
, size
;
154 u32 aspace
, cycle
, dwidth
;
156 switch (resource
->type
) {
158 retval
= vme_master_get(resource
, &enabled
, &base
, &size
,
159 &aspace
, &cycle
, &dwidth
);
164 retval
= vme_slave_get(resource
, &enabled
, &base
, &size
,
165 &buf_base
, &aspace
, &cycle
);
173 printk(KERN_ERR
"Unknown resource type\n");
178 EXPORT_SYMBOL(vme_get_size
);
180 int vme_check_window(u32 aspace
, unsigned long long vme_base
,
181 unsigned long long size
)
187 if (((vme_base
+ size
) > VME_A16_MAX
) ||
188 (vme_base
> VME_A16_MAX
))
192 if (((vme_base
+ size
) > VME_A24_MAX
) ||
193 (vme_base
> VME_A24_MAX
))
197 if (((vme_base
+ size
) > VME_A32_MAX
) ||
198 (vme_base
> VME_A32_MAX
))
202 if ((size
!= 0) && (vme_base
> U64_MAX
+ 1 - size
))
206 if (((vme_base
+ size
) > VME_CRCSR_MAX
) ||
207 (vme_base
> VME_CRCSR_MAX
))
217 printk(KERN_ERR
"Invalid address space\n");
224 EXPORT_SYMBOL(vme_check_window
);
226 static u32
vme_get_aspace(int am
)
260 * Request a slave image with specific attributes, return some unique
263 struct vme_resource
*vme_slave_request(struct vme_dev
*vdev
, u32 address
,
266 struct vme_bridge
*bridge
;
267 struct list_head
*slave_pos
= NULL
;
268 struct vme_slave_resource
*allocated_image
= NULL
;
269 struct vme_slave_resource
*slave_image
= NULL
;
270 struct vme_resource
*resource
= NULL
;
272 bridge
= vdev
->bridge
;
273 if (bridge
== NULL
) {
274 printk(KERN_ERR
"Can't find VME bus\n");
278 /* Loop through slave resources */
279 list_for_each(slave_pos
, &bridge
->slave_resources
) {
280 slave_image
= list_entry(slave_pos
,
281 struct vme_slave_resource
, list
);
283 if (slave_image
== NULL
) {
284 printk(KERN_ERR
"Registered NULL Slave resource\n");
288 /* Find an unlocked and compatible image */
289 mutex_lock(&slave_image
->mtx
);
290 if (((slave_image
->address_attr
& address
) == address
) &&
291 ((slave_image
->cycle_attr
& cycle
) == cycle
) &&
292 (slave_image
->locked
== 0)) {
294 slave_image
->locked
= 1;
295 mutex_unlock(&slave_image
->mtx
);
296 allocated_image
= slave_image
;
299 mutex_unlock(&slave_image
->mtx
);
303 if (allocated_image
== NULL
)
306 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
307 if (resource
== NULL
) {
308 printk(KERN_WARNING
"Unable to allocate resource structure\n");
311 resource
->type
= VME_SLAVE
;
312 resource
->entry
= &allocated_image
->list
;
318 mutex_lock(&slave_image
->mtx
);
319 slave_image
->locked
= 0;
320 mutex_unlock(&slave_image
->mtx
);
325 EXPORT_SYMBOL(vme_slave_request
);
327 int vme_slave_set(struct vme_resource
*resource
, int enabled
,
328 unsigned long long vme_base
, unsigned long long size
,
329 dma_addr_t buf_base
, u32 aspace
, u32 cycle
)
331 struct vme_bridge
*bridge
= find_bridge(resource
);
332 struct vme_slave_resource
*image
;
335 if (resource
->type
!= VME_SLAVE
) {
336 printk(KERN_ERR
"Not a slave resource\n");
340 image
= list_entry(resource
->entry
, struct vme_slave_resource
, list
);
342 if (bridge
->slave_set
== NULL
) {
343 printk(KERN_ERR
"Function not supported\n");
347 if (!(((image
->address_attr
& aspace
) == aspace
) &&
348 ((image
->cycle_attr
& cycle
) == cycle
))) {
349 printk(KERN_ERR
"Invalid attributes\n");
353 retval
= vme_check_window(aspace
, vme_base
, size
);
357 return bridge
->slave_set(image
, enabled
, vme_base
, size
, buf_base
,
360 EXPORT_SYMBOL(vme_slave_set
);
362 int vme_slave_get(struct vme_resource
*resource
, int *enabled
,
363 unsigned long long *vme_base
, unsigned long long *size
,
364 dma_addr_t
*buf_base
, u32
*aspace
, u32
*cycle
)
366 struct vme_bridge
*bridge
= find_bridge(resource
);
367 struct vme_slave_resource
*image
;
369 if (resource
->type
!= VME_SLAVE
) {
370 printk(KERN_ERR
"Not a slave resource\n");
374 image
= list_entry(resource
->entry
, struct vme_slave_resource
, list
);
376 if (bridge
->slave_get
== NULL
) {
377 printk(KERN_ERR
"vme_slave_get not supported\n");
381 return bridge
->slave_get(image
, enabled
, vme_base
, size
, buf_base
,
384 EXPORT_SYMBOL(vme_slave_get
);
386 void vme_slave_free(struct vme_resource
*resource
)
388 struct vme_slave_resource
*slave_image
;
390 if (resource
->type
!= VME_SLAVE
) {
391 printk(KERN_ERR
"Not a slave resource\n");
395 slave_image
= list_entry(resource
->entry
, struct vme_slave_resource
,
397 if (slave_image
== NULL
) {
398 printk(KERN_ERR
"Can't find slave resource\n");
403 mutex_lock(&slave_image
->mtx
);
404 if (slave_image
->locked
== 0)
405 printk(KERN_ERR
"Image is already free\n");
407 slave_image
->locked
= 0;
408 mutex_unlock(&slave_image
->mtx
);
410 /* Free up resource memory */
413 EXPORT_SYMBOL(vme_slave_free
);
416 * Request a master image with specific attributes, return some unique
419 struct vme_resource
*vme_master_request(struct vme_dev
*vdev
, u32 address
,
420 u32 cycle
, u32 dwidth
)
422 struct vme_bridge
*bridge
;
423 struct list_head
*master_pos
= NULL
;
424 struct vme_master_resource
*allocated_image
= NULL
;
425 struct vme_master_resource
*master_image
= NULL
;
426 struct vme_resource
*resource
= NULL
;
428 bridge
= vdev
->bridge
;
429 if (bridge
== NULL
) {
430 printk(KERN_ERR
"Can't find VME bus\n");
434 /* Loop through master resources */
435 list_for_each(master_pos
, &bridge
->master_resources
) {
436 master_image
= list_entry(master_pos
,
437 struct vme_master_resource
, list
);
439 if (master_image
== NULL
) {
440 printk(KERN_WARNING
"Registered NULL master resource\n");
444 /* Find an unlocked and compatible image */
445 spin_lock(&master_image
->lock
);
446 if (((master_image
->address_attr
& address
) == address
) &&
447 ((master_image
->cycle_attr
& cycle
) == cycle
) &&
448 ((master_image
->width_attr
& dwidth
) == dwidth
) &&
449 (master_image
->locked
== 0)) {
451 master_image
->locked
= 1;
452 spin_unlock(&master_image
->lock
);
453 allocated_image
= master_image
;
456 spin_unlock(&master_image
->lock
);
459 /* Check to see if we found a resource */
460 if (allocated_image
== NULL
) {
461 printk(KERN_ERR
"Can't find a suitable resource\n");
465 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
466 if (resource
== NULL
) {
467 printk(KERN_ERR
"Unable to allocate resource structure\n");
470 resource
->type
= VME_MASTER
;
471 resource
->entry
= &allocated_image
->list
;
477 spin_lock(&master_image
->lock
);
478 master_image
->locked
= 0;
479 spin_unlock(&master_image
->lock
);
484 EXPORT_SYMBOL(vme_master_request
);
486 int vme_master_set(struct vme_resource
*resource
, int enabled
,
487 unsigned long long vme_base
, unsigned long long size
, u32 aspace
,
488 u32 cycle
, u32 dwidth
)
490 struct vme_bridge
*bridge
= find_bridge(resource
);
491 struct vme_master_resource
*image
;
494 if (resource
->type
!= VME_MASTER
) {
495 printk(KERN_ERR
"Not a master resource\n");
499 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
501 if (bridge
->master_set
== NULL
) {
502 printk(KERN_WARNING
"vme_master_set not supported\n");
506 if (!(((image
->address_attr
& aspace
) == aspace
) &&
507 ((image
->cycle_attr
& cycle
) == cycle
) &&
508 ((image
->width_attr
& dwidth
) == dwidth
))) {
509 printk(KERN_WARNING
"Invalid attributes\n");
513 retval
= vme_check_window(aspace
, vme_base
, size
);
517 return bridge
->master_set(image
, enabled
, vme_base
, size
, aspace
,
520 EXPORT_SYMBOL(vme_master_set
);
522 int vme_master_get(struct vme_resource
*resource
, int *enabled
,
523 unsigned long long *vme_base
, unsigned long long *size
, u32
*aspace
,
524 u32
*cycle
, u32
*dwidth
)
526 struct vme_bridge
*bridge
= find_bridge(resource
);
527 struct vme_master_resource
*image
;
529 if (resource
->type
!= VME_MASTER
) {
530 printk(KERN_ERR
"Not a master resource\n");
534 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
536 if (bridge
->master_get
== NULL
) {
537 printk(KERN_WARNING
"%s not supported\n", __func__
);
541 return bridge
->master_get(image
, enabled
, vme_base
, size
, aspace
,
544 EXPORT_SYMBOL(vme_master_get
);
547 * Read data out of VME space into a buffer.
549 ssize_t
vme_master_read(struct vme_resource
*resource
, void *buf
, size_t count
,
552 struct vme_bridge
*bridge
= find_bridge(resource
);
553 struct vme_master_resource
*image
;
556 if (bridge
->master_read
== NULL
) {
557 printk(KERN_WARNING
"Reading from resource not supported\n");
561 if (resource
->type
!= VME_MASTER
) {
562 printk(KERN_ERR
"Not a master resource\n");
566 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
568 length
= vme_get_size(resource
);
570 if (offset
> length
) {
571 printk(KERN_WARNING
"Invalid Offset\n");
575 if ((offset
+ count
) > length
)
576 count
= length
- offset
;
578 return bridge
->master_read(image
, buf
, count
, offset
);
581 EXPORT_SYMBOL(vme_master_read
);
584 * Write data out to VME space from a buffer.
586 ssize_t
vme_master_write(struct vme_resource
*resource
, void *buf
,
587 size_t count
, loff_t offset
)
589 struct vme_bridge
*bridge
= find_bridge(resource
);
590 struct vme_master_resource
*image
;
593 if (bridge
->master_write
== NULL
) {
594 printk(KERN_WARNING
"Writing to resource not supported\n");
598 if (resource
->type
!= VME_MASTER
) {
599 printk(KERN_ERR
"Not a master resource\n");
603 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
605 length
= vme_get_size(resource
);
607 if (offset
> length
) {
608 printk(KERN_WARNING
"Invalid Offset\n");
612 if ((offset
+ count
) > length
)
613 count
= length
- offset
;
615 return bridge
->master_write(image
, buf
, count
, offset
);
617 EXPORT_SYMBOL(vme_master_write
);
620 * Perform RMW cycle to provided location.
622 unsigned int vme_master_rmw(struct vme_resource
*resource
, unsigned int mask
,
623 unsigned int compare
, unsigned int swap
, loff_t offset
)
625 struct vme_bridge
*bridge
= find_bridge(resource
);
626 struct vme_master_resource
*image
;
628 if (bridge
->master_rmw
== NULL
) {
629 printk(KERN_WARNING
"Writing to resource not supported\n");
633 if (resource
->type
!= VME_MASTER
) {
634 printk(KERN_ERR
"Not a master resource\n");
638 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
640 return bridge
->master_rmw(image
, mask
, compare
, swap
, offset
);
642 EXPORT_SYMBOL(vme_master_rmw
);
644 int vme_master_mmap(struct vme_resource
*resource
, struct vm_area_struct
*vma
)
646 struct vme_master_resource
*image
;
647 phys_addr_t phys_addr
;
648 unsigned long vma_size
;
650 if (resource
->type
!= VME_MASTER
) {
651 pr_err("Not a master resource\n");
655 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
656 phys_addr
= image
->bus_resource
.start
+ (vma
->vm_pgoff
<< PAGE_SHIFT
);
657 vma_size
= vma
->vm_end
- vma
->vm_start
;
659 if (phys_addr
+ vma_size
> image
->bus_resource
.end
+ 1) {
660 pr_err("Map size cannot exceed the window size\n");
664 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
666 return vm_iomap_memory(vma
, phys_addr
, vma
->vm_end
- vma
->vm_start
);
668 EXPORT_SYMBOL(vme_master_mmap
);
670 void vme_master_free(struct vme_resource
*resource
)
672 struct vme_master_resource
*master_image
;
674 if (resource
->type
!= VME_MASTER
) {
675 printk(KERN_ERR
"Not a master resource\n");
679 master_image
= list_entry(resource
->entry
, struct vme_master_resource
,
681 if (master_image
== NULL
) {
682 printk(KERN_ERR
"Can't find master resource\n");
687 spin_lock(&master_image
->lock
);
688 if (master_image
->locked
== 0)
689 printk(KERN_ERR
"Image is already free\n");
691 master_image
->locked
= 0;
692 spin_unlock(&master_image
->lock
);
694 /* Free up resource memory */
697 EXPORT_SYMBOL(vme_master_free
);
700 * Request a DMA controller with specific attributes, return some unique
703 struct vme_resource
*vme_dma_request(struct vme_dev
*vdev
, u32 route
)
705 struct vme_bridge
*bridge
;
706 struct list_head
*dma_pos
= NULL
;
707 struct vme_dma_resource
*allocated_ctrlr
= NULL
;
708 struct vme_dma_resource
*dma_ctrlr
= NULL
;
709 struct vme_resource
*resource
= NULL
;
711 /* XXX Not checking resource attributes */
712 printk(KERN_ERR
"No VME resource Attribute tests done\n");
714 bridge
= vdev
->bridge
;
715 if (bridge
== NULL
) {
716 printk(KERN_ERR
"Can't find VME bus\n");
720 /* Loop through DMA resources */
721 list_for_each(dma_pos
, &bridge
->dma_resources
) {
722 dma_ctrlr
= list_entry(dma_pos
,
723 struct vme_dma_resource
, list
);
725 if (dma_ctrlr
== NULL
) {
726 printk(KERN_ERR
"Registered NULL DMA resource\n");
730 /* Find an unlocked and compatible controller */
731 mutex_lock(&dma_ctrlr
->mtx
);
732 if (((dma_ctrlr
->route_attr
& route
) == route
) &&
733 (dma_ctrlr
->locked
== 0)) {
735 dma_ctrlr
->locked
= 1;
736 mutex_unlock(&dma_ctrlr
->mtx
);
737 allocated_ctrlr
= dma_ctrlr
;
740 mutex_unlock(&dma_ctrlr
->mtx
);
743 /* Check to see if we found a resource */
744 if (allocated_ctrlr
== NULL
)
747 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
748 if (resource
== NULL
) {
749 printk(KERN_WARNING
"Unable to allocate resource structure\n");
752 resource
->type
= VME_DMA
;
753 resource
->entry
= &allocated_ctrlr
->list
;
759 mutex_lock(&dma_ctrlr
->mtx
);
760 dma_ctrlr
->locked
= 0;
761 mutex_unlock(&dma_ctrlr
->mtx
);
766 EXPORT_SYMBOL(vme_dma_request
);
771 struct vme_dma_list
*vme_new_dma_list(struct vme_resource
*resource
)
773 struct vme_dma_resource
*ctrlr
;
774 struct vme_dma_list
*dma_list
;
776 if (resource
->type
!= VME_DMA
) {
777 printk(KERN_ERR
"Not a DMA resource\n");
781 ctrlr
= list_entry(resource
->entry
, struct vme_dma_resource
, list
);
783 dma_list
= kmalloc(sizeof(struct vme_dma_list
), GFP_KERNEL
);
784 if (dma_list
== NULL
) {
785 printk(KERN_ERR
"Unable to allocate memory for new dma list\n");
788 INIT_LIST_HEAD(&dma_list
->entries
);
789 dma_list
->parent
= ctrlr
;
790 mutex_init(&dma_list
->mtx
);
794 EXPORT_SYMBOL(vme_new_dma_list
);
797 * Create "Pattern" type attributes
799 struct vme_dma_attr
*vme_dma_pattern_attribute(u32 pattern
, u32 type
)
801 struct vme_dma_attr
*attributes
;
802 struct vme_dma_pattern
*pattern_attr
;
804 attributes
= kmalloc(sizeof(struct vme_dma_attr
), GFP_KERNEL
);
805 if (attributes
== NULL
) {
806 printk(KERN_ERR
"Unable to allocate memory for attributes structure\n");
810 pattern_attr
= kmalloc(sizeof(struct vme_dma_pattern
), GFP_KERNEL
);
811 if (pattern_attr
== NULL
) {
812 printk(KERN_ERR
"Unable to allocate memory for pattern attributes\n");
816 attributes
->type
= VME_DMA_PATTERN
;
817 attributes
->private = (void *)pattern_attr
;
819 pattern_attr
->pattern
= pattern
;
820 pattern_attr
->type
= type
;
829 EXPORT_SYMBOL(vme_dma_pattern_attribute
);
832 * Create "PCI" type attributes
834 struct vme_dma_attr
*vme_dma_pci_attribute(dma_addr_t address
)
836 struct vme_dma_attr
*attributes
;
837 struct vme_dma_pci
*pci_attr
;
839 /* XXX Run some sanity checks here */
841 attributes
= kmalloc(sizeof(struct vme_dma_attr
), GFP_KERNEL
);
842 if (attributes
== NULL
) {
843 printk(KERN_ERR
"Unable to allocate memory for attributes structure\n");
847 pci_attr
= kmalloc(sizeof(struct vme_dma_pci
), GFP_KERNEL
);
848 if (pci_attr
== NULL
) {
849 printk(KERN_ERR
"Unable to allocate memory for pci attributes\n");
855 attributes
->type
= VME_DMA_PCI
;
856 attributes
->private = (void *)pci_attr
;
858 pci_attr
->address
= address
;
867 EXPORT_SYMBOL(vme_dma_pci_attribute
);
870 * Create "VME" type attributes
872 struct vme_dma_attr
*vme_dma_vme_attribute(unsigned long long address
,
873 u32 aspace
, u32 cycle
, u32 dwidth
)
875 struct vme_dma_attr
*attributes
;
876 struct vme_dma_vme
*vme_attr
;
878 attributes
= kmalloc(
879 sizeof(struct vme_dma_attr
), GFP_KERNEL
);
880 if (attributes
== NULL
) {
881 printk(KERN_ERR
"Unable to allocate memory for attributes structure\n");
885 vme_attr
= kmalloc(sizeof(struct vme_dma_vme
), GFP_KERNEL
);
886 if (vme_attr
== NULL
) {
887 printk(KERN_ERR
"Unable to allocate memory for vme attributes\n");
891 attributes
->type
= VME_DMA_VME
;
892 attributes
->private = (void *)vme_attr
;
894 vme_attr
->address
= address
;
895 vme_attr
->aspace
= aspace
;
896 vme_attr
->cycle
= cycle
;
897 vme_attr
->dwidth
= dwidth
;
906 EXPORT_SYMBOL(vme_dma_vme_attribute
);
911 void vme_dma_free_attribute(struct vme_dma_attr
*attributes
)
913 kfree(attributes
->private);
916 EXPORT_SYMBOL(vme_dma_free_attribute
);
918 int vme_dma_list_add(struct vme_dma_list
*list
, struct vme_dma_attr
*src
,
919 struct vme_dma_attr
*dest
, size_t count
)
921 struct vme_bridge
*bridge
= list
->parent
->parent
;
924 if (bridge
->dma_list_add
== NULL
) {
925 printk(KERN_WARNING
"Link List DMA generation not supported\n");
929 if (!mutex_trylock(&list
->mtx
)) {
930 printk(KERN_ERR
"Link List already submitted\n");
934 retval
= bridge
->dma_list_add(list
, src
, dest
, count
);
936 mutex_unlock(&list
->mtx
);
940 EXPORT_SYMBOL(vme_dma_list_add
);
942 int vme_dma_list_exec(struct vme_dma_list
*list
)
944 struct vme_bridge
*bridge
= list
->parent
->parent
;
947 if (bridge
->dma_list_exec
== NULL
) {
948 printk(KERN_ERR
"Link List DMA execution not supported\n");
952 mutex_lock(&list
->mtx
);
954 retval
= bridge
->dma_list_exec(list
);
956 mutex_unlock(&list
->mtx
);
960 EXPORT_SYMBOL(vme_dma_list_exec
);
962 int vme_dma_list_free(struct vme_dma_list
*list
)
964 struct vme_bridge
*bridge
= list
->parent
->parent
;
967 if (bridge
->dma_list_empty
== NULL
) {
968 printk(KERN_WARNING
"Emptying of Link Lists not supported\n");
972 if (!mutex_trylock(&list
->mtx
)) {
973 printk(KERN_ERR
"Link List in use\n");
978 * Empty out all of the entries from the dma list. We need to go to the
979 * low level driver as dma entries are driver specific.
981 retval
= bridge
->dma_list_empty(list
);
983 printk(KERN_ERR
"Unable to empty link-list entries\n");
984 mutex_unlock(&list
->mtx
);
987 mutex_unlock(&list
->mtx
);
992 EXPORT_SYMBOL(vme_dma_list_free
);
994 int vme_dma_free(struct vme_resource
*resource
)
996 struct vme_dma_resource
*ctrlr
;
998 if (resource
->type
!= VME_DMA
) {
999 printk(KERN_ERR
"Not a DMA resource\n");
1003 ctrlr
= list_entry(resource
->entry
, struct vme_dma_resource
, list
);
1005 if (!mutex_trylock(&ctrlr
->mtx
)) {
1006 printk(KERN_ERR
"Resource busy, can't free\n");
1010 if (!(list_empty(&ctrlr
->pending
) && list_empty(&ctrlr
->running
))) {
1011 printk(KERN_WARNING
"Resource still processing transfers\n");
1012 mutex_unlock(&ctrlr
->mtx
);
1018 mutex_unlock(&ctrlr
->mtx
);
1024 EXPORT_SYMBOL(vme_dma_free
);
1026 void vme_bus_error_handler(struct vme_bridge
*bridge
,
1027 unsigned long long address
, int am
)
1029 struct list_head
*handler_pos
= NULL
;
1030 struct vme_error_handler
*handler
;
1031 int handler_triggered
= 0;
1032 u32 aspace
= vme_get_aspace(am
);
1034 list_for_each(handler_pos
, &bridge
->vme_error_handlers
) {
1035 handler
= list_entry(handler_pos
, struct vme_error_handler
,
1037 if ((aspace
== handler
->aspace
) &&
1038 (address
>= handler
->start
) &&
1039 (address
< handler
->end
)) {
1040 if (!handler
->num_errors
)
1041 handler
->first_error
= address
;
1042 if (handler
->num_errors
!= UINT_MAX
)
1043 handler
->num_errors
++;
1044 handler_triggered
= 1;
1048 if (!handler_triggered
)
1049 dev_err(bridge
->parent
,
1050 "Unhandled VME access error at address 0x%llx\n",
1053 EXPORT_SYMBOL(vme_bus_error_handler
);
1055 struct vme_error_handler
*vme_register_error_handler(
1056 struct vme_bridge
*bridge
, u32 aspace
,
1057 unsigned long long address
, size_t len
)
1059 struct vme_error_handler
*handler
;
1061 handler
= kmalloc(sizeof(*handler
), GFP_KERNEL
);
1065 handler
->aspace
= aspace
;
1066 handler
->start
= address
;
1067 handler
->end
= address
+ len
;
1068 handler
->num_errors
= 0;
1069 handler
->first_error
= 0;
1070 list_add_tail(&handler
->list
, &bridge
->vme_error_handlers
);
1074 EXPORT_SYMBOL(vme_register_error_handler
);
1076 void vme_unregister_error_handler(struct vme_error_handler
*handler
)
1078 list_del(&handler
->list
);
1081 EXPORT_SYMBOL(vme_unregister_error_handler
);
1083 void vme_irq_handler(struct vme_bridge
*bridge
, int level
, int statid
)
1085 void (*call
)(int, int, void *);
1088 call
= bridge
->irq
[level
- 1].callback
[statid
].func
;
1089 priv_data
= bridge
->irq
[level
- 1].callback
[statid
].priv_data
;
1092 call(level
, statid
, priv_data
);
1094 printk(KERN_WARNING
"Spurilous VME interrupt, level:%x, vector:%x\n",
1097 EXPORT_SYMBOL(vme_irq_handler
);
1099 int vme_irq_request(struct vme_dev
*vdev
, int level
, int statid
,
1100 void (*callback
)(int, int, void *),
1103 struct vme_bridge
*bridge
;
1105 bridge
= vdev
->bridge
;
1106 if (bridge
== NULL
) {
1107 printk(KERN_ERR
"Can't find VME bus\n");
1111 if ((level
< 1) || (level
> 7)) {
1112 printk(KERN_ERR
"Invalid interrupt level\n");
1116 if (bridge
->irq_set
== NULL
) {
1117 printk(KERN_ERR
"Configuring interrupts not supported\n");
1121 mutex_lock(&bridge
->irq_mtx
);
1123 if (bridge
->irq
[level
- 1].callback
[statid
].func
) {
1124 mutex_unlock(&bridge
->irq_mtx
);
1125 printk(KERN_WARNING
"VME Interrupt already taken\n");
1129 bridge
->irq
[level
- 1].count
++;
1130 bridge
->irq
[level
- 1].callback
[statid
].priv_data
= priv_data
;
1131 bridge
->irq
[level
- 1].callback
[statid
].func
= callback
;
1133 /* Enable IRQ level */
1134 bridge
->irq_set(bridge
, level
, 1, 1);
1136 mutex_unlock(&bridge
->irq_mtx
);
1140 EXPORT_SYMBOL(vme_irq_request
);
1142 void vme_irq_free(struct vme_dev
*vdev
, int level
, int statid
)
1144 struct vme_bridge
*bridge
;
1146 bridge
= vdev
->bridge
;
1147 if (bridge
== NULL
) {
1148 printk(KERN_ERR
"Can't find VME bus\n");
1152 if ((level
< 1) || (level
> 7)) {
1153 printk(KERN_ERR
"Invalid interrupt level\n");
1157 if (bridge
->irq_set
== NULL
) {
1158 printk(KERN_ERR
"Configuring interrupts not supported\n");
1162 mutex_lock(&bridge
->irq_mtx
);
1164 bridge
->irq
[level
- 1].count
--;
1166 /* Disable IRQ level if no more interrupts attached at this level*/
1167 if (bridge
->irq
[level
- 1].count
== 0)
1168 bridge
->irq_set(bridge
, level
, 0, 1);
1170 bridge
->irq
[level
- 1].callback
[statid
].func
= NULL
;
1171 bridge
->irq
[level
- 1].callback
[statid
].priv_data
= NULL
;
1173 mutex_unlock(&bridge
->irq_mtx
);
1175 EXPORT_SYMBOL(vme_irq_free
);
1177 int vme_irq_generate(struct vme_dev
*vdev
, int level
, int statid
)
1179 struct vme_bridge
*bridge
;
1181 bridge
= vdev
->bridge
;
1182 if (bridge
== NULL
) {
1183 printk(KERN_ERR
"Can't find VME bus\n");
1187 if ((level
< 1) || (level
> 7)) {
1188 printk(KERN_WARNING
"Invalid interrupt level\n");
1192 if (bridge
->irq_generate
== NULL
) {
1193 printk(KERN_WARNING
"Interrupt generation not supported\n");
1197 return bridge
->irq_generate(bridge
, level
, statid
);
1199 EXPORT_SYMBOL(vme_irq_generate
);
1202 * Request the location monitor, return resource or NULL
1204 struct vme_resource
*vme_lm_request(struct vme_dev
*vdev
)
1206 struct vme_bridge
*bridge
;
1207 struct list_head
*lm_pos
= NULL
;
1208 struct vme_lm_resource
*allocated_lm
= NULL
;
1209 struct vme_lm_resource
*lm
= NULL
;
1210 struct vme_resource
*resource
= NULL
;
1212 bridge
= vdev
->bridge
;
1213 if (bridge
== NULL
) {
1214 printk(KERN_ERR
"Can't find VME bus\n");
1218 /* Loop through DMA resources */
1219 list_for_each(lm_pos
, &bridge
->lm_resources
) {
1220 lm
= list_entry(lm_pos
,
1221 struct vme_lm_resource
, list
);
1224 printk(KERN_ERR
"Registered NULL Location Monitor resource\n");
1228 /* Find an unlocked controller */
1229 mutex_lock(&lm
->mtx
);
1230 if (lm
->locked
== 0) {
1232 mutex_unlock(&lm
->mtx
);
1236 mutex_unlock(&lm
->mtx
);
1239 /* Check to see if we found a resource */
1240 if (allocated_lm
== NULL
)
1243 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
1244 if (resource
== NULL
) {
1245 printk(KERN_ERR
"Unable to allocate resource structure\n");
1248 resource
->type
= VME_LM
;
1249 resource
->entry
= &allocated_lm
->list
;
1255 mutex_lock(&lm
->mtx
);
1257 mutex_unlock(&lm
->mtx
);
1262 EXPORT_SYMBOL(vme_lm_request
);
1264 int vme_lm_count(struct vme_resource
*resource
)
1266 struct vme_lm_resource
*lm
;
1268 if (resource
->type
!= VME_LM
) {
1269 printk(KERN_ERR
"Not a Location Monitor resource\n");
1273 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1275 return lm
->monitors
;
1277 EXPORT_SYMBOL(vme_lm_count
);
1279 int vme_lm_set(struct vme_resource
*resource
, unsigned long long lm_base
,
1280 u32 aspace
, u32 cycle
)
1282 struct vme_bridge
*bridge
= find_bridge(resource
);
1283 struct vme_lm_resource
*lm
;
1285 if (resource
->type
!= VME_LM
) {
1286 printk(KERN_ERR
"Not a Location Monitor resource\n");
1290 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1292 if (bridge
->lm_set
== NULL
) {
1293 printk(KERN_ERR
"vme_lm_set not supported\n");
1297 return bridge
->lm_set(lm
, lm_base
, aspace
, cycle
);
1299 EXPORT_SYMBOL(vme_lm_set
);
1301 int vme_lm_get(struct vme_resource
*resource
, unsigned long long *lm_base
,
1302 u32
*aspace
, u32
*cycle
)
1304 struct vme_bridge
*bridge
= find_bridge(resource
);
1305 struct vme_lm_resource
*lm
;
1307 if (resource
->type
!= VME_LM
) {
1308 printk(KERN_ERR
"Not a Location Monitor resource\n");
1312 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1314 if (bridge
->lm_get
== NULL
) {
1315 printk(KERN_ERR
"vme_lm_get not supported\n");
1319 return bridge
->lm_get(lm
, lm_base
, aspace
, cycle
);
1321 EXPORT_SYMBOL(vme_lm_get
);
1323 int vme_lm_attach(struct vme_resource
*resource
, int monitor
,
1324 void (*callback
)(int))
1326 struct vme_bridge
*bridge
= find_bridge(resource
);
1327 struct vme_lm_resource
*lm
;
1329 if (resource
->type
!= VME_LM
) {
1330 printk(KERN_ERR
"Not a Location Monitor resource\n");
1334 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1336 if (bridge
->lm_attach
== NULL
) {
1337 printk(KERN_ERR
"vme_lm_attach not supported\n");
1341 return bridge
->lm_attach(lm
, monitor
, callback
);
1343 EXPORT_SYMBOL(vme_lm_attach
);
1345 int vme_lm_detach(struct vme_resource
*resource
, int monitor
)
1347 struct vme_bridge
*bridge
= find_bridge(resource
);
1348 struct vme_lm_resource
*lm
;
1350 if (resource
->type
!= VME_LM
) {
1351 printk(KERN_ERR
"Not a Location Monitor resource\n");
1355 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1357 if (bridge
->lm_detach
== NULL
) {
1358 printk(KERN_ERR
"vme_lm_detach not supported\n");
1362 return bridge
->lm_detach(lm
, monitor
);
1364 EXPORT_SYMBOL(vme_lm_detach
);
1366 void vme_lm_free(struct vme_resource
*resource
)
1368 struct vme_lm_resource
*lm
;
1370 if (resource
->type
!= VME_LM
) {
1371 printk(KERN_ERR
"Not a Location Monitor resource\n");
1375 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1377 mutex_lock(&lm
->mtx
);
1380 * Check to see that there aren't any callbacks still attached, if
1381 * there are we should probably be detaching them!
1386 mutex_unlock(&lm
->mtx
);
1390 EXPORT_SYMBOL(vme_lm_free
);
1392 int vme_slot_num(struct vme_dev
*vdev
)
1394 struct vme_bridge
*bridge
;
1396 bridge
= vdev
->bridge
;
1397 if (bridge
== NULL
) {
1398 printk(KERN_ERR
"Can't find VME bus\n");
1402 if (bridge
->slot_get
== NULL
) {
1403 printk(KERN_WARNING
"vme_slot_num not supported\n");
1407 return bridge
->slot_get(bridge
);
1409 EXPORT_SYMBOL(vme_slot_num
);
1411 int vme_bus_num(struct vme_dev
*vdev
)
1413 struct vme_bridge
*bridge
;
1415 bridge
= vdev
->bridge
;
1416 if (bridge
== NULL
) {
1417 pr_err("Can't find VME bus\n");
1423 EXPORT_SYMBOL(vme_bus_num
);
1425 /* - Bridge Registration --------------------------------------------------- */
1427 static void vme_dev_release(struct device
*dev
)
1429 kfree(dev_to_vme_dev(dev
));
1432 int vme_register_bridge(struct vme_bridge
*bridge
)
1437 mutex_lock(&vme_buses_lock
);
1438 for (i
= 0; i
< sizeof(vme_bus_numbers
) * 8; i
++) {
1439 if ((vme_bus_numbers
& (1 << i
)) == 0) {
1440 vme_bus_numbers
|= (1 << i
);
1442 INIT_LIST_HEAD(&bridge
->devices
);
1443 list_add_tail(&bridge
->bus_list
, &vme_bus_list
);
1448 mutex_unlock(&vme_buses_lock
);
1452 EXPORT_SYMBOL(vme_register_bridge
);
1454 void vme_unregister_bridge(struct vme_bridge
*bridge
)
1456 struct vme_dev
*vdev
;
1457 struct vme_dev
*tmp
;
1459 mutex_lock(&vme_buses_lock
);
1460 vme_bus_numbers
&= ~(1 << bridge
->num
);
1461 list_for_each_entry_safe(vdev
, tmp
, &bridge
->devices
, bridge_list
) {
1462 list_del(&vdev
->drv_list
);
1463 list_del(&vdev
->bridge_list
);
1464 device_unregister(&vdev
->dev
);
1466 list_del(&bridge
->bus_list
);
1467 mutex_unlock(&vme_buses_lock
);
1469 EXPORT_SYMBOL(vme_unregister_bridge
);
1471 /* - Driver Registration --------------------------------------------------- */
1473 static int __vme_register_driver_bus(struct vme_driver
*drv
,
1474 struct vme_bridge
*bridge
, unsigned int ndevs
)
1478 struct vme_dev
*vdev
;
1479 struct vme_dev
*tmp
;
1481 for (i
= 0; i
< ndevs
; i
++) {
1482 vdev
= kzalloc(sizeof(struct vme_dev
), GFP_KERNEL
);
1488 vdev
->bridge
= bridge
;
1489 vdev
->dev
.platform_data
= drv
;
1490 vdev
->dev
.release
= vme_dev_release
;
1491 vdev
->dev
.parent
= bridge
->parent
;
1492 vdev
->dev
.bus
= &vme_bus_type
;
1493 dev_set_name(&vdev
->dev
, "%s.%u-%u", drv
->name
, bridge
->num
,
1496 err
= device_register(&vdev
->dev
);
1500 if (vdev
->dev
.platform_data
) {
1501 list_add_tail(&vdev
->drv_list
, &drv
->devices
);
1502 list_add_tail(&vdev
->bridge_list
, &bridge
->devices
);
1504 device_unregister(&vdev
->dev
);
1509 put_device(&vdev
->dev
);
1512 list_for_each_entry_safe(vdev
, tmp
, &drv
->devices
, drv_list
) {
1513 list_del(&vdev
->drv_list
);
1514 list_del(&vdev
->bridge_list
);
1515 device_unregister(&vdev
->dev
);
1520 static int __vme_register_driver(struct vme_driver
*drv
, unsigned int ndevs
)
1522 struct vme_bridge
*bridge
;
1525 mutex_lock(&vme_buses_lock
);
1526 list_for_each_entry(bridge
, &vme_bus_list
, bus_list
) {
1528 * This cannot cause trouble as we already have vme_buses_lock
1529 * and if the bridge is removed, it will have to go through
1530 * vme_unregister_bridge() to do it (which calls remove() on
1531 * the bridge which in turn tries to acquire vme_buses_lock and
1532 * will have to wait).
1534 err
= __vme_register_driver_bus(drv
, bridge
, ndevs
);
1538 mutex_unlock(&vme_buses_lock
);
1542 int vme_register_driver(struct vme_driver
*drv
, unsigned int ndevs
)
1546 drv
->driver
.name
= drv
->name
;
1547 drv
->driver
.bus
= &vme_bus_type
;
1548 INIT_LIST_HEAD(&drv
->devices
);
1550 err
= driver_register(&drv
->driver
);
1554 err
= __vme_register_driver(drv
, ndevs
);
1556 driver_unregister(&drv
->driver
);
1560 EXPORT_SYMBOL(vme_register_driver
);
1562 void vme_unregister_driver(struct vme_driver
*drv
)
1564 struct vme_dev
*dev
, *dev_tmp
;
1566 mutex_lock(&vme_buses_lock
);
1567 list_for_each_entry_safe(dev
, dev_tmp
, &drv
->devices
, drv_list
) {
1568 list_del(&dev
->drv_list
);
1569 list_del(&dev
->bridge_list
);
1570 device_unregister(&dev
->dev
);
1572 mutex_unlock(&vme_buses_lock
);
1574 driver_unregister(&drv
->driver
);
1576 EXPORT_SYMBOL(vme_unregister_driver
);
1578 /* - Bus Registration ------------------------------------------------------ */
1580 static int vme_bus_match(struct device
*dev
, struct device_driver
*drv
)
1582 struct vme_driver
*vme_drv
;
1584 vme_drv
= container_of(drv
, struct vme_driver
, driver
);
1586 if (dev
->platform_data
== vme_drv
) {
1587 struct vme_dev
*vdev
= dev_to_vme_dev(dev
);
1589 if (vme_drv
->match
&& vme_drv
->match(vdev
))
1592 dev
->platform_data
= NULL
;
1597 static int vme_bus_probe(struct device
*dev
)
1599 int retval
= -ENODEV
;
1600 struct vme_driver
*driver
;
1601 struct vme_dev
*vdev
= dev_to_vme_dev(dev
);
1603 driver
= dev
->platform_data
;
1605 if (driver
->probe
!= NULL
)
1606 retval
= driver
->probe(vdev
);
1611 static int vme_bus_remove(struct device
*dev
)
1613 int retval
= -ENODEV
;
1614 struct vme_driver
*driver
;
1615 struct vme_dev
*vdev
= dev_to_vme_dev(dev
);
1617 driver
= dev
->platform_data
;
1619 if (driver
->remove
!= NULL
)
1620 retval
= driver
->remove(vdev
);
1625 struct bus_type vme_bus_type
= {
1627 .match
= vme_bus_match
,
1628 .probe
= vme_bus_probe
,
1629 .remove
= vme_bus_remove
,
1631 EXPORT_SYMBOL(vme_bus_type
);
1633 static int __init
vme_init(void)
1635 return bus_register(&vme_bus_type
);
1638 static void __exit
vme_exit(void)
1640 bus_unregister(&vme_bus_type
);
1643 subsys_initcall(vme_init
);
1644 module_exit(vme_exit
);