Path: blob/master/arch/mn10300/unit-asb2305/pci-asb2305.c
10817 views
/* ASB2305 PCI resource stuff1*2* Copyright (C) 2001 Red Hat, Inc. All Rights Reserved.3* Written by David Howells ([email protected])4* - Derived from arch/i386/pci-i386.c5* - Copyright 1997--2000 Martin Mares <[email protected]>6*7* This program is free software; you can redistribute it and/or8* modify it under the terms of the GNU General Public Licence9* as published by the Free Software Foundation; either version10* 2 of the Licence, or (at your option) any later version.11*/12#include <linux/types.h>13#include <linux/kernel.h>14#include <linux/pci.h>15#include <linux/init.h>16#include <linux/ioport.h>17#include <linux/errno.h>18#include "pci-asb2305.h"1920/*21* We need to avoid collisions with `mirrored' VGA ports22* and other strange ISA hardware, so we always want the23* addresses to be allocated in the 0x000-0x0ff region24* modulo 0x400.25*26* Why? Because some silly external IO cards only decode27* the low 10 bits of the IO address. The 0x00-0xff region28* is reserved for motherboard devices that decode all 1629* bits, so it's ok to allocate at, say, 0x2800-0x28ff,30* but we want to try to avoid allocating at 0x2900-0x2bff31* which might have be mirrored at 0x0100-0x03ff..32*/33resource_size_t pcibios_align_resource(void *data, const struct resource *res,34resource_size_t size, resource_size_t align)35{36resource_size_t start = res->start;3738#if 039struct pci_dev *dev = data;4041printk(KERN_DEBUG42"### PCIBIOS_ALIGN_RESOURCE(%s,,{%08lx-%08lx,%08lx},%lx)\n",43pci_name(dev),44res->start,45res->end,46res->flags,47size48);49#endif5051if ((res->flags & IORESOURCE_IO) && (start & 0x300))52start = (start + 0x3ff) & ~0x3ff;5354return start;55}565758/*59* Handle resources of PCI devices. If the world were perfect, we could60* just allocate all the resource regions and do nothing more. It isn't.61* On the other hand, we cannot just re-allocate all devices, as it would62* require us to know lots of host bridge internals. So we attempt to63* keep as much of the original configuration as possible, but tweak it64* when it's found to be wrong.65*66* Known BIOS problems we have to work around:67* - I/O or memory regions not configured68* - regions configured, but not enabled in the command register69* - bogus I/O addresses above 64K used70* - expansion ROMs left enabled (this may sound harmless, but given71* the fact the PCI specs explicitly allow address decoders to be72* shared between expansion ROMs and other resource regions, it's73* at least dangerous)74*75* Our solution:76* (1) Allocate resources for all buses behind PCI-to-PCI bridges.77* This gives us fixed barriers on where we can allocate.78* (2) Allocate resources for all enabled devices. If there is79* a collision, just mark the resource as unallocated. Also80* disable expansion ROMs during this step.81* (3) Try to allocate resources for disabled devices. If the82* resources were assigned correctly, everything goes well,83* if they weren't, they won't disturb allocation of other84* resources.85* (4) Assign new addresses to resources which were either86* not configured at all or misconfigured. If explicitly87* requested by the user, configure expansion ROM address88* as well.89*/90static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)91{92struct pci_bus *bus;93struct pci_dev *dev;94int idx;95struct resource *r;9697/* Depth-First Search on bus tree */98list_for_each_entry(bus, bus_list, node) {99dev = bus->self;100if (dev) {101for (idx = PCI_BRIDGE_RESOURCES;102idx < PCI_NUM_RESOURCES;103idx++) {104r = &dev->resource[idx];105if (!r->flags)106continue;107if (!r->start ||108pci_claim_resource(dev, idx) < 0) {109printk(KERN_ERR "PCI:"110" Cannot allocate resource"111" region %d of bridge %s\n",112idx, pci_name(dev));113/* Something is wrong with the region.114* Invalidate the resource to prevent115* child resource allocations in this116* range. */117r->start = r->end = 0;118r->flags = 0;119}120}121}122pcibios_allocate_bus_resources(&bus->children);123}124}125126static void __init pcibios_allocate_resources(int pass)127{128struct pci_dev *dev = NULL;129int idx, disabled;130u16 command;131struct resource *r;132133for_each_pci_dev(dev) {134pci_read_config_word(dev, PCI_COMMAND, &command);135for (idx = 0; idx < 6; idx++) {136r = &dev->resource[idx];137if (r->parent) /* Already allocated */138continue;139if (!r->start) /* Address not assigned */140continue;141if (r->flags & IORESOURCE_IO)142disabled = !(command & PCI_COMMAND_IO);143else144disabled = !(command & PCI_COMMAND_MEMORY);145if (pass == disabled) {146DBG("PCI[%s]: Resource %08lx-%08lx"147" (f=%lx, d=%d, p=%d)\n",148pci_name(dev), r->start, r->end, r->flags,149disabled, pass);150if (pci_claim_resource(dev, idx) < 0) {151printk(KERN_ERR "PCI:"152" Cannot allocate resource"153" region %d of device %s\n",154idx, pci_name(dev));155/* We'll assign a new address later */156r->end -= r->start;157r->start = 0;158}159}160}161if (!pass) {162r = &dev->resource[PCI_ROM_RESOURCE];163if (r->flags & IORESOURCE_ROM_ENABLE) {164/* Turn the ROM off, leave the resource region,165* but keep it unregistered. */166u32 reg;167DBG("PCI: Switching off ROM of %s\n",168pci_name(dev));169r->flags &= ~IORESOURCE_ROM_ENABLE;170pci_read_config_dword(171dev, dev->rom_base_reg, ®);172pci_write_config_dword(173dev, dev->rom_base_reg,174reg & ~PCI_ROM_ADDRESS_ENABLE);175}176}177}178}179180static int __init pcibios_assign_resources(void)181{182struct pci_dev *dev = NULL;183struct resource *r;184185if (!(pci_probe & PCI_ASSIGN_ROMS)) {186/* Try to use BIOS settings for ROMs, otherwise let187pci_assign_unassigned_resources() allocate the new188addresses. */189for_each_pci_dev(dev) {190r = &dev->resource[PCI_ROM_RESOURCE];191if (!r->flags || !r->start)192continue;193if (pci_claim_resource(dev, PCI_ROM_RESOURCE) < 0) {194r->end -= r->start;195r->start = 0;196}197}198}199200pci_assign_unassigned_resources();201202return 0;203}204205fs_initcall(pcibios_assign_resources);206207void __init pcibios_resource_survey(void)208{209DBG("PCI: Allocating resources\n");210pcibios_allocate_bus_resources(&pci_root_buses);211pcibios_allocate_resources(0);212pcibios_allocate_resources(1);213}214215/*216* If we set up a device for bus mastering, we need to check the latency217* timer as certain crappy BIOSes forget to set it properly.218*/219unsigned int pcibios_max_latency = 255;220221void pcibios_set_master(struct pci_dev *dev)222{223u8 lat;224225pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);226227if (lat < 16)228lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;229else if (lat > pcibios_max_latency)230lat = pcibios_max_latency;231else232return;233234pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);235}236237int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,238enum pci_mmap_state mmap_state, int write_combine)239{240unsigned long prot;241242/* Leave vm_pgoff as-is, the PCI space address is the physical243* address on this platform.244*/245vma->vm_flags |= VM_LOCKED | VM_IO;246247prot = pgprot_val(vma->vm_page_prot);248prot &= ~_PAGE_CACHE;249vma->vm_page_prot = __pgprot(prot);250251/* Write-combine setting is ignored */252if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,253vma->vm_end - vma->vm_start,254vma->vm_page_prot))255return -EAGAIN;256257return 0;258}259260261