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 static 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
))
203 * Any value held in an unsigned long long can be used as the
208 if (((vme_base
+ size
) > VME_CRCSR_MAX
) ||
209 (vme_base
> VME_CRCSR_MAX
))
219 printk(KERN_ERR
"Invalid address space\n");
228 * Request a slave image with specific attributes, return some unique
231 struct vme_resource
*vme_slave_request(struct vme_dev
*vdev
, u32 address
,
234 struct vme_bridge
*bridge
;
235 struct list_head
*slave_pos
= NULL
;
236 struct vme_slave_resource
*allocated_image
= NULL
;
237 struct vme_slave_resource
*slave_image
= NULL
;
238 struct vme_resource
*resource
= NULL
;
240 bridge
= vdev
->bridge
;
241 if (bridge
== NULL
) {
242 printk(KERN_ERR
"Can't find VME bus\n");
246 /* Loop through slave resources */
247 list_for_each(slave_pos
, &bridge
->slave_resources
) {
248 slave_image
= list_entry(slave_pos
,
249 struct vme_slave_resource
, list
);
251 if (slave_image
== NULL
) {
252 printk(KERN_ERR
"Registered NULL Slave resource\n");
256 /* Find an unlocked and compatible image */
257 mutex_lock(&slave_image
->mtx
);
258 if (((slave_image
->address_attr
& address
) == address
) &&
259 ((slave_image
->cycle_attr
& cycle
) == cycle
) &&
260 (slave_image
->locked
== 0)) {
262 slave_image
->locked
= 1;
263 mutex_unlock(&slave_image
->mtx
);
264 allocated_image
= slave_image
;
267 mutex_unlock(&slave_image
->mtx
);
271 if (allocated_image
== NULL
)
274 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
275 if (resource
== NULL
) {
276 printk(KERN_WARNING
"Unable to allocate resource structure\n");
279 resource
->type
= VME_SLAVE
;
280 resource
->entry
= &allocated_image
->list
;
286 mutex_lock(&slave_image
->mtx
);
287 slave_image
->locked
= 0;
288 mutex_unlock(&slave_image
->mtx
);
293 EXPORT_SYMBOL(vme_slave_request
);
295 int vme_slave_set(struct vme_resource
*resource
, int enabled
,
296 unsigned long long vme_base
, unsigned long long size
,
297 dma_addr_t buf_base
, u32 aspace
, u32 cycle
)
299 struct vme_bridge
*bridge
= find_bridge(resource
);
300 struct vme_slave_resource
*image
;
303 if (resource
->type
!= VME_SLAVE
) {
304 printk(KERN_ERR
"Not a slave resource\n");
308 image
= list_entry(resource
->entry
, struct vme_slave_resource
, list
);
310 if (bridge
->slave_set
== NULL
) {
311 printk(KERN_ERR
"Function not supported\n");
315 if (!(((image
->address_attr
& aspace
) == aspace
) &&
316 ((image
->cycle_attr
& cycle
) == cycle
))) {
317 printk(KERN_ERR
"Invalid attributes\n");
321 retval
= vme_check_window(aspace
, vme_base
, size
);
325 return bridge
->slave_set(image
, enabled
, vme_base
, size
, buf_base
,
328 EXPORT_SYMBOL(vme_slave_set
);
330 int vme_slave_get(struct vme_resource
*resource
, int *enabled
,
331 unsigned long long *vme_base
, unsigned long long *size
,
332 dma_addr_t
*buf_base
, u32
*aspace
, u32
*cycle
)
334 struct vme_bridge
*bridge
= find_bridge(resource
);
335 struct vme_slave_resource
*image
;
337 if (resource
->type
!= VME_SLAVE
) {
338 printk(KERN_ERR
"Not a slave resource\n");
342 image
= list_entry(resource
->entry
, struct vme_slave_resource
, list
);
344 if (bridge
->slave_get
== NULL
) {
345 printk(KERN_ERR
"vme_slave_get not supported\n");
349 return bridge
->slave_get(image
, enabled
, vme_base
, size
, buf_base
,
352 EXPORT_SYMBOL(vme_slave_get
);
354 void vme_slave_free(struct vme_resource
*resource
)
356 struct vme_slave_resource
*slave_image
;
358 if (resource
->type
!= VME_SLAVE
) {
359 printk(KERN_ERR
"Not a slave resource\n");
363 slave_image
= list_entry(resource
->entry
, struct vme_slave_resource
,
365 if (slave_image
== NULL
) {
366 printk(KERN_ERR
"Can't find slave resource\n");
371 mutex_lock(&slave_image
->mtx
);
372 if (slave_image
->locked
== 0)
373 printk(KERN_ERR
"Image is already free\n");
375 slave_image
->locked
= 0;
376 mutex_unlock(&slave_image
->mtx
);
378 /* Free up resource memory */
381 EXPORT_SYMBOL(vme_slave_free
);
384 * Request a master image with specific attributes, return some unique
387 struct vme_resource
*vme_master_request(struct vme_dev
*vdev
, u32 address
,
388 u32 cycle
, u32 dwidth
)
390 struct vme_bridge
*bridge
;
391 struct list_head
*master_pos
= NULL
;
392 struct vme_master_resource
*allocated_image
= NULL
;
393 struct vme_master_resource
*master_image
= NULL
;
394 struct vme_resource
*resource
= NULL
;
396 bridge
= vdev
->bridge
;
397 if (bridge
== NULL
) {
398 printk(KERN_ERR
"Can't find VME bus\n");
402 /* Loop through master resources */
403 list_for_each(master_pos
, &bridge
->master_resources
) {
404 master_image
= list_entry(master_pos
,
405 struct vme_master_resource
, list
);
407 if (master_image
== NULL
) {
408 printk(KERN_WARNING
"Registered NULL master resource\n");
412 /* Find an unlocked and compatible image */
413 spin_lock(&master_image
->lock
);
414 if (((master_image
->address_attr
& address
) == address
) &&
415 ((master_image
->cycle_attr
& cycle
) == cycle
) &&
416 ((master_image
->width_attr
& dwidth
) == dwidth
) &&
417 (master_image
->locked
== 0)) {
419 master_image
->locked
= 1;
420 spin_unlock(&master_image
->lock
);
421 allocated_image
= master_image
;
424 spin_unlock(&master_image
->lock
);
427 /* Check to see if we found a resource */
428 if (allocated_image
== NULL
) {
429 printk(KERN_ERR
"Can't find a suitable resource\n");
433 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
434 if (resource
== NULL
) {
435 printk(KERN_ERR
"Unable to allocate resource structure\n");
438 resource
->type
= VME_MASTER
;
439 resource
->entry
= &allocated_image
->list
;
445 spin_lock(&master_image
->lock
);
446 master_image
->locked
= 0;
447 spin_unlock(&master_image
->lock
);
452 EXPORT_SYMBOL(vme_master_request
);
454 int vme_master_set(struct vme_resource
*resource
, int enabled
,
455 unsigned long long vme_base
, unsigned long long size
, u32 aspace
,
456 u32 cycle
, u32 dwidth
)
458 struct vme_bridge
*bridge
= find_bridge(resource
);
459 struct vme_master_resource
*image
;
462 if (resource
->type
!= VME_MASTER
) {
463 printk(KERN_ERR
"Not a master resource\n");
467 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
469 if (bridge
->master_set
== NULL
) {
470 printk(KERN_WARNING
"vme_master_set not supported\n");
474 if (!(((image
->address_attr
& aspace
) == aspace
) &&
475 ((image
->cycle_attr
& cycle
) == cycle
) &&
476 ((image
->width_attr
& dwidth
) == dwidth
))) {
477 printk(KERN_WARNING
"Invalid attributes\n");
481 retval
= vme_check_window(aspace
, vme_base
, size
);
485 return bridge
->master_set(image
, enabled
, vme_base
, size
, aspace
,
488 EXPORT_SYMBOL(vme_master_set
);
490 int vme_master_get(struct vme_resource
*resource
, int *enabled
,
491 unsigned long long *vme_base
, unsigned long long *size
, u32
*aspace
,
492 u32
*cycle
, u32
*dwidth
)
494 struct vme_bridge
*bridge
= find_bridge(resource
);
495 struct vme_master_resource
*image
;
497 if (resource
->type
!= VME_MASTER
) {
498 printk(KERN_ERR
"Not a master resource\n");
502 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
504 if (bridge
->master_get
== NULL
) {
505 printk(KERN_WARNING
"vme_master_set not supported\n");
509 return bridge
->master_get(image
, enabled
, vme_base
, size
, aspace
,
512 EXPORT_SYMBOL(vme_master_get
);
515 * Read data out of VME space into a buffer.
517 ssize_t
vme_master_read(struct vme_resource
*resource
, void *buf
, size_t count
,
520 struct vme_bridge
*bridge
= find_bridge(resource
);
521 struct vme_master_resource
*image
;
524 if (bridge
->master_read
== NULL
) {
525 printk(KERN_WARNING
"Reading from resource not supported\n");
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 length
= vme_get_size(resource
);
538 if (offset
> length
) {
539 printk(KERN_WARNING
"Invalid Offset\n");
543 if ((offset
+ count
) > length
)
544 count
= length
- offset
;
546 return bridge
->master_read(image
, buf
, count
, offset
);
549 EXPORT_SYMBOL(vme_master_read
);
552 * Write data out to VME space from a buffer.
554 ssize_t
vme_master_write(struct vme_resource
*resource
, void *buf
,
555 size_t count
, loff_t offset
)
557 struct vme_bridge
*bridge
= find_bridge(resource
);
558 struct vme_master_resource
*image
;
561 if (bridge
->master_write
== NULL
) {
562 printk(KERN_WARNING
"Writing to resource not supported\n");
566 if (resource
->type
!= VME_MASTER
) {
567 printk(KERN_ERR
"Not a master resource\n");
571 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
573 length
= vme_get_size(resource
);
575 if (offset
> length
) {
576 printk(KERN_WARNING
"Invalid Offset\n");
580 if ((offset
+ count
) > length
)
581 count
= length
- offset
;
583 return bridge
->master_write(image
, buf
, count
, offset
);
585 EXPORT_SYMBOL(vme_master_write
);
588 * Perform RMW cycle to provided location.
590 unsigned int vme_master_rmw(struct vme_resource
*resource
, unsigned int mask
,
591 unsigned int compare
, unsigned int swap
, loff_t offset
)
593 struct vme_bridge
*bridge
= find_bridge(resource
);
594 struct vme_master_resource
*image
;
596 if (bridge
->master_rmw
== NULL
) {
597 printk(KERN_WARNING
"Writing to resource not supported\n");
601 if (resource
->type
!= VME_MASTER
) {
602 printk(KERN_ERR
"Not a master resource\n");
606 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
608 return bridge
->master_rmw(image
, mask
, compare
, swap
, offset
);
610 EXPORT_SYMBOL(vme_master_rmw
);
612 void vme_master_free(struct vme_resource
*resource
)
614 struct vme_master_resource
*master_image
;
616 if (resource
->type
!= VME_MASTER
) {
617 printk(KERN_ERR
"Not a master resource\n");
621 master_image
= list_entry(resource
->entry
, struct vme_master_resource
,
623 if (master_image
== NULL
) {
624 printk(KERN_ERR
"Can't find master resource\n");
629 spin_lock(&master_image
->lock
);
630 if (master_image
->locked
== 0)
631 printk(KERN_ERR
"Image is already free\n");
633 master_image
->locked
= 0;
634 spin_unlock(&master_image
->lock
);
636 /* Free up resource memory */
639 EXPORT_SYMBOL(vme_master_free
);
642 * Request a DMA controller with specific attributes, return some unique
645 struct vme_resource
*vme_dma_request(struct vme_dev
*vdev
, u32 route
)
647 struct vme_bridge
*bridge
;
648 struct list_head
*dma_pos
= NULL
;
649 struct vme_dma_resource
*allocated_ctrlr
= NULL
;
650 struct vme_dma_resource
*dma_ctrlr
= NULL
;
651 struct vme_resource
*resource
= NULL
;
653 /* XXX Not checking resource attributes */
654 printk(KERN_ERR
"No VME resource Attribute tests done\n");
656 bridge
= vdev
->bridge
;
657 if (bridge
== NULL
) {
658 printk(KERN_ERR
"Can't find VME bus\n");
662 /* Loop through DMA resources */
663 list_for_each(dma_pos
, &bridge
->dma_resources
) {
664 dma_ctrlr
= list_entry(dma_pos
,
665 struct vme_dma_resource
, list
);
667 if (dma_ctrlr
== NULL
) {
668 printk(KERN_ERR
"Registered NULL DMA resource\n");
672 /* Find an unlocked and compatible controller */
673 mutex_lock(&dma_ctrlr
->mtx
);
674 if (((dma_ctrlr
->route_attr
& route
) == route
) &&
675 (dma_ctrlr
->locked
== 0)) {
677 dma_ctrlr
->locked
= 1;
678 mutex_unlock(&dma_ctrlr
->mtx
);
679 allocated_ctrlr
= dma_ctrlr
;
682 mutex_unlock(&dma_ctrlr
->mtx
);
685 /* Check to see if we found a resource */
686 if (allocated_ctrlr
== NULL
)
689 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
690 if (resource
== NULL
) {
691 printk(KERN_WARNING
"Unable to allocate resource structure\n");
694 resource
->type
= VME_DMA
;
695 resource
->entry
= &allocated_ctrlr
->list
;
701 mutex_lock(&dma_ctrlr
->mtx
);
702 dma_ctrlr
->locked
= 0;
703 mutex_unlock(&dma_ctrlr
->mtx
);
708 EXPORT_SYMBOL(vme_dma_request
);
713 struct vme_dma_list
*vme_new_dma_list(struct vme_resource
*resource
)
715 struct vme_dma_resource
*ctrlr
;
716 struct vme_dma_list
*dma_list
;
718 if (resource
->type
!= VME_DMA
) {
719 printk(KERN_ERR
"Not a DMA resource\n");
723 ctrlr
= list_entry(resource
->entry
, struct vme_dma_resource
, list
);
725 dma_list
= kmalloc(sizeof(struct vme_dma_list
), GFP_KERNEL
);
726 if (dma_list
== NULL
) {
727 printk(KERN_ERR
"Unable to allocate memory for new dma list\n");
730 INIT_LIST_HEAD(&dma_list
->entries
);
731 dma_list
->parent
= ctrlr
;
732 mutex_init(&dma_list
->mtx
);
736 EXPORT_SYMBOL(vme_new_dma_list
);
739 * Create "Pattern" type attributes
741 struct vme_dma_attr
*vme_dma_pattern_attribute(u32 pattern
, u32 type
)
743 struct vme_dma_attr
*attributes
;
744 struct vme_dma_pattern
*pattern_attr
;
746 attributes
= kmalloc(sizeof(struct vme_dma_attr
), GFP_KERNEL
);
747 if (attributes
== NULL
) {
748 printk(KERN_ERR
"Unable to allocate memory for attributes structure\n");
752 pattern_attr
= kmalloc(sizeof(struct vme_dma_pattern
), GFP_KERNEL
);
753 if (pattern_attr
== NULL
) {
754 printk(KERN_ERR
"Unable to allocate memory for pattern attributes\n");
758 attributes
->type
= VME_DMA_PATTERN
;
759 attributes
->private = (void *)pattern_attr
;
761 pattern_attr
->pattern
= pattern
;
762 pattern_attr
->type
= type
;
771 EXPORT_SYMBOL(vme_dma_pattern_attribute
);
774 * Create "PCI" type attributes
776 struct vme_dma_attr
*vme_dma_pci_attribute(dma_addr_t address
)
778 struct vme_dma_attr
*attributes
;
779 struct vme_dma_pci
*pci_attr
;
781 /* XXX Run some sanity checks here */
783 attributes
= kmalloc(sizeof(struct vme_dma_attr
), GFP_KERNEL
);
784 if (attributes
== NULL
) {
785 printk(KERN_ERR
"Unable to allocate memory for attributes structure\n");
789 pci_attr
= kmalloc(sizeof(struct vme_dma_pci
), GFP_KERNEL
);
790 if (pci_attr
== NULL
) {
791 printk(KERN_ERR
"Unable to allocate memory for pci attributes\n");
797 attributes
->type
= VME_DMA_PCI
;
798 attributes
->private = (void *)pci_attr
;
800 pci_attr
->address
= address
;
809 EXPORT_SYMBOL(vme_dma_pci_attribute
);
812 * Create "VME" type attributes
814 struct vme_dma_attr
*vme_dma_vme_attribute(unsigned long long address
,
815 u32 aspace
, u32 cycle
, u32 dwidth
)
817 struct vme_dma_attr
*attributes
;
818 struct vme_dma_vme
*vme_attr
;
820 attributes
= kmalloc(
821 sizeof(struct vme_dma_attr
), GFP_KERNEL
);
822 if (attributes
== NULL
) {
823 printk(KERN_ERR
"Unable to allocate memory for attributes structure\n");
827 vme_attr
= kmalloc(sizeof(struct vme_dma_vme
), GFP_KERNEL
);
828 if (vme_attr
== NULL
) {
829 printk(KERN_ERR
"Unable to allocate memory for vme attributes\n");
833 attributes
->type
= VME_DMA_VME
;
834 attributes
->private = (void *)vme_attr
;
836 vme_attr
->address
= address
;
837 vme_attr
->aspace
= aspace
;
838 vme_attr
->cycle
= cycle
;
839 vme_attr
->dwidth
= dwidth
;
848 EXPORT_SYMBOL(vme_dma_vme_attribute
);
853 void vme_dma_free_attribute(struct vme_dma_attr
*attributes
)
855 kfree(attributes
->private);
858 EXPORT_SYMBOL(vme_dma_free_attribute
);
860 int vme_dma_list_add(struct vme_dma_list
*list
, struct vme_dma_attr
*src
,
861 struct vme_dma_attr
*dest
, size_t count
)
863 struct vme_bridge
*bridge
= list
->parent
->parent
;
866 if (bridge
->dma_list_add
== NULL
) {
867 printk(KERN_WARNING
"Link List DMA generation not supported\n");
871 if (!mutex_trylock(&list
->mtx
)) {
872 printk(KERN_ERR
"Link List already submitted\n");
876 retval
= bridge
->dma_list_add(list
, src
, dest
, count
);
878 mutex_unlock(&list
->mtx
);
882 EXPORT_SYMBOL(vme_dma_list_add
);
884 int vme_dma_list_exec(struct vme_dma_list
*list
)
886 struct vme_bridge
*bridge
= list
->parent
->parent
;
889 if (bridge
->dma_list_exec
== NULL
) {
890 printk(KERN_ERR
"Link List DMA execution not supported\n");
894 mutex_lock(&list
->mtx
);
896 retval
= bridge
->dma_list_exec(list
);
898 mutex_unlock(&list
->mtx
);
902 EXPORT_SYMBOL(vme_dma_list_exec
);
904 int vme_dma_list_free(struct vme_dma_list
*list
)
906 struct vme_bridge
*bridge
= list
->parent
->parent
;
909 if (bridge
->dma_list_empty
== NULL
) {
910 printk(KERN_WARNING
"Emptying of Link Lists not supported\n");
914 if (!mutex_trylock(&list
->mtx
)) {
915 printk(KERN_ERR
"Link List in use\n");
920 * Empty out all of the entries from the dma list. We need to go to the
921 * low level driver as dma entries are driver specific.
923 retval
= bridge
->dma_list_empty(list
);
925 printk(KERN_ERR
"Unable to empty link-list entries\n");
926 mutex_unlock(&list
->mtx
);
929 mutex_unlock(&list
->mtx
);
934 EXPORT_SYMBOL(vme_dma_list_free
);
936 int vme_dma_free(struct vme_resource
*resource
)
938 struct vme_dma_resource
*ctrlr
;
940 if (resource
->type
!= VME_DMA
) {
941 printk(KERN_ERR
"Not a DMA resource\n");
945 ctrlr
= list_entry(resource
->entry
, struct vme_dma_resource
, list
);
947 if (!mutex_trylock(&ctrlr
->mtx
)) {
948 printk(KERN_ERR
"Resource busy, can't free\n");
952 if (!(list_empty(&ctrlr
->pending
) && list_empty(&ctrlr
->running
))) {
953 printk(KERN_WARNING
"Resource still processing transfers\n");
954 mutex_unlock(&ctrlr
->mtx
);
960 mutex_unlock(&ctrlr
->mtx
);
964 EXPORT_SYMBOL(vme_dma_free
);
966 void vme_irq_handler(struct vme_bridge
*bridge
, int level
, int statid
)
968 void (*call
)(int, int, void *);
971 call
= bridge
->irq
[level
- 1].callback
[statid
].func
;
972 priv_data
= bridge
->irq
[level
- 1].callback
[statid
].priv_data
;
975 call(level
, statid
, priv_data
);
977 printk(KERN_WARNING
"Spurilous VME interrupt, level:%x, vector:%x\n",
980 EXPORT_SYMBOL(vme_irq_handler
);
982 int vme_irq_request(struct vme_dev
*vdev
, int level
, int statid
,
983 void (*callback
)(int, int, void *),
986 struct vme_bridge
*bridge
;
988 bridge
= vdev
->bridge
;
989 if (bridge
== NULL
) {
990 printk(KERN_ERR
"Can't find VME bus\n");
994 if ((level
< 1) || (level
> 7)) {
995 printk(KERN_ERR
"Invalid interrupt level\n");
999 if (bridge
->irq_set
== NULL
) {
1000 printk(KERN_ERR
"Configuring interrupts not supported\n");
1004 mutex_lock(&bridge
->irq_mtx
);
1006 if (bridge
->irq
[level
- 1].callback
[statid
].func
) {
1007 mutex_unlock(&bridge
->irq_mtx
);
1008 printk(KERN_WARNING
"VME Interrupt already taken\n");
1012 bridge
->irq
[level
- 1].count
++;
1013 bridge
->irq
[level
- 1].callback
[statid
].priv_data
= priv_data
;
1014 bridge
->irq
[level
- 1].callback
[statid
].func
= callback
;
1016 /* Enable IRQ level */
1017 bridge
->irq_set(bridge
, level
, 1, 1);
1019 mutex_unlock(&bridge
->irq_mtx
);
1023 EXPORT_SYMBOL(vme_irq_request
);
1025 void vme_irq_free(struct vme_dev
*vdev
, int level
, int statid
)
1027 struct vme_bridge
*bridge
;
1029 bridge
= vdev
->bridge
;
1030 if (bridge
== NULL
) {
1031 printk(KERN_ERR
"Can't find VME bus\n");
1035 if ((level
< 1) || (level
> 7)) {
1036 printk(KERN_ERR
"Invalid interrupt level\n");
1040 if (bridge
->irq_set
== NULL
) {
1041 printk(KERN_ERR
"Configuring interrupts not supported\n");
1045 mutex_lock(&bridge
->irq_mtx
);
1047 bridge
->irq
[level
- 1].count
--;
1049 /* Disable IRQ level if no more interrupts attached at this level*/
1050 if (bridge
->irq
[level
- 1].count
== 0)
1051 bridge
->irq_set(bridge
, level
, 0, 1);
1053 bridge
->irq
[level
- 1].callback
[statid
].func
= NULL
;
1054 bridge
->irq
[level
- 1].callback
[statid
].priv_data
= NULL
;
1056 mutex_unlock(&bridge
->irq_mtx
);
1058 EXPORT_SYMBOL(vme_irq_free
);
1060 int vme_irq_generate(struct vme_dev
*vdev
, int level
, int statid
)
1062 struct vme_bridge
*bridge
;
1064 bridge
= vdev
->bridge
;
1065 if (bridge
== NULL
) {
1066 printk(KERN_ERR
"Can't find VME bus\n");
1070 if ((level
< 1) || (level
> 7)) {
1071 printk(KERN_WARNING
"Invalid interrupt level\n");
1075 if (bridge
->irq_generate
== NULL
) {
1076 printk(KERN_WARNING
"Interrupt generation not supported\n");
1080 return bridge
->irq_generate(bridge
, level
, statid
);
1082 EXPORT_SYMBOL(vme_irq_generate
);
1085 * Request the location monitor, return resource or NULL
1087 struct vme_resource
*vme_lm_request(struct vme_dev
*vdev
)
1089 struct vme_bridge
*bridge
;
1090 struct list_head
*lm_pos
= NULL
;
1091 struct vme_lm_resource
*allocated_lm
= NULL
;
1092 struct vme_lm_resource
*lm
= NULL
;
1093 struct vme_resource
*resource
= NULL
;
1095 bridge
= vdev
->bridge
;
1096 if (bridge
== NULL
) {
1097 printk(KERN_ERR
"Can't find VME bus\n");
1101 /* Loop through DMA resources */
1102 list_for_each(lm_pos
, &bridge
->lm_resources
) {
1103 lm
= list_entry(lm_pos
,
1104 struct vme_lm_resource
, list
);
1107 printk(KERN_ERR
"Registered NULL Location Monitor resource\n");
1111 /* Find an unlocked controller */
1112 mutex_lock(&lm
->mtx
);
1113 if (lm
->locked
== 0) {
1115 mutex_unlock(&lm
->mtx
);
1119 mutex_unlock(&lm
->mtx
);
1122 /* Check to see if we found a resource */
1123 if (allocated_lm
== NULL
)
1126 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
1127 if (resource
== NULL
) {
1128 printk(KERN_ERR
"Unable to allocate resource structure\n");
1131 resource
->type
= VME_LM
;
1132 resource
->entry
= &allocated_lm
->list
;
1138 mutex_lock(&lm
->mtx
);
1140 mutex_unlock(&lm
->mtx
);
1145 EXPORT_SYMBOL(vme_lm_request
);
1147 int vme_lm_count(struct vme_resource
*resource
)
1149 struct vme_lm_resource
*lm
;
1151 if (resource
->type
!= VME_LM
) {
1152 printk(KERN_ERR
"Not a Location Monitor resource\n");
1156 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1158 return lm
->monitors
;
1160 EXPORT_SYMBOL(vme_lm_count
);
1162 int vme_lm_set(struct vme_resource
*resource
, unsigned long long lm_base
,
1163 u32 aspace
, u32 cycle
)
1165 struct vme_bridge
*bridge
= find_bridge(resource
);
1166 struct vme_lm_resource
*lm
;
1168 if (resource
->type
!= VME_LM
) {
1169 printk(KERN_ERR
"Not a Location Monitor resource\n");
1173 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1175 if (bridge
->lm_set
== NULL
) {
1176 printk(KERN_ERR
"vme_lm_set not supported\n");
1180 return bridge
->lm_set(lm
, lm_base
, aspace
, cycle
);
1182 EXPORT_SYMBOL(vme_lm_set
);
1184 int vme_lm_get(struct vme_resource
*resource
, unsigned long long *lm_base
,
1185 u32
*aspace
, u32
*cycle
)
1187 struct vme_bridge
*bridge
= find_bridge(resource
);
1188 struct vme_lm_resource
*lm
;
1190 if (resource
->type
!= VME_LM
) {
1191 printk(KERN_ERR
"Not a Location Monitor resource\n");
1195 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1197 if (bridge
->lm_get
== NULL
) {
1198 printk(KERN_ERR
"vme_lm_get not supported\n");
1202 return bridge
->lm_get(lm
, lm_base
, aspace
, cycle
);
1204 EXPORT_SYMBOL(vme_lm_get
);
1206 int vme_lm_attach(struct vme_resource
*resource
, int monitor
,
1207 void (*callback
)(int))
1209 struct vme_bridge
*bridge
= find_bridge(resource
);
1210 struct vme_lm_resource
*lm
;
1212 if (resource
->type
!= VME_LM
) {
1213 printk(KERN_ERR
"Not a Location Monitor resource\n");
1217 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1219 if (bridge
->lm_attach
== NULL
) {
1220 printk(KERN_ERR
"vme_lm_attach not supported\n");
1224 return bridge
->lm_attach(lm
, monitor
, callback
);
1226 EXPORT_SYMBOL(vme_lm_attach
);
1228 int vme_lm_detach(struct vme_resource
*resource
, int monitor
)
1230 struct vme_bridge
*bridge
= find_bridge(resource
);
1231 struct vme_lm_resource
*lm
;
1233 if (resource
->type
!= VME_LM
) {
1234 printk(KERN_ERR
"Not a Location Monitor resource\n");
1238 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1240 if (bridge
->lm_detach
== NULL
) {
1241 printk(KERN_ERR
"vme_lm_detach not supported\n");
1245 return bridge
->lm_detach(lm
, monitor
);
1247 EXPORT_SYMBOL(vme_lm_detach
);
1249 void vme_lm_free(struct vme_resource
*resource
)
1251 struct vme_lm_resource
*lm
;
1253 if (resource
->type
!= VME_LM
) {
1254 printk(KERN_ERR
"Not a Location Monitor resource\n");
1258 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1260 mutex_lock(&lm
->mtx
);
1263 * Check to see that there aren't any callbacks still attached, if
1264 * there are we should probably be detaching them!
1269 mutex_unlock(&lm
->mtx
);
1273 EXPORT_SYMBOL(vme_lm_free
);
1275 int vme_slot_get(struct vme_dev
*vdev
)
1277 struct vme_bridge
*bridge
;
1279 bridge
= vdev
->bridge
;
1280 if (bridge
== NULL
) {
1281 printk(KERN_ERR
"Can't find VME bus\n");
1285 if (bridge
->slot_get
== NULL
) {
1286 printk(KERN_WARNING
"vme_slot_get not supported\n");
1290 return bridge
->slot_get(bridge
);
1292 EXPORT_SYMBOL(vme_slot_get
);
1295 /* - Bridge Registration --------------------------------------------------- */
1297 static void vme_dev_release(struct device
*dev
)
1299 kfree(dev_to_vme_dev(dev
));
1302 int vme_register_bridge(struct vme_bridge
*bridge
)
1307 mutex_lock(&vme_buses_lock
);
1308 for (i
= 0; i
< sizeof(vme_bus_numbers
) * 8; i
++) {
1309 if ((vme_bus_numbers
& (1 << i
)) == 0) {
1310 vme_bus_numbers
|= (1 << i
);
1312 INIT_LIST_HEAD(&bridge
->devices
);
1313 list_add_tail(&bridge
->bus_list
, &vme_bus_list
);
1318 mutex_unlock(&vme_buses_lock
);
1322 EXPORT_SYMBOL(vme_register_bridge
);
1324 void vme_unregister_bridge(struct vme_bridge
*bridge
)
1326 struct vme_dev
*vdev
;
1327 struct vme_dev
*tmp
;
1329 mutex_lock(&vme_buses_lock
);
1330 vme_bus_numbers
&= ~(1 << bridge
->num
);
1331 list_for_each_entry_safe(vdev
, tmp
, &bridge
->devices
, bridge_list
) {
1332 list_del(&vdev
->drv_list
);
1333 list_del(&vdev
->bridge_list
);
1334 device_unregister(&vdev
->dev
);
1336 list_del(&bridge
->bus_list
);
1337 mutex_unlock(&vme_buses_lock
);
1339 EXPORT_SYMBOL(vme_unregister_bridge
);
1341 /* - Driver Registration --------------------------------------------------- */
1343 static int __vme_register_driver_bus(struct vme_driver
*drv
,
1344 struct vme_bridge
*bridge
, unsigned int ndevs
)
1348 struct vme_dev
*vdev
;
1349 struct vme_dev
*tmp
;
1351 for (i
= 0; i
< ndevs
; i
++) {
1352 vdev
= kzalloc(sizeof(struct vme_dev
), GFP_KERNEL
);
1358 vdev
->bridge
= bridge
;
1359 vdev
->dev
.platform_data
= drv
;
1360 vdev
->dev
.release
= vme_dev_release
;
1361 vdev
->dev
.parent
= bridge
->parent
;
1362 vdev
->dev
.bus
= &vme_bus_type
;
1363 dev_set_name(&vdev
->dev
, "%s.%u-%u", drv
->name
, bridge
->num
,
1366 err
= device_register(&vdev
->dev
);
1370 if (vdev
->dev
.platform_data
) {
1371 list_add_tail(&vdev
->drv_list
, &drv
->devices
);
1372 list_add_tail(&vdev
->bridge_list
, &bridge
->devices
);
1374 device_unregister(&vdev
->dev
);
1381 list_for_each_entry_safe(vdev
, tmp
, &drv
->devices
, drv_list
) {
1382 list_del(&vdev
->drv_list
);
1383 list_del(&vdev
->bridge_list
);
1384 device_unregister(&vdev
->dev
);
1389 static int __vme_register_driver(struct vme_driver
*drv
, unsigned int ndevs
)
1391 struct vme_bridge
*bridge
;
1394 mutex_lock(&vme_buses_lock
);
1395 list_for_each_entry(bridge
, &vme_bus_list
, bus_list
) {
1397 * This cannot cause trouble as we already have vme_buses_lock
1398 * and if the bridge is removed, it will have to go through
1399 * vme_unregister_bridge() to do it (which calls remove() on
1400 * the bridge which in turn tries to acquire vme_buses_lock and
1401 * will have to wait).
1403 err
= __vme_register_driver_bus(drv
, bridge
, ndevs
);
1407 mutex_unlock(&vme_buses_lock
);
1411 int vme_register_driver(struct vme_driver
*drv
, unsigned int ndevs
)
1415 drv
->driver
.name
= drv
->name
;
1416 drv
->driver
.bus
= &vme_bus_type
;
1417 INIT_LIST_HEAD(&drv
->devices
);
1419 err
= driver_register(&drv
->driver
);
1423 err
= __vme_register_driver(drv
, ndevs
);
1425 driver_unregister(&drv
->driver
);
1429 EXPORT_SYMBOL(vme_register_driver
);
1431 void vme_unregister_driver(struct vme_driver
*drv
)
1433 struct vme_dev
*dev
, *dev_tmp
;
1435 mutex_lock(&vme_buses_lock
);
1436 list_for_each_entry_safe(dev
, dev_tmp
, &drv
->devices
, drv_list
) {
1437 list_del(&dev
->drv_list
);
1438 list_del(&dev
->bridge_list
);
1439 device_unregister(&dev
->dev
);
1441 mutex_unlock(&vme_buses_lock
);
1443 driver_unregister(&drv
->driver
);
1445 EXPORT_SYMBOL(vme_unregister_driver
);
1447 /* - Bus Registration ------------------------------------------------------ */
1449 static int vme_bus_match(struct device
*dev
, struct device_driver
*drv
)
1451 struct vme_driver
*vme_drv
;
1453 vme_drv
= container_of(drv
, struct vme_driver
, driver
);
1455 if (dev
->platform_data
== vme_drv
) {
1456 struct vme_dev
*vdev
= dev_to_vme_dev(dev
);
1458 if (vme_drv
->match
&& vme_drv
->match(vdev
))
1461 dev
->platform_data
= NULL
;
1466 static int vme_bus_probe(struct device
*dev
)
1468 int retval
= -ENODEV
;
1469 struct vme_driver
*driver
;
1470 struct vme_dev
*vdev
= dev_to_vme_dev(dev
);
1472 driver
= dev
->platform_data
;
1474 if (driver
->probe
!= NULL
)
1475 retval
= driver
->probe(vdev
);
1480 static int vme_bus_remove(struct device
*dev
)
1482 int retval
= -ENODEV
;
1483 struct vme_driver
*driver
;
1484 struct vme_dev
*vdev
= dev_to_vme_dev(dev
);
1486 driver
= dev
->platform_data
;
1488 if (driver
->remove
!= NULL
)
1489 retval
= driver
->remove(vdev
);
1494 struct bus_type vme_bus_type
= {
1496 .match
= vme_bus_match
,
1497 .probe
= vme_bus_probe
,
1498 .remove
= vme_bus_remove
,
1500 EXPORT_SYMBOL(vme_bus_type
);
1502 static int __init
vme_init(void)
1504 return bus_register(&vme_bus_type
);
1507 static void __exit
vme_exit(void)
1509 bus_unregister(&vme_bus_type
);
1512 MODULE_DESCRIPTION("VME bridge driver framework");
1513 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
1514 MODULE_LICENSE("GPL");
1516 module_init(vme_init
);
1517 module_exit(vme_exit
);