2 * arch/v850/kernel/mb_a_pci.c -- PCI support for Midas lab RTE-MOTHER-A board
4 * Copyright (C) 2001,02,03,05 NEC Electronics Corporation
5 * Copyright (C) 2001,02,03,05 Miles Bader <miles@gnu.org>
7 * This file is subject to the terms and conditions of the GNU General
8 * Public License. See the file COPYING in the main directory of this
9 * archive for more details.
11 * Written by Miles Bader <miles@gnu.org>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
19 #include <linux/pci.h>
21 #include <asm/machdep.h>
23 /* __nomods_init is like __devinit, but is a no-op when modules are enabled.
24 This is used by some routines that can be called either during boot
27 #define __nomods_init /*nothing*/
29 #define __nomods_init __devinit
32 /* PCI devices on the Mother-A board can only do DMA to/from the MB SRAM
33 (the RTE-V850E/MA1-CB cpu board doesn't support PCI access to
34 CPU-board memory), and since linux DMA buffers are allocated in
35 normal kernel memory, we basically have to copy DMA blocks around
36 (this is like a `bounce buffer'). When a DMA block is `mapped', we
37 allocate an identically sized block in MB SRAM, and if we're doing
38 output to the device, copy the CPU-memory block to the MB-SRAM block.
39 When an active block is `unmapped', we will copy the block back to
40 CPU memory if necessary, and then deallocate the MB SRAM block.
43 /* Where the motherboard SRAM is in the PCI-bus address space (the
44 first 512K of it is also mapped at PCI address 0). */
45 #define PCI_MB_SRAM_ADDR 0x800000
47 /* Convert CPU-view MB SRAM address to/from PCI-view addresses of the
49 #define MB_SRAM_TO_PCI(mb_sram_addr) \
50 ((dma_addr_t)mb_sram_addr - MB_A_SRAM_ADDR + PCI_MB_SRAM_ADDR)
51 #define PCI_TO_MB_SRAM(pci_addr) \
52 (void *)(pci_addr - PCI_MB_SRAM_ADDR + MB_A_SRAM_ADDR)
54 static void pcibios_assign_resources (void);
56 struct mb_pci_dev_irq {
57 unsigned dev; /* PCI device number */
58 unsigned irq_base; /* First IRQ */
59 unsigned query_pin; /* True if we should read the device's
60 Interrupt Pin info, and allocate
61 interrupt IRQ_BASE + PIN. */
64 /* PCI interrupts are mapped statically to GBUS interrupts. */
65 static struct mb_pci_dev_irq mb_pci_dev_irqs[] = {
66 /* Motherboard SB82558 ethernet controller */
67 { 10, IRQ_MB_A_LAN, 0 },
69 { 8, IRQ_MB_A_PCI1(0), 1 },
71 { 9, IRQ_MB_A_PCI2(0), 1 }
73 #define NUM_MB_PCI_DEV_IRQS ARRAY_SIZE(mb_pci_dev_irqs)
76 /* PCI configuration primitives. */
78 #define CONFIG_DMCFGA(bus, devfn, offs) \
82 | ((bus)->number << 16))
85 mb_pci_read (struct pci_bus *bus, unsigned devfn, int offs, int size, u32 *rval)
90 local_irq_save (flags);
93 MB_A_PCI_DMCFGA = CONFIG_DMCFGA (bus, devfn, offs);
95 addr = MB_A_PCI_IO_ADDR + (offs & 0x3);
98 case 1: *rval = *(volatile u8 *)addr; break;
99 case 2: *rval = *(volatile u16 *)addr; break;
100 case 4: *rval = *(volatile u32 *)addr; break;
103 if (MB_A_PCI_PCISR & 0x2000) {
104 MB_A_PCI_PCISR = 0x2000;
110 local_irq_restore (flags);
112 return PCIBIOS_SUCCESSFUL;
116 mb_pci_write (struct pci_bus *bus, unsigned devfn, int offs, int size, u32 val)
121 local_irq_save (flags);
123 MB_A_PCI_PCICR = 0x7;
124 MB_A_PCI_DMCFGA = CONFIG_DMCFGA (bus, devfn, offs);
126 addr = MB_A_PCI_IO_ADDR + (offs & 0x3);
129 case 1: *(volatile u8 *)addr = val; break;
130 case 2: *(volatile u16 *)addr = val; break;
131 case 4: *(volatile u32 *)addr = val; break;
134 if (MB_A_PCI_PCISR & 0x2000)
135 MB_A_PCI_PCISR = 0x2000;
139 local_irq_restore (flags);
141 return PCIBIOS_SUCCESSFUL;
144 static struct pci_ops mb_pci_config_ops = {
146 .write = mb_pci_write,
150 /* PCI Initialization. */
152 static struct pci_bus *mb_pci_bus = 0;
154 /* Do initial PCI setup. */
155 static int __devinit pcibios_init (void)
157 u32 id = MB_A_PCI_PCIHIDR;
158 u16 vendor = id & 0xFFFF;
159 u16 device = (id >> 16) & 0xFFFF;
161 if (vendor == PCI_VENDOR_ID_PLX && device == PCI_DEVICE_ID_PLX_9080) {
163 "PCI: PLX Technology PCI9080 HOST/PCI bridge\n");
165 MB_A_PCI_PCICR = 0x147;
167 MB_A_PCI_PCIBAR0 = 0x007FFF00;
168 MB_A_PCI_PCIBAR1 = 0x0000FF00;
169 MB_A_PCI_PCIBAR2 = 0x00800000;
171 MB_A_PCI_PCILTR = 0x20;
173 MB_A_PCI_PCIPBAM |= 0x3;
175 MB_A_PCI_PCISR = ~0; /* Clear errors. */
177 /* Reprogram the motherboard's IO/config address space,
178 as we don't support the GCS7 address space that the
181 /* Significant address bits used for decoding PCI GCS5 space
183 MB_A_PCI_DMRR = ~(MB_A_PCI_MEM_SIZE - 1);
185 /* I don't understand this, but the SolutionGear example code
186 uses such an offset, and it doesn't work without it. XXX */
187 #if GCS5_SIZE == 0x00800000
188 #define GCS5_CFG_OFFS 0x00800000
190 #define GCS5_CFG_OFFS 0
193 /* Address bit values for matching. Note that we have to give
194 the address from the motherboard's point of view, which is
195 different than the CPU's. */
196 /* PCI memory space. */
197 MB_A_PCI_DMLBAM = GCS5_CFG_OFFS + 0x0;
200 GCS5_CFG_OFFS + (MB_A_PCI_IO_ADDR - GCS5_ADDR);
202 mb_pci_bus = pci_scan_bus (0, &mb_pci_config_ops, 0);
204 pcibios_assign_resources ();
206 printk (KERN_ERR "PCI: HOST/PCI bridge not found\n");
211 subsys_initcall (pcibios_init);
213 char __devinit *pcibios_setup (char *option)
215 /* Don't handle any options. */
220 int __nomods_init pcibios_enable_device (struct pci_dev *dev, int mask)
226 pci_read_config_word(dev, PCI_COMMAND, &cmd);
228 for (idx = 0; idx < 6; idx++) {
229 r = &dev->resource[idx];
230 if (!r->start && r->end) {
231 printk(KERN_ERR "PCI: Device %s not available because "
232 "of resource collisions\n", pci_name(dev));
235 if (r->flags & IORESOURCE_IO)
236 cmd |= PCI_COMMAND_IO;
237 if (r->flags & IORESOURCE_MEM)
238 cmd |= PCI_COMMAND_MEMORY;
240 if (cmd != old_cmd) {
241 printk("PCI: Enabling device %s (%04x -> %04x)\n",
242 pci_name(dev), old_cmd, cmd);
243 pci_write_config_word(dev, PCI_COMMAND, cmd);
249 /* Resource allocation. */
250 static void __devinit pcibios_assign_resources (void)
252 struct pci_dev *dev = NULL;
255 for_each_pci_dev(dev) {
257 unsigned class = dev->class >> 8;
259 if (class && class != PCI_CLASS_BRIDGE_HOST) {
261 for(r_num = 0; r_num < 6; r_num++) {
262 r = &dev->resource[r_num];
263 if (!r->start && r->end)
264 pci_assign_resource (dev, r_num);
268 /* Assign interrupts. */
269 for (di_num = 0; di_num < NUM_MB_PCI_DEV_IRQS; di_num++) {
270 struct mb_pci_dev_irq *di = &mb_pci_dev_irqs[di_num];
272 if (di->dev == PCI_SLOT (dev->devfn)) {
273 unsigned irq = di->irq_base;
276 /* Find out which interrupt pin
277 this device uses (each PCI
281 pci_read_config_byte (dev,
286 /* Doesn't use interrupts. */
292 pcibios_update_irq (dev, irq);
298 void __devinit pcibios_update_irq (struct pci_dev *dev, int irq)
301 pci_write_config_byte (dev, PCI_INTERRUPT_LINE, irq);
305 pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
306 struct resource *res)
308 unsigned long offset = 0;
310 if (res->flags & IORESOURCE_IO) {
311 offset = MB_A_PCI_IO_ADDR;
312 } else if (res->flags & IORESOURCE_MEM) {
313 offset = MB_A_PCI_MEM_ADDR;
316 region->start = res->start - offset;
317 region->end = res->end - offset;
321 /* Stubs for things we don't use. */
323 /* Called after each bus is probed, but before its children are examined. */
324 void pcibios_fixup_bus(struct pci_bus *b)
329 pcibios_align_resource (void *data, struct resource *res,
330 resource_size_t size, resource_size_t align)
334 void pcibios_set_master (struct pci_dev *dev)
339 /* Mother-A SRAM memory allocation. This is a simple first-fit allocator. */
341 /* A memory free-list node. */
342 struct mb_sram_free_area {
345 struct mb_sram_free_area *next;
348 /* The tail of the free-list, which starts out containing all the SRAM. */
349 static struct mb_sram_free_area mb_sram_free_tail = {
350 (void *)MB_A_SRAM_ADDR, MB_A_SRAM_SIZE, 0
354 static struct mb_sram_free_area *mb_sram_free_areas = &mb_sram_free_tail;
356 /* The free-list of free free-list nodes. (:-) */
357 static struct mb_sram_free_area *mb_sram_free_free_areas = 0;
359 /* Spinlock protecting the above globals. */
360 static DEFINE_SPINLOCK(mb_sram_lock);
362 /* Allocate a memory block at least SIZE bytes long in the Mother-A SRAM
364 static void *alloc_mb_sram (size_t size)
366 struct mb_sram_free_area *prev, *fa;
370 spin_lock_irqsave (mb_sram_lock, flags);
372 /* Look for a free area that can contain SIZE bytes. */
373 for (prev = 0, fa = mb_sram_free_areas; fa; prev = fa, fa = fa->next)
374 if (fa->size >= size) {
378 if (fa->size == size) {
379 /* In fact, it fits exactly, so remove
380 this node from the free-list. */
382 prev->next = fa->next;
384 mb_sram_free_areas = fa->next;
385 /* Put it on the free-list-entry-free-list. */
386 fa->next = mb_sram_free_free_areas;
387 mb_sram_free_free_areas = fa;
389 /* FA is bigger than SIZE, so just
390 reduce its size to account for this
399 spin_unlock_irqrestore (mb_sram_lock, flags);
404 /* Return the memory area MEM of size SIZE to the MB SRAM free pool. */
405 static void free_mb_sram (void *mem, size_t size)
407 struct mb_sram_free_area *prev, *fa, *new_fa;
409 void *end = mem + size;
411 spin_lock_irqsave (mb_sram_lock, flags);
414 /* Find an adjacent free-list entry. */
415 for (prev = 0, fa = mb_sram_free_areas; fa; prev = fa, fa = fa->next)
416 if (fa->mem == end) {
417 /* FA is just after MEM, grow down to encompass it. */
421 } else if (fa->mem + fa->size == mem) {
422 struct mb_sram_free_area *next_fa = fa->next;
424 /* FA is just before MEM, expand to encompass it. */
427 /* See if FA can now be merged with its successor. */
428 if (next_fa && fa->mem + fa->size == next_fa->mem) {
429 /* Yup; merge NEXT_FA's info into FA. */
430 fa->size += next_fa->size;
431 fa->next = next_fa->next;
433 next_fa->next = mb_sram_free_free_areas;
434 mb_sram_free_free_areas = next_fa;
437 } else if (fa->mem > mem)
438 /* We've reached the right spot in the free-list
439 without finding an adjacent free-area, so add
440 a new free area to hold mem. */
443 /* Make a new free-list entry. */
445 /* First, get a free-list entry. */
446 if (! mb_sram_free_free_areas) {
447 /* There are none, so make some. */
449 size_t block_size = sizeof (struct mb_sram_free_area) * 8;
451 /* Don't hold the lock while calling kmalloc (I'm not
452 sure whether it would be a problem, since we use
453 GFP_ATOMIC, but it makes me nervous). */
454 spin_unlock_irqrestore (mb_sram_lock, flags);
456 block = kmalloc (block_size, GFP_ATOMIC);
458 panic ("free_mb_sram: can't allocate free-list entry");
460 /* Now get the lock back. */
461 spin_lock_irqsave (mb_sram_lock, flags);
463 /* Add the new free free-list entries. */
464 while (block_size > 0) {
465 struct mb_sram_free_area *nfa = block;
466 nfa->next = mb_sram_free_free_areas;
467 mb_sram_free_free_areas = nfa;
468 block += sizeof *nfa;
469 block_size -= sizeof *nfa;
472 /* Since we dropped the lock to call kmalloc, the
473 free-list could have changed, so retry from the
478 /* Remove NEW_FA from the free-list of free-list entries. */
479 new_fa = mb_sram_free_free_areas;
480 mb_sram_free_free_areas = new_fa->next;
482 /* NEW_FA initially holds only MEM. */
486 /* Insert NEW_FA in the free-list between PREV and FA. */
491 mb_sram_free_areas = new_fa;
494 spin_unlock_irqrestore (mb_sram_lock, flags);
498 /* Maintainence of CPU -> Mother-A DMA mappings. */
504 struct dma_mapping *next;
507 /* A list of mappings from CPU addresses to MB SRAM addresses for active
508 DMA blocks (that have been `granted' to the PCI device). */
509 static struct dma_mapping *active_dma_mappings = 0;
511 /* A list of free mapping objects. */
512 static struct dma_mapping *free_dma_mappings = 0;
514 /* Spinlock protecting the above globals. */
515 static DEFINE_SPINLOCK(dma_mappings_lock);
517 static struct dma_mapping *new_dma_mapping (size_t size)
520 struct dma_mapping *mapping;
521 void *mb_sram_block = alloc_mb_sram (size);
526 spin_lock_irqsave (dma_mappings_lock, flags);
528 if (! free_dma_mappings) {
529 /* We're out of mapping structures, make more. */
531 size_t mblock_size = sizeof (struct dma_mapping) * 8;
533 /* Don't hold the lock while calling kmalloc (I'm not
534 sure whether it would be a problem, since we use
535 GFP_ATOMIC, but it makes me nervous). */
536 spin_unlock_irqrestore (dma_mappings_lock, flags);
538 mblock = kmalloc (mblock_size, GFP_ATOMIC);
540 free_mb_sram (mb_sram_block, size);
544 /* Get the lock back. */
545 spin_lock_irqsave (dma_mappings_lock, flags);
547 /* Add the new mapping structures to the free-list. */
548 while (mblock_size > 0) {
549 struct dma_mapping *fm = mblock;
550 fm->next = free_dma_mappings;
551 free_dma_mappings = fm;
552 mblock += sizeof *fm;
553 mblock_size -= sizeof *fm;
557 /* Get a mapping struct from the freelist. */
558 mapping = free_dma_mappings;
559 free_dma_mappings = mapping->next;
561 /* Initialize the mapping. Other fields should be filled in by
563 mapping->mb_sram_addr = mb_sram_block;
564 mapping->size = size;
566 /* Add it to the list of active mappings. */
567 mapping->next = active_dma_mappings;
568 active_dma_mappings = mapping;
570 spin_unlock_irqrestore (dma_mappings_lock, flags);
575 static struct dma_mapping *find_dma_mapping (void *mb_sram_addr)
578 struct dma_mapping *mapping;
580 spin_lock_irqsave (dma_mappings_lock, flags);
582 for (mapping = active_dma_mappings; mapping; mapping = mapping->next)
583 if (mapping->mb_sram_addr == mb_sram_addr) {
584 spin_unlock_irqrestore (dma_mappings_lock, flags);
588 panic ("find_dma_mapping: unmapped PCI DMA addr 0x%x",
589 MB_SRAM_TO_PCI (mb_sram_addr));
592 static struct dma_mapping *deactivate_dma_mapping (void *mb_sram_addr)
595 struct dma_mapping *mapping, *prev;
597 spin_lock_irqsave (dma_mappings_lock, flags);
599 for (prev = 0, mapping = active_dma_mappings;
601 prev = mapping, mapping = mapping->next)
603 if (mapping->mb_sram_addr == mb_sram_addr) {
604 /* This is the MAPPING; deactivate it. */
606 prev->next = mapping->next;
608 active_dma_mappings = mapping->next;
610 spin_unlock_irqrestore (dma_mappings_lock, flags);
616 panic ("deactivate_dma_mapping: unmapped PCI DMA addr 0x%x",
617 MB_SRAM_TO_PCI (mb_sram_addr));
620 /* Return MAPPING to the freelist. */
622 free_dma_mapping (struct dma_mapping *mapping)
626 free_mb_sram (mapping->mb_sram_addr, mapping->size);
628 spin_lock_irqsave (dma_mappings_lock, flags);
630 mapping->next = free_dma_mappings;
631 free_dma_mappings = mapping;
633 spin_unlock_irqrestore (dma_mappings_lock, flags);
637 /* Single PCI DMA mappings. */
639 /* `Grant' to PDEV the memory block at CPU_ADDR, for doing DMA. The
640 32-bit PCI bus mastering address to use is returned. the device owns
641 this memory until either pci_unmap_single or pci_dma_sync_single is
644 pci_map_single (struct pci_dev *pdev, void *cpu_addr, size_t size, int dir)
646 struct dma_mapping *mapping = new_dma_mapping (size);
651 mapping->cpu_addr = cpu_addr;
653 if (dir == PCI_DMA_BIDIRECTIONAL || dir == PCI_DMA_TODEVICE)
654 memcpy (mapping->mb_sram_addr, cpu_addr, size);
656 return MB_SRAM_TO_PCI (mapping->mb_sram_addr);
659 /* Return to the CPU the PCI DMA memory block previously `granted' to
660 PDEV, at DMA_ADDR. */
661 void pci_unmap_single (struct pci_dev *pdev, dma_addr_t dma_addr, size_t size,
664 void *mb_sram_addr = PCI_TO_MB_SRAM (dma_addr);
665 struct dma_mapping *mapping = deactivate_dma_mapping (mb_sram_addr);
667 if (size != mapping->size)
668 panic ("pci_unmap_single: size (%d) doesn't match"
669 " size of mapping at PCI DMA addr 0x%x (%d)\n",
670 size, dma_addr, mapping->size);
672 /* Copy back the DMA'd contents if necessary. */
673 if (dir == PCI_DMA_BIDIRECTIONAL || dir == PCI_DMA_FROMDEVICE)
674 memcpy (mapping->cpu_addr, mb_sram_addr, size);
676 /* Return mapping to the freelist. */
677 free_dma_mapping (mapping);
680 /* Make physical memory consistent for a single streaming mode DMA
681 translation after a transfer.
683 If you perform a pci_map_single() but wish to interrogate the
684 buffer using the cpu, yet do not wish to teardown the PCI dma
685 mapping, you must call this function before doing so. At the next
686 point you give the PCI dma address back to the card, you must first
687 perform a pci_dma_sync_for_device, and then the device again owns
690 pci_dma_sync_single_for_cpu (struct pci_dev *pdev, dma_addr_t dma_addr, size_t size,
693 void *mb_sram_addr = PCI_TO_MB_SRAM (dma_addr);
694 struct dma_mapping *mapping = find_dma_mapping (mb_sram_addr);
696 /* Synchronize the DMA buffer with the CPU buffer if necessary. */
697 if (dir == PCI_DMA_FROMDEVICE)
698 memcpy (mapping->cpu_addr, mb_sram_addr, size);
699 else if (dir == PCI_DMA_TODEVICE)
700 ; /* nothing to do */
702 panic("pci_dma_sync_single: unsupported sync dir: %d", dir);
706 pci_dma_sync_single_for_device (struct pci_dev *pdev, dma_addr_t dma_addr, size_t size,
709 void *mb_sram_addr = PCI_TO_MB_SRAM (dma_addr);
710 struct dma_mapping *mapping = find_dma_mapping (mb_sram_addr);
712 /* Synchronize the DMA buffer with the CPU buffer if necessary. */
713 if (dir == PCI_DMA_FROMDEVICE)
714 ; /* nothing to do */
715 else if (dir == PCI_DMA_TODEVICE)
716 memcpy (mb_sram_addr, mapping->cpu_addr, size);
718 panic("pci_dma_sync_single: unsupported sync dir: %d", dir);
722 /* Scatter-gather PCI DMA mappings. */
724 /* Do multiple DMA mappings at once. */
726 pci_map_sg (struct pci_dev *pdev, struct scatterlist *sg, int sg_len, int dir)
732 /* Unmap multiple DMA mappings at once. */
734 pci_unmap_sg (struct pci_dev *pdev, struct scatterlist *sg, int sg_len,int dir)
739 /* Make physical memory consistent for a set of streaming mode DMA
740 translations after a transfer. The same as pci_dma_sync_single_* but
741 for a scatter-gather list, same rules and usage. */
744 pci_dma_sync_sg_for_cpu (struct pci_dev *dev,
745 struct scatterlist *sg, int sg_len,
752 pci_dma_sync_sg_for_device (struct pci_dev *dev,
753 struct scatterlist *sg, int sg_len,
760 /* PCI mem mapping. */
762 /* Allocate and map kernel buffer using consistent mode DMA for PCI
763 device. Returns non-NULL cpu-view pointer to the buffer if
764 successful and sets *DMA_ADDR to the pci side dma address as well,
765 else DMA_ADDR is undefined. */
767 pci_alloc_consistent (struct pci_dev *pdev, size_t size, dma_addr_t *dma_addr)
769 void *mb_sram_mem = alloc_mb_sram (size);
771 *dma_addr = MB_SRAM_TO_PCI (mb_sram_mem);
775 /* Free and unmap a consistent DMA buffer. CPU_ADDR and DMA_ADDR must
776 be values that were returned from pci_alloc_consistent. SIZE must be
777 the same as what as passed into pci_alloc_consistent. References to
778 the memory and mappings associated with CPU_ADDR or DMA_ADDR past
779 this call are illegal. */
781 pci_free_consistent (struct pci_dev *pdev, size_t size, void *cpu_addr,
784 void *mb_sram_mem = PCI_TO_MB_SRAM (dma_addr);
785 free_mb_sram (mb_sram_mem, size);
791 void __iomem *pci_iomap (struct pci_dev *dev, int bar, unsigned long max)
793 unsigned long start = pci_resource_start (dev, bar);
794 unsigned long len = pci_resource_len (dev, bar);
796 if (!start || len == 0)
799 /* None of the ioremap functions actually do anything, other than
800 re-casting their argument, so don't bother differentiating them. */
801 return ioremap (start, len);
804 void pci_iounmap (struct pci_dev *dev, void __iomem *addr)
810 /* symbol exports (for modules) */
812 EXPORT_SYMBOL (pci_map_single);
813 EXPORT_SYMBOL (pci_unmap_single);
814 EXPORT_SYMBOL (pci_alloc_consistent);
815 EXPORT_SYMBOL (pci_free_consistent);
816 EXPORT_SYMBOL (pci_dma_sync_single_for_cpu);
817 EXPORT_SYMBOL (pci_dma_sync_single_for_device);
818 EXPORT_SYMBOL (pci_iomap);
819 EXPORT_SYMBOL (pci_iounmap);