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 vme_address_t aspace
;
160 switch (resource
->type
) {
162 retval
= vme_master_get(resource
, &enabled
, &base
, &size
,
163 &aspace
, &cycle
, &dwidth
);
168 retval
= vme_slave_get(resource
, &enabled
, &base
, &size
,
169 &buf_base
, &aspace
, &cycle
);
177 printk(KERN_ERR
"Unknown resource type\n");
182 EXPORT_SYMBOL(vme_get_size
);
184 static int vme_check_window(vme_address_t aspace
, unsigned long long vme_base
,
185 unsigned long long size
)
191 if (((vme_base
+ size
) > VME_A16_MAX
) ||
192 (vme_base
> VME_A16_MAX
))
196 if (((vme_base
+ size
) > VME_A24_MAX
) ||
197 (vme_base
> VME_A24_MAX
))
201 if (((vme_base
+ size
) > VME_A32_MAX
) ||
202 (vme_base
> VME_A32_MAX
))
207 * Any value held in an unsigned long long can be used as the
212 if (((vme_base
+ size
) > VME_CRCSR_MAX
) ||
213 (vme_base
> VME_CRCSR_MAX
))
223 printk(KERN_ERR
"Invalid address space\n");
232 * Request a slave image with specific attributes, return some unique
235 struct vme_resource
*vme_slave_request(struct vme_dev
*vdev
,
236 vme_address_t address
, vme_cycle_t cycle
)
238 struct vme_bridge
*bridge
;
239 struct list_head
*slave_pos
= NULL
;
240 struct vme_slave_resource
*allocated_image
= NULL
;
241 struct vme_slave_resource
*slave_image
= NULL
;
242 struct vme_resource
*resource
= NULL
;
244 bridge
= vdev
->bridge
;
245 if (bridge
== NULL
) {
246 printk(KERN_ERR
"Can't find VME bus\n");
250 /* Loop through slave resources */
251 list_for_each(slave_pos
, &bridge
->slave_resources
) {
252 slave_image
= list_entry(slave_pos
,
253 struct vme_slave_resource
, list
);
255 if (slave_image
== NULL
) {
256 printk(KERN_ERR
"Registered NULL Slave resource\n");
260 /* Find an unlocked and compatible image */
261 mutex_lock(&slave_image
->mtx
);
262 if (((slave_image
->address_attr
& address
) == address
) &&
263 ((slave_image
->cycle_attr
& cycle
) == cycle
) &&
264 (slave_image
->locked
== 0)) {
266 slave_image
->locked
= 1;
267 mutex_unlock(&slave_image
->mtx
);
268 allocated_image
= slave_image
;
271 mutex_unlock(&slave_image
->mtx
);
275 if (allocated_image
== NULL
)
278 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
279 if (resource
== NULL
) {
280 printk(KERN_WARNING
"Unable to allocate resource structure\n");
283 resource
->type
= VME_SLAVE
;
284 resource
->entry
= &allocated_image
->list
;
290 mutex_lock(&slave_image
->mtx
);
291 slave_image
->locked
= 0;
292 mutex_unlock(&slave_image
->mtx
);
297 EXPORT_SYMBOL(vme_slave_request
);
299 int vme_slave_set(struct vme_resource
*resource
, int enabled
,
300 unsigned long long vme_base
, unsigned long long size
,
301 dma_addr_t buf_base
, vme_address_t aspace
, vme_cycle_t cycle
)
303 struct vme_bridge
*bridge
= find_bridge(resource
);
304 struct vme_slave_resource
*image
;
307 if (resource
->type
!= VME_SLAVE
) {
308 printk(KERN_ERR
"Not a slave resource\n");
312 image
= list_entry(resource
->entry
, struct vme_slave_resource
, list
);
314 if (bridge
->slave_set
== NULL
) {
315 printk(KERN_ERR
"Function not supported\n");
319 if (!(((image
->address_attr
& aspace
) == aspace
) &&
320 ((image
->cycle_attr
& cycle
) == cycle
))) {
321 printk(KERN_ERR
"Invalid attributes\n");
325 retval
= vme_check_window(aspace
, vme_base
, size
);
329 return bridge
->slave_set(image
, enabled
, vme_base
, size
, buf_base
,
332 EXPORT_SYMBOL(vme_slave_set
);
334 int vme_slave_get(struct vme_resource
*resource
, int *enabled
,
335 unsigned long long *vme_base
, unsigned long long *size
,
336 dma_addr_t
*buf_base
, vme_address_t
*aspace
, vme_cycle_t
*cycle
)
338 struct vme_bridge
*bridge
= find_bridge(resource
);
339 struct vme_slave_resource
*image
;
341 if (resource
->type
!= VME_SLAVE
) {
342 printk(KERN_ERR
"Not a slave resource\n");
346 image
= list_entry(resource
->entry
, struct vme_slave_resource
, list
);
348 if (bridge
->slave_get
== NULL
) {
349 printk(KERN_ERR
"vme_slave_get not supported\n");
353 return bridge
->slave_get(image
, enabled
, vme_base
, size
, buf_base
,
356 EXPORT_SYMBOL(vme_slave_get
);
358 void vme_slave_free(struct vme_resource
*resource
)
360 struct vme_slave_resource
*slave_image
;
362 if (resource
->type
!= VME_SLAVE
) {
363 printk(KERN_ERR
"Not a slave resource\n");
367 slave_image
= list_entry(resource
->entry
, struct vme_slave_resource
,
369 if (slave_image
== NULL
) {
370 printk(KERN_ERR
"Can't find slave resource\n");
375 mutex_lock(&slave_image
->mtx
);
376 if (slave_image
->locked
== 0)
377 printk(KERN_ERR
"Image is already free\n");
379 slave_image
->locked
= 0;
380 mutex_unlock(&slave_image
->mtx
);
382 /* Free up resource memory */
385 EXPORT_SYMBOL(vme_slave_free
);
388 * Request a master image with specific attributes, return some unique
391 struct vme_resource
*vme_master_request(struct vme_dev
*vdev
,
392 vme_address_t address
, vme_cycle_t cycle
, vme_width_t dwidth
)
394 struct vme_bridge
*bridge
;
395 struct list_head
*master_pos
= NULL
;
396 struct vme_master_resource
*allocated_image
= NULL
;
397 struct vme_master_resource
*master_image
= NULL
;
398 struct vme_resource
*resource
= NULL
;
400 bridge
= vdev
->bridge
;
401 if (bridge
== NULL
) {
402 printk(KERN_ERR
"Can't find VME bus\n");
406 /* Loop through master resources */
407 list_for_each(master_pos
, &bridge
->master_resources
) {
408 master_image
= list_entry(master_pos
,
409 struct vme_master_resource
, list
);
411 if (master_image
== NULL
) {
412 printk(KERN_WARNING
"Registered NULL master resource\n");
416 /* Find an unlocked and compatible image */
417 spin_lock(&master_image
->lock
);
418 if (((master_image
->address_attr
& address
) == address
) &&
419 ((master_image
->cycle_attr
& cycle
) == cycle
) &&
420 ((master_image
->width_attr
& dwidth
) == dwidth
) &&
421 (master_image
->locked
== 0)) {
423 master_image
->locked
= 1;
424 spin_unlock(&master_image
->lock
);
425 allocated_image
= master_image
;
428 spin_unlock(&master_image
->lock
);
431 /* Check to see if we found a resource */
432 if (allocated_image
== NULL
) {
433 printk(KERN_ERR
"Can't find a suitable resource\n");
437 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
438 if (resource
== NULL
) {
439 printk(KERN_ERR
"Unable to allocate resource structure\n");
442 resource
->type
= VME_MASTER
;
443 resource
->entry
= &allocated_image
->list
;
449 spin_lock(&master_image
->lock
);
450 master_image
->locked
= 0;
451 spin_unlock(&master_image
->lock
);
456 EXPORT_SYMBOL(vme_master_request
);
458 int vme_master_set(struct vme_resource
*resource
, int enabled
,
459 unsigned long long vme_base
, unsigned long long size
,
460 vme_address_t aspace
, vme_cycle_t cycle
, vme_width_t dwidth
)
462 struct vme_bridge
*bridge
= find_bridge(resource
);
463 struct vme_master_resource
*image
;
466 if (resource
->type
!= VME_MASTER
) {
467 printk(KERN_ERR
"Not a master resource\n");
471 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
473 if (bridge
->master_set
== NULL
) {
474 printk(KERN_WARNING
"vme_master_set not supported\n");
478 if (!(((image
->address_attr
& aspace
) == aspace
) &&
479 ((image
->cycle_attr
& cycle
) == cycle
) &&
480 ((image
->width_attr
& dwidth
) == dwidth
))) {
481 printk(KERN_WARNING
"Invalid attributes\n");
485 retval
= vme_check_window(aspace
, vme_base
, size
);
489 return bridge
->master_set(image
, enabled
, vme_base
, size
, aspace
,
492 EXPORT_SYMBOL(vme_master_set
);
494 int vme_master_get(struct vme_resource
*resource
, int *enabled
,
495 unsigned long long *vme_base
, unsigned long long *size
,
496 vme_address_t
*aspace
, vme_cycle_t
*cycle
, vme_width_t
*dwidth
)
498 struct vme_bridge
*bridge
= find_bridge(resource
);
499 struct vme_master_resource
*image
;
501 if (resource
->type
!= VME_MASTER
) {
502 printk(KERN_ERR
"Not a master resource\n");
506 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
508 if (bridge
->master_get
== NULL
) {
509 printk(KERN_WARNING
"vme_master_set not supported\n");
513 return bridge
->master_get(image
, enabled
, vme_base
, size
, aspace
,
516 EXPORT_SYMBOL(vme_master_get
);
519 * Read data out of VME space into a buffer.
521 ssize_t
vme_master_read(struct vme_resource
*resource
, void *buf
, size_t count
,
524 struct vme_bridge
*bridge
= find_bridge(resource
);
525 struct vme_master_resource
*image
;
528 if (bridge
->master_read
== NULL
) {
529 printk(KERN_WARNING
"Reading from resource not supported\n");
533 if (resource
->type
!= VME_MASTER
) {
534 printk(KERN_ERR
"Not a master resource\n");
538 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
540 length
= vme_get_size(resource
);
542 if (offset
> length
) {
543 printk(KERN_WARNING
"Invalid Offset\n");
547 if ((offset
+ count
) > length
)
548 count
= length
- offset
;
550 return bridge
->master_read(image
, buf
, count
, offset
);
553 EXPORT_SYMBOL(vme_master_read
);
556 * Write data out to VME space from a buffer.
558 ssize_t
vme_master_write(struct vme_resource
*resource
, void *buf
,
559 size_t count
, loff_t offset
)
561 struct vme_bridge
*bridge
= find_bridge(resource
);
562 struct vme_master_resource
*image
;
565 if (bridge
->master_write
== NULL
) {
566 printk(KERN_WARNING
"Writing to resource not supported\n");
570 if (resource
->type
!= VME_MASTER
) {
571 printk(KERN_ERR
"Not a master resource\n");
575 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
577 length
= vme_get_size(resource
);
579 if (offset
> length
) {
580 printk(KERN_WARNING
"Invalid Offset\n");
584 if ((offset
+ count
) > length
)
585 count
= length
- offset
;
587 return bridge
->master_write(image
, buf
, count
, offset
);
589 EXPORT_SYMBOL(vme_master_write
);
592 * Perform RMW cycle to provided location.
594 unsigned int vme_master_rmw(struct vme_resource
*resource
, unsigned int mask
,
595 unsigned int compare
, unsigned int swap
, loff_t offset
)
597 struct vme_bridge
*bridge
= find_bridge(resource
);
598 struct vme_master_resource
*image
;
600 if (bridge
->master_rmw
== NULL
) {
601 printk(KERN_WARNING
"Writing to resource not supported\n");
605 if (resource
->type
!= VME_MASTER
) {
606 printk(KERN_ERR
"Not a master resource\n");
610 image
= list_entry(resource
->entry
, struct vme_master_resource
, list
);
612 return bridge
->master_rmw(image
, mask
, compare
, swap
, offset
);
614 EXPORT_SYMBOL(vme_master_rmw
);
616 void vme_master_free(struct vme_resource
*resource
)
618 struct vme_master_resource
*master_image
;
620 if (resource
->type
!= VME_MASTER
) {
621 printk(KERN_ERR
"Not a master resource\n");
625 master_image
= list_entry(resource
->entry
, struct vme_master_resource
,
627 if (master_image
== NULL
) {
628 printk(KERN_ERR
"Can't find master resource\n");
633 spin_lock(&master_image
->lock
);
634 if (master_image
->locked
== 0)
635 printk(KERN_ERR
"Image is already free\n");
637 master_image
->locked
= 0;
638 spin_unlock(&master_image
->lock
);
640 /* Free up resource memory */
643 EXPORT_SYMBOL(vme_master_free
);
646 * Request a DMA controller with specific attributes, return some unique
649 struct vme_resource
*vme_dma_request(struct vme_dev
*vdev
,
650 vme_dma_route_t route
)
652 struct vme_bridge
*bridge
;
653 struct list_head
*dma_pos
= NULL
;
654 struct vme_dma_resource
*allocated_ctrlr
= NULL
;
655 struct vme_dma_resource
*dma_ctrlr
= NULL
;
656 struct vme_resource
*resource
= NULL
;
658 /* XXX Not checking resource attributes */
659 printk(KERN_ERR
"No VME resource Attribute tests done\n");
661 bridge
= vdev
->bridge
;
662 if (bridge
== NULL
) {
663 printk(KERN_ERR
"Can't find VME bus\n");
667 /* Loop through DMA resources */
668 list_for_each(dma_pos
, &bridge
->dma_resources
) {
669 dma_ctrlr
= list_entry(dma_pos
,
670 struct vme_dma_resource
, list
);
672 if (dma_ctrlr
== NULL
) {
673 printk(KERN_ERR
"Registered NULL DMA resource\n");
677 /* Find an unlocked and compatible controller */
678 mutex_lock(&dma_ctrlr
->mtx
);
679 if (((dma_ctrlr
->route_attr
& route
) == route
) &&
680 (dma_ctrlr
->locked
== 0)) {
682 dma_ctrlr
->locked
= 1;
683 mutex_unlock(&dma_ctrlr
->mtx
);
684 allocated_ctrlr
= dma_ctrlr
;
687 mutex_unlock(&dma_ctrlr
->mtx
);
690 /* Check to see if we found a resource */
691 if (allocated_ctrlr
== NULL
)
694 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
695 if (resource
== NULL
) {
696 printk(KERN_WARNING
"Unable to allocate resource structure\n");
699 resource
->type
= VME_DMA
;
700 resource
->entry
= &allocated_ctrlr
->list
;
706 mutex_lock(&dma_ctrlr
->mtx
);
707 dma_ctrlr
->locked
= 0;
708 mutex_unlock(&dma_ctrlr
->mtx
);
713 EXPORT_SYMBOL(vme_dma_request
);
718 struct vme_dma_list
*vme_new_dma_list(struct vme_resource
*resource
)
720 struct vme_dma_resource
*ctrlr
;
721 struct vme_dma_list
*dma_list
;
723 if (resource
->type
!= VME_DMA
) {
724 printk(KERN_ERR
"Not a DMA resource\n");
728 ctrlr
= list_entry(resource
->entry
, struct vme_dma_resource
, list
);
730 dma_list
= kmalloc(sizeof(struct vme_dma_list
), GFP_KERNEL
);
731 if (dma_list
== NULL
) {
732 printk(KERN_ERR
"Unable to allocate memory for new dma list\n");
735 INIT_LIST_HEAD(&dma_list
->entries
);
736 dma_list
->parent
= ctrlr
;
737 mutex_init(&dma_list
->mtx
);
741 EXPORT_SYMBOL(vme_new_dma_list
);
744 * Create "Pattern" type attributes
746 struct vme_dma_attr
*vme_dma_pattern_attribute(u32 pattern
,
749 struct vme_dma_attr
*attributes
;
750 struct vme_dma_pattern
*pattern_attr
;
752 attributes
= kmalloc(sizeof(struct vme_dma_attr
), GFP_KERNEL
);
753 if (attributes
== NULL
) {
754 printk(KERN_ERR
"Unable to allocate memory for attributes "
759 pattern_attr
= kmalloc(sizeof(struct vme_dma_pattern
), GFP_KERNEL
);
760 if (pattern_attr
== NULL
) {
761 printk(KERN_ERR
"Unable to allocate memory for pattern "
766 attributes
->type
= VME_DMA_PATTERN
;
767 attributes
->private = (void *)pattern_attr
;
769 pattern_attr
->pattern
= pattern
;
770 pattern_attr
->type
= type
;
779 EXPORT_SYMBOL(vme_dma_pattern_attribute
);
782 * Create "PCI" type attributes
784 struct vme_dma_attr
*vme_dma_pci_attribute(dma_addr_t address
)
786 struct vme_dma_attr
*attributes
;
787 struct vme_dma_pci
*pci_attr
;
789 /* XXX Run some sanity checks here */
791 attributes
= kmalloc(sizeof(struct vme_dma_attr
), GFP_KERNEL
);
792 if (attributes
== NULL
) {
793 printk(KERN_ERR
"Unable to allocate memory for attributes "
798 pci_attr
= kmalloc(sizeof(struct vme_dma_pci
), GFP_KERNEL
);
799 if (pci_attr
== NULL
) {
800 printk(KERN_ERR
"Unable to allocate memory for pci "
807 attributes
->type
= VME_DMA_PCI
;
808 attributes
->private = (void *)pci_attr
;
810 pci_attr
->address
= address
;
819 EXPORT_SYMBOL(vme_dma_pci_attribute
);
822 * Create "VME" type attributes
824 struct vme_dma_attr
*vme_dma_vme_attribute(unsigned long long address
,
825 vme_address_t aspace
, vme_cycle_t cycle
, vme_width_t dwidth
)
827 struct vme_dma_attr
*attributes
;
828 struct vme_dma_vme
*vme_attr
;
830 attributes
= kmalloc(
831 sizeof(struct vme_dma_attr
), GFP_KERNEL
);
832 if (attributes
== NULL
) {
833 printk(KERN_ERR
"Unable to allocate memory for attributes "
838 vme_attr
= kmalloc(sizeof(struct vme_dma_vme
), GFP_KERNEL
);
839 if (vme_attr
== NULL
) {
840 printk(KERN_ERR
"Unable to allocate memory for vme "
845 attributes
->type
= VME_DMA_VME
;
846 attributes
->private = (void *)vme_attr
;
848 vme_attr
->address
= address
;
849 vme_attr
->aspace
= aspace
;
850 vme_attr
->cycle
= cycle
;
851 vme_attr
->dwidth
= dwidth
;
860 EXPORT_SYMBOL(vme_dma_vme_attribute
);
865 void vme_dma_free_attribute(struct vme_dma_attr
*attributes
)
867 kfree(attributes
->private);
870 EXPORT_SYMBOL(vme_dma_free_attribute
);
872 int vme_dma_list_add(struct vme_dma_list
*list
, struct vme_dma_attr
*src
,
873 struct vme_dma_attr
*dest
, size_t count
)
875 struct vme_bridge
*bridge
= list
->parent
->parent
;
878 if (bridge
->dma_list_add
== NULL
) {
879 printk(KERN_WARNING
"Link List DMA generation not supported\n");
883 if (!mutex_trylock(&list
->mtx
)) {
884 printk(KERN_ERR
"Link List already submitted\n");
888 retval
= bridge
->dma_list_add(list
, src
, dest
, count
);
890 mutex_unlock(&list
->mtx
);
894 EXPORT_SYMBOL(vme_dma_list_add
);
896 int vme_dma_list_exec(struct vme_dma_list
*list
)
898 struct vme_bridge
*bridge
= list
->parent
->parent
;
901 if (bridge
->dma_list_exec
== NULL
) {
902 printk(KERN_ERR
"Link List DMA execution not supported\n");
906 mutex_lock(&list
->mtx
);
908 retval
= bridge
->dma_list_exec(list
);
910 mutex_unlock(&list
->mtx
);
914 EXPORT_SYMBOL(vme_dma_list_exec
);
916 int vme_dma_list_free(struct vme_dma_list
*list
)
918 struct vme_bridge
*bridge
= list
->parent
->parent
;
921 if (bridge
->dma_list_empty
== NULL
) {
922 printk(KERN_WARNING
"Emptying of Link Lists not supported\n");
926 if (!mutex_trylock(&list
->mtx
)) {
927 printk(KERN_ERR
"Link List in use\n");
932 * Empty out all of the entries from the dma list. We need to go to the
933 * low level driver as dma entries are driver specific.
935 retval
= bridge
->dma_list_empty(list
);
937 printk(KERN_ERR
"Unable to empty link-list entries\n");
938 mutex_unlock(&list
->mtx
);
941 mutex_unlock(&list
->mtx
);
946 EXPORT_SYMBOL(vme_dma_list_free
);
948 int vme_dma_free(struct vme_resource
*resource
)
950 struct vme_dma_resource
*ctrlr
;
952 if (resource
->type
!= VME_DMA
) {
953 printk(KERN_ERR
"Not a DMA resource\n");
957 ctrlr
= list_entry(resource
->entry
, struct vme_dma_resource
, list
);
959 if (!mutex_trylock(&ctrlr
->mtx
)) {
960 printk(KERN_ERR
"Resource busy, can't free\n");
964 if (!(list_empty(&ctrlr
->pending
) && list_empty(&ctrlr
->running
))) {
965 printk(KERN_WARNING
"Resource still processing transfers\n");
966 mutex_unlock(&ctrlr
->mtx
);
972 mutex_unlock(&ctrlr
->mtx
);
976 EXPORT_SYMBOL(vme_dma_free
);
978 void vme_irq_handler(struct vme_bridge
*bridge
, int level
, int statid
)
980 void (*call
)(int, int, void *);
983 call
= bridge
->irq
[level
- 1].callback
[statid
].func
;
984 priv_data
= bridge
->irq
[level
- 1].callback
[statid
].priv_data
;
987 call(level
, statid
, priv_data
);
989 printk(KERN_WARNING
"Spurilous VME interrupt, level:%x, "
990 "vector:%x\n", level
, statid
);
992 EXPORT_SYMBOL(vme_irq_handler
);
994 int vme_irq_request(struct vme_dev
*vdev
, int level
, int statid
,
995 void (*callback
)(int, int, void *),
998 struct vme_bridge
*bridge
;
1000 bridge
= vdev
->bridge
;
1001 if (bridge
== NULL
) {
1002 printk(KERN_ERR
"Can't find VME bus\n");
1006 if ((level
< 1) || (level
> 7)) {
1007 printk(KERN_ERR
"Invalid interrupt level\n");
1011 if (bridge
->irq_set
== NULL
) {
1012 printk(KERN_ERR
"Configuring interrupts not supported\n");
1016 mutex_lock(&bridge
->irq_mtx
);
1018 if (bridge
->irq
[level
- 1].callback
[statid
].func
) {
1019 mutex_unlock(&bridge
->irq_mtx
);
1020 printk(KERN_WARNING
"VME Interrupt already taken\n");
1024 bridge
->irq
[level
- 1].count
++;
1025 bridge
->irq
[level
- 1].callback
[statid
].priv_data
= priv_data
;
1026 bridge
->irq
[level
- 1].callback
[statid
].func
= callback
;
1028 /* Enable IRQ level */
1029 bridge
->irq_set(bridge
, level
, 1, 1);
1031 mutex_unlock(&bridge
->irq_mtx
);
1035 EXPORT_SYMBOL(vme_irq_request
);
1037 void vme_irq_free(struct vme_dev
*vdev
, int level
, int statid
)
1039 struct vme_bridge
*bridge
;
1041 bridge
= vdev
->bridge
;
1042 if (bridge
== NULL
) {
1043 printk(KERN_ERR
"Can't find VME bus\n");
1047 if ((level
< 1) || (level
> 7)) {
1048 printk(KERN_ERR
"Invalid interrupt level\n");
1052 if (bridge
->irq_set
== NULL
) {
1053 printk(KERN_ERR
"Configuring interrupts not supported\n");
1057 mutex_lock(&bridge
->irq_mtx
);
1059 bridge
->irq
[level
- 1].count
--;
1061 /* Disable IRQ level if no more interrupts attached at this level*/
1062 if (bridge
->irq
[level
- 1].count
== 0)
1063 bridge
->irq_set(bridge
, level
, 0, 1);
1065 bridge
->irq
[level
- 1].callback
[statid
].func
= NULL
;
1066 bridge
->irq
[level
- 1].callback
[statid
].priv_data
= NULL
;
1068 mutex_unlock(&bridge
->irq_mtx
);
1070 EXPORT_SYMBOL(vme_irq_free
);
1072 int vme_irq_generate(struct vme_dev
*vdev
, int level
, int statid
)
1074 struct vme_bridge
*bridge
;
1076 bridge
= vdev
->bridge
;
1077 if (bridge
== NULL
) {
1078 printk(KERN_ERR
"Can't find VME bus\n");
1082 if ((level
< 1) || (level
> 7)) {
1083 printk(KERN_WARNING
"Invalid interrupt level\n");
1087 if (bridge
->irq_generate
== NULL
) {
1088 printk(KERN_WARNING
"Interrupt generation not supported\n");
1092 return bridge
->irq_generate(bridge
, level
, statid
);
1094 EXPORT_SYMBOL(vme_irq_generate
);
1097 * Request the location monitor, return resource or NULL
1099 struct vme_resource
*vme_lm_request(struct vme_dev
*vdev
)
1101 struct vme_bridge
*bridge
;
1102 struct list_head
*lm_pos
= NULL
;
1103 struct vme_lm_resource
*allocated_lm
= NULL
;
1104 struct vme_lm_resource
*lm
= NULL
;
1105 struct vme_resource
*resource
= NULL
;
1107 bridge
= vdev
->bridge
;
1108 if (bridge
== NULL
) {
1109 printk(KERN_ERR
"Can't find VME bus\n");
1113 /* Loop through DMA resources */
1114 list_for_each(lm_pos
, &bridge
->lm_resources
) {
1115 lm
= list_entry(lm_pos
,
1116 struct vme_lm_resource
, list
);
1119 printk(KERN_ERR
"Registered NULL Location Monitor "
1124 /* Find an unlocked controller */
1125 mutex_lock(&lm
->mtx
);
1126 if (lm
->locked
== 0) {
1128 mutex_unlock(&lm
->mtx
);
1132 mutex_unlock(&lm
->mtx
);
1135 /* Check to see if we found a resource */
1136 if (allocated_lm
== NULL
)
1139 resource
= kmalloc(sizeof(struct vme_resource
), GFP_KERNEL
);
1140 if (resource
== NULL
) {
1141 printk(KERN_ERR
"Unable to allocate resource structure\n");
1144 resource
->type
= VME_LM
;
1145 resource
->entry
= &allocated_lm
->list
;
1151 mutex_lock(&lm
->mtx
);
1153 mutex_unlock(&lm
->mtx
);
1158 EXPORT_SYMBOL(vme_lm_request
);
1160 int vme_lm_count(struct vme_resource
*resource
)
1162 struct vme_lm_resource
*lm
;
1164 if (resource
->type
!= VME_LM
) {
1165 printk(KERN_ERR
"Not a Location Monitor resource\n");
1169 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1171 return lm
->monitors
;
1173 EXPORT_SYMBOL(vme_lm_count
);
1175 int vme_lm_set(struct vme_resource
*resource
, unsigned long long lm_base
,
1176 vme_address_t aspace
, vme_cycle_t cycle
)
1178 struct vme_bridge
*bridge
= find_bridge(resource
);
1179 struct vme_lm_resource
*lm
;
1181 if (resource
->type
!= VME_LM
) {
1182 printk(KERN_ERR
"Not a Location Monitor resource\n");
1186 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1188 if (bridge
->lm_set
== NULL
) {
1189 printk(KERN_ERR
"vme_lm_set not supported\n");
1193 return bridge
->lm_set(lm
, lm_base
, aspace
, cycle
);
1195 EXPORT_SYMBOL(vme_lm_set
);
1197 int vme_lm_get(struct vme_resource
*resource
, unsigned long long *lm_base
,
1198 vme_address_t
*aspace
, vme_cycle_t
*cycle
)
1200 struct vme_bridge
*bridge
= find_bridge(resource
);
1201 struct vme_lm_resource
*lm
;
1203 if (resource
->type
!= VME_LM
) {
1204 printk(KERN_ERR
"Not a Location Monitor resource\n");
1208 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1210 if (bridge
->lm_get
== NULL
) {
1211 printk(KERN_ERR
"vme_lm_get not supported\n");
1215 return bridge
->lm_get(lm
, lm_base
, aspace
, cycle
);
1217 EXPORT_SYMBOL(vme_lm_get
);
1219 int vme_lm_attach(struct vme_resource
*resource
, int monitor
,
1220 void (*callback
)(int))
1222 struct vme_bridge
*bridge
= find_bridge(resource
);
1223 struct vme_lm_resource
*lm
;
1225 if (resource
->type
!= VME_LM
) {
1226 printk(KERN_ERR
"Not a Location Monitor resource\n");
1230 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1232 if (bridge
->lm_attach
== NULL
) {
1233 printk(KERN_ERR
"vme_lm_attach not supported\n");
1237 return bridge
->lm_attach(lm
, monitor
, callback
);
1239 EXPORT_SYMBOL(vme_lm_attach
);
1241 int vme_lm_detach(struct vme_resource
*resource
, int monitor
)
1243 struct vme_bridge
*bridge
= find_bridge(resource
);
1244 struct vme_lm_resource
*lm
;
1246 if (resource
->type
!= VME_LM
) {
1247 printk(KERN_ERR
"Not a Location Monitor resource\n");
1251 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1253 if (bridge
->lm_detach
== NULL
) {
1254 printk(KERN_ERR
"vme_lm_detach not supported\n");
1258 return bridge
->lm_detach(lm
, monitor
);
1260 EXPORT_SYMBOL(vme_lm_detach
);
1262 void vme_lm_free(struct vme_resource
*resource
)
1264 struct vme_lm_resource
*lm
;
1266 if (resource
->type
!= VME_LM
) {
1267 printk(KERN_ERR
"Not a Location Monitor resource\n");
1271 lm
= list_entry(resource
->entry
, struct vme_lm_resource
, list
);
1273 mutex_lock(&lm
->mtx
);
1276 * Check to see that there aren't any callbacks still attached, if
1277 * there are we should probably be detaching them!
1282 mutex_unlock(&lm
->mtx
);
1286 EXPORT_SYMBOL(vme_lm_free
);
1288 int vme_slot_get(struct vme_dev
*vdev
)
1290 struct vme_bridge
*bridge
;
1292 bridge
= vdev
->bridge
;
1293 if (bridge
== NULL
) {
1294 printk(KERN_ERR
"Can't find VME bus\n");
1298 if (bridge
->slot_get
== NULL
) {
1299 printk(KERN_WARNING
"vme_slot_get not supported\n");
1303 return bridge
->slot_get(bridge
);
1305 EXPORT_SYMBOL(vme_slot_get
);
1308 /* - Bridge Registration --------------------------------------------------- */
1310 static int vme_add_bus(struct vme_bridge
*bridge
)
1315 mutex_lock(&vme_buses_lock
);
1316 for (i
= 0; i
< sizeof(vme_bus_numbers
) * 8; i
++) {
1317 if ((vme_bus_numbers
& (1 << i
)) == 0) {
1318 vme_bus_numbers
|= (1 << i
);
1320 INIT_LIST_HEAD(&bridge
->devices
);
1321 list_add_tail(&bridge
->bus_list
, &vme_bus_list
);
1326 mutex_unlock(&vme_buses_lock
);
1331 static void vme_remove_bus(struct vme_bridge
*bridge
)
1333 struct vme_dev
*vdev
;
1334 struct vme_dev
*tmp
;
1336 mutex_lock(&vme_buses_lock
);
1337 vme_bus_numbers
&= ~(1 << bridge
->num
);
1338 list_for_each_entry_safe(vdev
, tmp
, &bridge
->devices
, bridge_list
) {
1339 list_del(&vdev
->drv_list
);
1340 list_del(&vdev
->bridge_list
);
1341 device_unregister(&vdev
->dev
);
1343 list_del(&bridge
->bus_list
);
1344 mutex_unlock(&vme_buses_lock
);
1347 static void vme_dev_release(struct device
*dev
)
1349 kfree(dev_to_vme_dev(dev
));
1352 int vme_register_bridge(struct vme_bridge
*bridge
)
1354 return vme_add_bus(bridge
);
1356 EXPORT_SYMBOL(vme_register_bridge
);
1358 void vme_unregister_bridge(struct vme_bridge
*bridge
)
1360 vme_remove_bus(bridge
);
1362 EXPORT_SYMBOL(vme_unregister_bridge
);
1364 /* - Driver Registration --------------------------------------------------- */
1366 static int __vme_register_driver_bus(struct vme_driver
*drv
,
1367 struct vme_bridge
*bridge
, unsigned int ndevs
)
1371 struct vme_dev
*vdev
;
1372 struct vme_dev
*tmp
;
1374 for (i
= 0; i
< ndevs
; i
++) {
1375 vdev
= kzalloc(sizeof(struct vme_dev
), GFP_KERNEL
);
1381 vdev
->id
.bus
= bridge
->num
;
1382 vdev
->id
.slot
= i
+ 1;
1383 vdev
->bridge
= bridge
;
1384 vdev
->dev
.platform_data
= drv
;
1385 vdev
->dev
.release
= vme_dev_release
;
1386 vdev
->dev
.parent
= bridge
->parent
;
1387 vdev
->dev
.bus
= &vme_bus_type
;
1388 dev_set_name(&vdev
->dev
, "%s.%u-%u", drv
->name
, vdev
->id
.bus
,
1391 err
= device_register(&vdev
->dev
);
1395 if (vdev
->dev
.platform_data
) {
1396 list_add_tail(&vdev
->drv_list
, &drv
->devices
);
1397 list_add_tail(&vdev
->bridge_list
, &bridge
->devices
);
1399 device_unregister(&vdev
->dev
);
1406 list_for_each_entry_safe(vdev
, tmp
, &drv
->devices
, drv_list
) {
1407 list_del(&vdev
->drv_list
);
1408 list_del(&vdev
->bridge_list
);
1409 device_unregister(&vdev
->dev
);
1414 static int __vme_register_driver(struct vme_driver
*drv
, unsigned int ndevs
)
1416 struct vme_bridge
*bridge
;
1419 mutex_lock(&vme_buses_lock
);
1420 list_for_each_entry(bridge
, &vme_bus_list
, bus_list
) {
1422 * This cannot cause trouble as we already have vme_buses_lock
1423 * and if the bridge is removed, it will have to go through
1424 * vme_unregister_bridge() to do it (which calls remove() on
1425 * the bridge which in turn tries to acquire vme_buses_lock and
1426 * will have to wait). The probe() called after device
1427 * registration in __vme_register_driver below will also fail
1428 * as the bridge is being removed (since the probe() calls
1429 * vme_bridge_get()).
1431 err
= __vme_register_driver_bus(drv
, bridge
, ndevs
);
1435 mutex_unlock(&vme_buses_lock
);
1439 int vme_register_driver(struct vme_driver
*drv
, unsigned int ndevs
)
1443 drv
->driver
.name
= drv
->name
;
1444 drv
->driver
.bus
= &vme_bus_type
;
1445 INIT_LIST_HEAD(&drv
->devices
);
1447 err
= driver_register(&drv
->driver
);
1451 err
= __vme_register_driver(drv
, ndevs
);
1453 driver_unregister(&drv
->driver
);
1457 EXPORT_SYMBOL(vme_register_driver
);
1459 void vme_unregister_driver(struct vme_driver
*drv
)
1461 struct vme_dev
*dev
, *dev_tmp
;
1463 mutex_lock(&vme_buses_lock
);
1464 list_for_each_entry_safe(dev
, dev_tmp
, &drv
->devices
, drv_list
) {
1465 list_del(&dev
->drv_list
);
1466 list_del(&dev
->bridge_list
);
1467 device_unregister(&dev
->dev
);
1469 mutex_unlock(&vme_buses_lock
);
1471 driver_unregister(&drv
->driver
);
1473 EXPORT_SYMBOL(vme_unregister_driver
);
1475 /* - Bus Registration ------------------------------------------------------ */
1477 static int vme_bus_match(struct device
*dev
, struct device_driver
*drv
)
1479 struct vme_driver
*vme_drv
;
1481 vme_drv
= container_of(drv
, struct vme_driver
, driver
);
1483 if (dev
->platform_data
== vme_drv
) {
1484 struct vme_dev
*vdev
= dev_to_vme_dev(dev
);
1486 if (vme_drv
->match
&& vme_drv
->match(vdev
))
1489 dev
->platform_data
= NULL
;
1494 static int vme_bus_probe(struct device
*dev
)
1496 int retval
= -ENODEV
;
1497 struct vme_driver
*driver
;
1498 struct vme_dev
*vdev
= dev_to_vme_dev(dev
);
1500 driver
= dev
->platform_data
;
1502 if (driver
->probe
!= NULL
)
1503 retval
= driver
->probe(vdev
);
1508 static int vme_bus_remove(struct device
*dev
)
1510 int retval
= -ENODEV
;
1511 struct vme_driver
*driver
;
1512 struct vme_dev
*vdev
= dev_to_vme_dev(dev
);
1514 driver
= dev
->platform_data
;
1516 if (driver
->remove
!= NULL
)
1517 retval
= driver
->remove(vdev
);
1522 struct bus_type vme_bus_type
= {
1524 .match
= vme_bus_match
,
1525 .probe
= vme_bus_probe
,
1526 .remove
= vme_bus_remove
,
1528 EXPORT_SYMBOL(vme_bus_type
);
1530 static int __init
vme_init(void)
1532 return bus_register(&vme_bus_type
);
1535 static void __exit
vme_exit(void)
1537 bus_unregister(&vme_bus_type
);
1540 MODULE_DESCRIPTION("VME bridge driver framework");
1541 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
1542 MODULE_LICENSE("GPL");
1544 module_init(vme_init
);
1545 module_exit(vme_exit
);