4 * Author: Martyn Welch <martyn.welch@gefanuc.com>
5 * Copyright 2008 GE Fanuc 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/version.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/pci.h>
24 #include <linux/poll.h>
25 #include <linux/highmem.h>
26 #include <linux/interrupt.h>
27 #include <linux/pagemap.h>
28 #include <linux/device.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/syscalls.h>
31 #include <linux/semaphore.h>
32 #include <linux/spinlock.h>
35 #include "vme_bridge.h"
37 /* Bitmask and semaphore to keep track of bridge numbers */
38 static unsigned int vme_bus_numbers;
39 DECLARE_MUTEX(vme_bus_num_sem);
41 static void __exit vme_exit (void);
42 static int __init vme_init (void);
46 * Find the bridge resource associated with a specific device resource
48 static struct vme_bridge *dev_to_bridge(struct device *dev)
50 return dev->platform_data;
54 * Find the bridge that the resource is associated with.
56 static struct vme_bridge *find_bridge(struct vme_resource *resource)
58 /* Get list to search */
59 switch (resource->type) {
61 return list_entry(resource->entry, struct vme_master_resource,
65 return list_entry(resource->entry, struct vme_slave_resource,
69 return list_entry(resource->entry, struct vme_dma_resource,
73 printk(KERN_ERR "Unknown resource type\n");
80 * Allocate a contiguous block of memory for use by the driver. This is used to
81 * create the buffers for the slave windows.
83 * XXX VME bridges could be available on buses other than PCI. At the momment
84 * this framework only supports PCI devices.
86 void * vme_alloc_consistent(struct vme_resource *resource, size_t size,
89 struct vme_bridge *bridge;
92 if(resource == NULL) {
93 printk("No resource\n");
97 bridge = find_bridge(resource);
99 printk("Can't find bridge\n");
103 /* Find pci_dev container of dev */
104 if (bridge->parent == NULL) {
105 printk("Dev entry NULL\n");
108 pdev = container_of(bridge->parent, struct pci_dev, dev);
110 return pci_alloc_consistent(pdev, size, dma);
112 EXPORT_SYMBOL(vme_alloc_consistent);
115 * Free previously allocated contiguous block of memory.
117 * XXX VME bridges could be available on buses other than PCI. At the momment
118 * this framework only supports PCI devices.
120 void vme_free_consistent(struct vme_resource *resource, size_t size,
121 void *vaddr, dma_addr_t dma)
123 struct vme_bridge *bridge;
124 struct pci_dev *pdev;
126 if(resource == NULL) {
127 printk("No resource\n");
131 bridge = find_bridge(resource);
133 printk("Can't find bridge\n");
137 /* Find pci_dev container of dev */
138 pdev = container_of(bridge->parent, struct pci_dev, dev);
140 pci_free_consistent(pdev, size, vaddr, dma);
142 EXPORT_SYMBOL(vme_free_consistent);
144 size_t vme_get_size(struct vme_resource *resource)
147 unsigned long long base, size;
149 vme_address_t aspace;
153 switch (resource->type) {
155 retval = vme_master_get(resource, &enabled, &base, &size,
156 &aspace, &cycle, &dwidth);
161 retval = vme_slave_get(resource, &enabled, &base, &size,
162 &buf_base, &aspace, &cycle);
170 printk(KERN_ERR "Unknown resource type\n");
175 EXPORT_SYMBOL(vme_get_size);
177 static int vme_check_window(vme_address_t aspace, unsigned long long vme_base,
178 unsigned long long size)
184 if (((vme_base + size) > VME_A16_MAX) ||
185 (vme_base > VME_A16_MAX))
189 if (((vme_base + size) > VME_A24_MAX) ||
190 (vme_base > VME_A24_MAX))
194 if (((vme_base + size) > VME_A32_MAX) ||
195 (vme_base > VME_A32_MAX))
200 * Any value held in an unsigned long long can be used as the
205 if (((vme_base + size) > VME_CRCSR_MAX) ||
206 (vme_base > VME_CRCSR_MAX))
216 printk("Invalid address space\n");
225 * Request a slave image with specific attributes, return some unique
228 struct vme_resource * vme_slave_request(struct device *dev,
229 vme_address_t address, vme_cycle_t cycle)
231 struct vme_bridge *bridge;
232 struct list_head *slave_pos = NULL;
233 struct vme_slave_resource *allocated_image = NULL;
234 struct vme_slave_resource *slave_image = NULL;
235 struct vme_resource *resource = NULL;
237 bridge = dev_to_bridge(dev);
238 if (bridge == NULL) {
239 printk(KERN_ERR "Can't find VME bus\n");
243 /* Loop through slave resources */
244 list_for_each(slave_pos, &(bridge->slave_resources)) {
245 slave_image = list_entry(slave_pos,
246 struct vme_slave_resource, list);
248 if (slave_image == NULL) {
249 printk("Registered NULL Slave resource\n");
253 /* Find an unlocked and compatible image */
254 down(&(slave_image->sem));
255 if(((slave_image->address_attr & address) == address) &&
256 ((slave_image->cycle_attr & cycle) == cycle) &&
257 (slave_image->locked == 0)) {
259 slave_image->locked = 1;
260 up(&(slave_image->sem));
261 allocated_image = slave_image;
264 up(&(slave_image->sem));
268 if (allocated_image == NULL)
271 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
272 if (resource == NULL) {
273 printk(KERN_WARNING "Unable to allocate resource structure\n");
276 resource->type = VME_SLAVE;
277 resource->entry = &(allocated_image->list);
283 down(&(slave_image->sem));
284 slave_image->locked = 0;
285 up(&(slave_image->sem));
290 EXPORT_SYMBOL(vme_slave_request);
292 int vme_slave_set (struct vme_resource *resource, int enabled,
293 unsigned long long vme_base, unsigned long long size,
294 dma_addr_t buf_base, vme_address_t aspace, vme_cycle_t cycle)
296 struct vme_bridge *bridge = find_bridge(resource);
297 struct vme_slave_resource *image;
300 if (resource->type != VME_SLAVE) {
301 printk("Not a slave resource\n");
305 image = list_entry(resource->entry, struct vme_slave_resource, list);
307 if (bridge->slave_set == NULL) {
308 printk("Function not supported\n");
312 if(!(((image->address_attr & aspace) == aspace) &&
313 ((image->cycle_attr & cycle) == cycle))) {
314 printk("Invalid attributes\n");
318 retval = vme_check_window(aspace, vme_base, size);
322 return bridge->slave_set(image, enabled, vme_base, size, buf_base,
325 EXPORT_SYMBOL(vme_slave_set);
327 int vme_slave_get (struct vme_resource *resource, int *enabled,
328 unsigned long long *vme_base, unsigned long long *size,
329 dma_addr_t *buf_base, vme_address_t *aspace, vme_cycle_t *cycle)
331 struct vme_bridge *bridge = find_bridge(resource);
332 struct vme_slave_resource *image;
334 if (resource->type != VME_SLAVE) {
335 printk("Not a slave resource\n");
339 image = list_entry(resource->entry, struct vme_slave_resource, list);
341 if (bridge->slave_set == NULL) {
342 printk("vme_slave_get not supported\n");
346 return bridge->slave_get(image, enabled, vme_base, size, buf_base,
349 EXPORT_SYMBOL(vme_slave_get);
351 void vme_slave_free(struct vme_resource *resource)
353 struct vme_slave_resource *slave_image;
355 if (resource->type != VME_SLAVE) {
356 printk("Not a slave resource\n");
360 slave_image = list_entry(resource->entry, struct vme_slave_resource,
362 if (slave_image == NULL) {
363 printk("Can't find slave resource\n");
368 down(&(slave_image->sem));
369 if (slave_image->locked == 0)
370 printk(KERN_ERR "Image is already free\n");
372 slave_image->locked = 0;
373 up(&(slave_image->sem));
375 /* Free up resource memory */
378 EXPORT_SYMBOL(vme_slave_free);
381 * Request a master image with specific attributes, return some unique
384 struct vme_resource * vme_master_request(struct device *dev,
385 vme_address_t address, vme_cycle_t cycle, vme_width_t dwidth)
387 struct vme_bridge *bridge;
388 struct list_head *master_pos = NULL;
389 struct vme_master_resource *allocated_image = NULL;
390 struct vme_master_resource *master_image = NULL;
391 struct vme_resource *resource = NULL;
393 bridge = dev_to_bridge(dev);
394 if (bridge == NULL) {
395 printk(KERN_ERR "Can't find VME bus\n");
399 /* Loop through master resources */
400 list_for_each(master_pos, &(bridge->master_resources)) {
401 master_image = list_entry(master_pos,
402 struct vme_master_resource, list);
404 if (master_image == NULL) {
405 printk(KERN_WARNING "Registered NULL master resource\n");
409 /* Find an unlocked and compatible image */
410 spin_lock(&(master_image->lock));
411 if(((master_image->address_attr & address) == address) &&
412 ((master_image->cycle_attr & cycle) == cycle) &&
413 ((master_image->width_attr & dwidth) == dwidth) &&
414 (master_image->locked == 0)) {
416 master_image->locked = 1;
417 spin_unlock(&(master_image->lock));
418 allocated_image = master_image;
421 spin_unlock(&(master_image->lock));
424 /* Check to see if we found a resource */
425 if (allocated_image == NULL) {
426 printk(KERN_ERR "Can't find a suitable resource\n");
430 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
431 if (resource == NULL) {
432 printk(KERN_ERR "Unable to allocate resource structure\n");
435 resource->type = VME_MASTER;
436 resource->entry = &(allocated_image->list);
443 spin_lock(&(master_image->lock));
444 master_image->locked = 0;
445 spin_unlock(&(master_image->lock));
450 EXPORT_SYMBOL(vme_master_request);
452 int vme_master_set (struct vme_resource *resource, int enabled,
453 unsigned long long vme_base, unsigned long long size,
454 vme_address_t aspace, vme_cycle_t cycle, vme_width_t dwidth)
456 struct vme_bridge *bridge = find_bridge(resource);
457 struct vme_master_resource *image;
460 if (resource->type != VME_MASTER) {
461 printk("Not a master resource\n");
465 image = list_entry(resource->entry, struct vme_master_resource, list);
467 if (bridge->master_set == NULL) {
468 printk("vme_master_set not supported\n");
472 if(!(((image->address_attr & aspace) == aspace) &&
473 ((image->cycle_attr & cycle) == cycle) &&
474 ((image->width_attr & dwidth) == dwidth))) {
475 printk("Invalid attributes\n");
479 retval = vme_check_window(aspace, vme_base, size);
483 return bridge->master_set(image, enabled, vme_base, size, aspace,
486 EXPORT_SYMBOL(vme_master_set);
488 int vme_master_get (struct vme_resource *resource, int *enabled,
489 unsigned long long *vme_base, unsigned long long *size,
490 vme_address_t *aspace, vme_cycle_t *cycle, vme_width_t *dwidth)
492 struct vme_bridge *bridge = find_bridge(resource);
493 struct vme_master_resource *image;
495 if (resource->type != VME_MASTER) {
496 printk("Not a master resource\n");
500 image = list_entry(resource->entry, struct vme_master_resource, list);
502 if (bridge->master_set == NULL) {
503 printk("vme_master_set not supported\n");
507 return bridge->master_get(image, enabled, vme_base, size, aspace,
510 EXPORT_SYMBOL(vme_master_get);
513 * Read data out of VME space into a buffer.
515 ssize_t vme_master_read (struct vme_resource *resource, void *buf, size_t count,
518 struct vme_bridge *bridge = find_bridge(resource);
519 struct vme_master_resource *image;
522 if (bridge->master_read == NULL) {
523 printk("Reading from resource not supported\n");
527 if (resource->type != VME_MASTER) {
528 printk("Not a master resource\n");
532 image = list_entry(resource->entry, struct vme_master_resource, list);
534 length = vme_get_size(resource);
536 if (offset > length) {
537 printk("Invalid Offset\n");
541 if ((offset + count) > length)
542 count = length - offset;
544 return bridge->master_read(image, buf, count, offset);
547 EXPORT_SYMBOL(vme_master_read);
550 * Write data out to VME space from a buffer.
552 ssize_t vme_master_write (struct vme_resource *resource, void *buf,
553 size_t count, loff_t offset)
555 struct vme_bridge *bridge = find_bridge(resource);
556 struct vme_master_resource *image;
559 if (bridge->master_write == NULL) {
560 printk("Writing to resource not supported\n");
564 if (resource->type != VME_MASTER) {
565 printk("Not a master resource\n");
569 image = list_entry(resource->entry, struct vme_master_resource, list);
571 length = vme_get_size(resource);
573 if (offset > length) {
574 printk("Invalid Offset\n");
578 if ((offset + count) > length)
579 count = length - offset;
581 return bridge->master_write(image, buf, count, offset);
583 EXPORT_SYMBOL(vme_master_write);
586 * Perform RMW cycle to provided location.
588 unsigned int vme_master_rmw (struct vme_resource *resource, unsigned int mask,
589 unsigned int compare, unsigned int swap, loff_t offset)
591 struct vme_bridge *bridge = find_bridge(resource);
592 struct vme_master_resource *image;
594 if (bridge->master_rmw == NULL) {
595 printk("Writing to resource not supported\n");
599 if (resource->type != VME_MASTER) {
600 printk("Not a master resource\n");
604 image = list_entry(resource->entry, struct vme_master_resource, list);
606 return bridge->master_rmw(image, mask, compare, swap, offset);
608 EXPORT_SYMBOL(vme_master_rmw);
610 void vme_master_free(struct vme_resource *resource)
612 struct vme_master_resource *master_image;
614 if (resource->type != VME_MASTER) {
615 printk("Not a master resource\n");
619 master_image = list_entry(resource->entry, struct vme_master_resource,
621 if (master_image == NULL) {
622 printk("Can't find master resource\n");
627 spin_lock(&(master_image->lock));
628 if (master_image->locked == 0)
629 printk(KERN_ERR "Image is already free\n");
631 master_image->locked = 0;
632 spin_unlock(&(master_image->lock));
634 /* Free up resource memory */
637 EXPORT_SYMBOL(vme_master_free);
640 * Request a DMA controller with specific attributes, return some unique
643 struct vme_resource *vme_request_dma(struct device *dev)
645 struct vme_bridge *bridge;
646 struct list_head *dma_pos = NULL;
647 struct vme_dma_resource *allocated_ctrlr = NULL;
648 struct vme_dma_resource *dma_ctrlr = NULL;
649 struct vme_resource *resource = NULL;
651 /* XXX Not checking resource attributes */
652 printk(KERN_ERR "No VME resource Attribute tests done\n");
654 bridge = dev_to_bridge(dev);
655 if (bridge == NULL) {
656 printk(KERN_ERR "Can't find VME bus\n");
660 /* Loop through DMA resources */
661 list_for_each(dma_pos, &(bridge->dma_resources)) {
662 dma_ctrlr = list_entry(dma_pos,
663 struct vme_dma_resource, list);
665 if (dma_ctrlr == NULL) {
666 printk("Registered NULL DMA resource\n");
670 /* Find an unlocked controller */
671 down(&(dma_ctrlr->sem));
672 if(dma_ctrlr->locked == 0) {
673 dma_ctrlr->locked = 1;
674 up(&(dma_ctrlr->sem));
675 allocated_ctrlr = dma_ctrlr;
678 up(&(dma_ctrlr->sem));
681 /* Check to see if we found a resource */
682 if (allocated_ctrlr == NULL)
685 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
686 if (resource == NULL) {
687 printk(KERN_WARNING "Unable to allocate resource structure\n");
690 resource->type = VME_DMA;
691 resource->entry = &(allocated_ctrlr->list);
697 down(&(dma_ctrlr->sem));
698 dma_ctrlr->locked = 0;
699 up(&(dma_ctrlr->sem));
704 EXPORT_SYMBOL(vme_request_dma);
709 struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
711 struct vme_dma_resource *ctrlr;
712 struct vme_dma_list *dma_list;
714 if (resource->type != VME_DMA) {
715 printk("Not a DMA resource\n");
719 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
721 dma_list = (struct vme_dma_list *)kmalloc(
722 sizeof(struct vme_dma_list), GFP_KERNEL);
723 if(dma_list == NULL) {
724 printk("Unable to allocate memory for new dma list\n");
727 INIT_LIST_HEAD(&(dma_list->entries));
728 dma_list->parent = ctrlr;
729 init_MUTEX(&(dma_list->sem));
733 EXPORT_SYMBOL(vme_new_dma_list);
736 * Create "Pattern" type attributes
738 struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern,
741 struct vme_dma_attr *attributes;
742 struct vme_dma_pattern *pattern_attr;
744 attributes = (struct vme_dma_attr *)kmalloc(
745 sizeof(struct vme_dma_attr), GFP_KERNEL);
746 if(attributes == NULL) {
747 printk("Unable to allocate memory for attributes structure\n");
751 pattern_attr = (struct vme_dma_pattern *)kmalloc(
752 sizeof(struct vme_dma_pattern), GFP_KERNEL);
753 if(pattern_attr == NULL) {
754 printk("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;
772 EXPORT_SYMBOL(vme_dma_pattern_attribute);
775 * Create "PCI" type attributes
777 struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
779 struct vme_dma_attr *attributes;
780 struct vme_dma_pci *pci_attr;
782 /* XXX Run some sanity checks here */
784 attributes = (struct vme_dma_attr *)kmalloc(
785 sizeof(struct vme_dma_attr), GFP_KERNEL);
786 if(attributes == NULL) {
787 printk("Unable to allocate memory for attributes structure\n");
791 pci_attr = (struct vme_dma_pci *)kmalloc(sizeof(struct vme_dma_pci),
793 if(pci_attr == NULL) {
794 printk("Unable to allocate memory for pci attributes\n");
800 attributes->type = VME_DMA_PCI;
801 attributes->private = (void *)pci_attr;
803 pci_attr->address = address;
813 EXPORT_SYMBOL(vme_dma_pci_attribute);
816 * Create "VME" type attributes
818 struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
819 vme_address_t aspace, vme_cycle_t cycle, vme_width_t dwidth)
821 struct vme_dma_attr *attributes;
822 struct vme_dma_vme *vme_attr;
824 /* XXX Run some sanity checks here */
826 attributes = (struct vme_dma_attr *)kmalloc(
827 sizeof(struct vme_dma_attr), GFP_KERNEL);
828 if(attributes == NULL) {
829 printk("Unable to allocate memory for attributes structure\n");
833 vme_attr = (struct vme_dma_vme *)kmalloc(sizeof(struct vme_dma_vme),
835 if(vme_attr == NULL) {
836 printk("Unable to allocate memory for vme attributes\n");
840 attributes->type = VME_DMA_VME;
841 attributes->private = (void *)vme_attr;
843 vme_attr->address = address;
844 vme_attr->aspace = aspace;
845 vme_attr->cycle = cycle;
846 vme_attr->dwidth = dwidth;
856 EXPORT_SYMBOL(vme_dma_vme_attribute);
861 void vme_dma_free_attribute(struct vme_dma_attr *attributes)
863 kfree(attributes->private);
866 EXPORT_SYMBOL(vme_dma_free_attribute);
868 int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
869 struct vme_dma_attr *dest, size_t count)
871 struct vme_bridge *bridge = list->parent->parent;
874 if (bridge->dma_list_add == NULL) {
875 printk("Link List DMA generation not supported\n");
879 if (down_trylock(&(list->sem))) {
880 printk("Link List already submitted\n");
884 retval = bridge->dma_list_add(list, src, dest, count);
890 EXPORT_SYMBOL(vme_dma_list_add);
892 int vme_dma_list_exec(struct vme_dma_list *list)
894 struct vme_bridge *bridge = list->parent->parent;
897 if (bridge->dma_list_exec == NULL) {
898 printk("Link List DMA execution not supported\n");
904 retval = bridge->dma_list_exec(list);
910 EXPORT_SYMBOL(vme_dma_list_exec);
912 int vme_dma_list_free(struct vme_dma_list *list)
914 struct vme_bridge *bridge = list->parent->parent;
917 if (bridge->dma_list_empty == NULL) {
918 printk("Emptying of Link Lists not supported\n");
922 if (down_trylock(&(list->sem))) {
923 printk("Link List in use\n");
928 * Empty out all of the entries from the dma list. We need to go to the
929 * low level driver as dma entries are driver specific.
931 retval = bridge->dma_list_empty(list);
933 printk("Unable to empty link-list entries\n");
942 EXPORT_SYMBOL(vme_dma_list_free);
944 int vme_dma_free(struct vme_resource *resource)
946 struct vme_dma_resource *ctrlr;
948 if (resource->type != VME_DMA) {
949 printk("Not a DMA resource\n");
953 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
955 if (down_trylock(&(ctrlr->sem))) {
956 printk("Resource busy, can't free\n");
960 if (!(list_empty(&(ctrlr->pending)) && list_empty(&(ctrlr->running)))) {
961 printk("Resource still processing transfers\n");
972 EXPORT_SYMBOL(vme_dma_free);
974 int vme_request_irq(struct device *dev, int level, int statid,
975 void (*callback)(int level, int vector, void *priv_data),
978 struct vme_bridge *bridge;
980 bridge = dev_to_bridge(dev);
981 if (bridge == NULL) {
982 printk(KERN_ERR "Can't find VME bus\n");
986 if((level < 1) || (level > 7)) {
987 printk(KERN_WARNING "Invalid interrupt level\n");
991 if (bridge->request_irq == NULL) {
992 printk("Registering interrupts not supported\n");
996 return bridge->request_irq(level, statid, callback, priv_data);
998 EXPORT_SYMBOL(vme_request_irq);
1000 void vme_free_irq(struct device *dev, int level, int statid)
1002 struct vme_bridge *bridge;
1004 bridge = dev_to_bridge(dev);
1005 if (bridge == NULL) {
1006 printk(KERN_ERR "Can't find VME bus\n");
1010 if((level < 1) || (level > 7)) {
1011 printk(KERN_WARNING "Invalid interrupt level\n");
1015 if (bridge->free_irq == NULL) {
1016 printk("Freeing interrupts not supported\n");
1020 bridge->free_irq(level, statid);
1022 EXPORT_SYMBOL(vme_free_irq);
1024 int vme_generate_irq(struct device *dev, int level, int statid)
1026 struct vme_bridge *bridge;
1028 bridge = dev_to_bridge(dev);
1029 if (bridge == NULL) {
1030 printk(KERN_ERR "Can't find VME bus\n");
1034 if((level < 1) || (level > 7)) {
1035 printk(KERN_WARNING "Invalid interrupt level\n");
1039 if (bridge->generate_irq == NULL) {
1040 printk("Interrupt generation not supported\n");
1044 return bridge->generate_irq(level, statid);
1046 EXPORT_SYMBOL(vme_generate_irq);
1048 int vme_lm_set(struct device *dev, unsigned long long lm_base, vme_address_t aspace,
1051 struct vme_bridge *bridge;
1053 bridge = dev_to_bridge(dev);
1054 if (bridge == NULL) {
1055 printk(KERN_ERR "Can't find VME bus\n");
1059 if (bridge->lm_set == NULL) {
1060 printk("vme_lm_set not supported\n");
1064 return bridge->lm_set(lm_base, aspace, cycle);
1066 EXPORT_SYMBOL(vme_lm_set);
1068 int vme_lm_get(struct device *dev, unsigned long long *lm_base, vme_address_t *aspace,
1071 struct vme_bridge *bridge;
1073 bridge = dev_to_bridge(dev);
1074 if (bridge == NULL) {
1075 printk(KERN_ERR "Can't find VME bus\n");
1079 if (bridge->lm_get == NULL) {
1080 printk("vme_lm_get not supported\n");
1084 return bridge->lm_get(lm_base, aspace, cycle);
1086 EXPORT_SYMBOL(vme_lm_get);
1088 int vme_lm_attach(struct device *dev, int monitor, void (*callback)(int))
1090 struct vme_bridge *bridge;
1092 bridge = dev_to_bridge(dev);
1093 if (bridge == NULL) {
1094 printk(KERN_ERR "Can't find VME bus\n");
1098 if (bridge->lm_attach == NULL) {
1099 printk("vme_lm_attach not supported\n");
1103 return bridge->lm_attach(monitor, callback);
1105 EXPORT_SYMBOL(vme_lm_attach);
1107 int vme_lm_detach(struct device *dev, int monitor)
1109 struct vme_bridge *bridge;
1111 bridge = dev_to_bridge(dev);
1112 if (bridge == NULL) {
1113 printk(KERN_ERR "Can't find VME bus\n");
1117 if (bridge->lm_detach == NULL) {
1118 printk("vme_lm_detach not supported\n");
1122 return bridge->lm_detach(monitor);
1124 EXPORT_SYMBOL(vme_lm_detach);
1126 int vme_slot_get(struct device *bus)
1128 struct vme_bridge *bridge;
1130 bridge = dev_to_bridge(bus);
1131 if (bridge == NULL) {
1132 printk(KERN_ERR "Can't find VME bus\n");
1136 if (bridge->slot_get == NULL) {
1137 printk("vme_slot_get not supported\n");
1141 return bridge->slot_get();
1143 EXPORT_SYMBOL(vme_slot_get);
1146 /* - Bridge Registration --------------------------------------------------- */
1148 static int vme_alloc_bus_num(void)
1152 down(&vme_bus_num_sem);
1153 for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
1154 if (((vme_bus_numbers >> i) & 0x1) == 0) {
1155 vme_bus_numbers |= (0x1 << i);
1159 up(&vme_bus_num_sem);
1164 static void vme_free_bus_num(int bus)
1166 down(&vme_bus_num_sem);
1167 vme_bus_numbers |= ~(0x1 << bus);
1168 up(&vme_bus_num_sem);
1171 int vme_register_bridge (struct vme_bridge *bridge)
1177 bridge->num = vme_alloc_bus_num();
1179 /* This creates 32 vme "slot" devices. This equates to a slot for each
1180 * ID available in a system conforming to the ANSI/VITA 1-1994
1183 for (i = 0; i < VME_SLOTS_MAX; i++) {
1184 dev = &(bridge->dev[i]);
1185 memset(dev, 0, sizeof(struct device));
1187 dev->parent = bridge->parent;
1188 dev->bus = &(vme_bus_type);
1190 * We save a pointer to the bridge in platform_data so that we
1191 * can get to it later. We keep driver_data for use by the
1192 * driver that binds against the slot
1194 dev->platform_data = bridge;
1195 dev_set_name(dev, "vme-%x.%x", bridge->num, i + 1);
1197 retval = device_register(dev);
1207 dev = &(bridge->dev[i]);
1208 device_unregister(dev);
1210 vme_free_bus_num(bridge->num);
1213 EXPORT_SYMBOL(vme_register_bridge);
1215 void vme_unregister_bridge (struct vme_bridge *bridge)
1221 for (i = 0; i < VME_SLOTS_MAX; i++) {
1222 dev = &(bridge->dev[i]);
1223 device_unregister(dev);
1225 vme_free_bus_num(bridge->num);
1227 EXPORT_SYMBOL(vme_unregister_bridge);
1230 /* - Driver Registration --------------------------------------------------- */
1232 int vme_register_driver (struct vme_driver *drv)
1234 drv->driver.name = drv->name;
1235 drv->driver.bus = &vme_bus_type;
1237 return driver_register(&drv->driver);
1239 EXPORT_SYMBOL(vme_register_driver);
1241 void vme_unregister_driver (struct vme_driver *drv)
1243 driver_unregister(&drv->driver);
1245 EXPORT_SYMBOL(vme_unregister_driver);
1247 /* - Bus Registration ------------------------------------------------------ */
1249 int vme_calc_slot(struct device *dev)
1251 struct vme_bridge *bridge;
1254 bridge = dev_to_bridge(dev);
1256 /* Determine slot number */
1258 while(num < VME_SLOTS_MAX) {
1259 if(&(bridge->dev[num]) == dev) {
1264 if (num == VME_SLOTS_MAX) {
1265 dev_err(dev, "Failed to identify slot\n");
1275 static struct vme_driver *dev_to_vme_driver(struct device *dev)
1277 if(dev->driver == NULL)
1278 printk("Bugger dev->driver is NULL\n");
1280 return container_of(dev->driver, struct vme_driver, driver);
1283 static int vme_bus_match(struct device *dev, struct device_driver *drv)
1285 struct vme_bridge *bridge;
1286 struct vme_driver *driver;
1289 bridge = dev_to_bridge(dev);
1290 driver = container_of(drv, struct vme_driver, driver);
1292 num = vme_calc_slot(dev);
1296 if (driver->bind_table == NULL) {
1297 dev_err(dev, "Bind table NULL\n");
1302 while((driver->bind_table[i].bus != 0) ||
1303 (driver->bind_table[i].slot != 0)) {
1305 if ((bridge->num == driver->bind_table[i].bus) &&
1306 (num == driver->bind_table[i].slot))
1316 static int vme_bus_probe(struct device *dev)
1318 struct vme_bridge *bridge;
1319 struct vme_driver *driver;
1320 int retval = -ENODEV;
1322 driver = dev_to_vme_driver(dev);
1323 bridge = dev_to_bridge(dev);
1325 if(driver->probe != NULL) {
1326 retval = driver->probe(dev, bridge->num, vme_calc_slot(dev));
1332 static int vme_bus_remove(struct device *dev)
1334 struct vme_bridge *bridge;
1335 struct vme_driver *driver;
1336 int retval = -ENODEV;
1338 driver = dev_to_vme_driver(dev);
1339 bridge = dev_to_bridge(dev);
1341 if(driver->remove != NULL) {
1342 retval = driver->remove(dev, bridge->num, vme_calc_slot(dev));
1348 struct bus_type vme_bus_type = {
1350 .match = vme_bus_match,
1351 .probe = vme_bus_probe,
1352 .remove = vme_bus_remove,
1354 EXPORT_SYMBOL(vme_bus_type);
1356 static int __init vme_init (void)
1358 return bus_register(&vme_bus_type);
1361 static void __exit vme_exit (void)
1363 bus_unregister(&vme_bus_type);
1366 MODULE_DESCRIPTION("VME bridge driver framework");
1367 MODULE_AUTHOR("Martyn Welch <martyn.welch@gefanuc.com");
1368 MODULE_LICENSE("GPL");
1370 module_init(vme_init);
1371 module_exit(vme_exit);