/* bonito.c - PCI bonito interface. */ /* * VasEBoot -- GRand Unified Bootloader * Copyright (C) 2009 Free Software Foundation, Inc. * * VasEBoot is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VasEBoot is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with VasEBoot. If not, see . */ #include #include static VasEBoot_uint32_t base_win[VasEBoot_MACHINE_PCI_NUM_WIN]; static const VasEBoot_size_t sizes_win[VasEBoot_MACHINE_PCI_NUM_WIN] = {VasEBoot_MACHINE_PCI_WIN1_SIZE, VasEBoot_MACHINE_PCI_WIN_SIZE, VasEBoot_MACHINE_PCI_WIN_SIZE}; /* Usage counters. */ static int usage_win[VasEBoot_MACHINE_PCI_NUM_WIN]; static VasEBoot_addr_t addr_win[VasEBoot_MACHINE_PCI_NUM_WIN] = {VasEBoot_MACHINE_PCI_WIN1_ADDR, VasEBoot_MACHINE_PCI_WIN2_ADDR, VasEBoot_MACHINE_PCI_WIN3_ADDR}; VasEBoot_bonito_type_t VasEBoot_bonito_type; static volatile void * config_addr (VasEBoot_pci_address_t addr) { if (VasEBoot_bonito_type == VasEBoot_BONITO_2F) { VasEBoot_MACHINE_PCI_CONF_CTRL_REG_2F = 1 << ((addr >> 11) & 0xf); return (volatile void *) (VasEBoot_MACHINE_PCI_CONFSPACE_2F | (addr & 0x07ff)); } else { if (addr >> 16) return (volatile void *) (VasEBoot_MACHINE_PCI_CONFSPACE_3A_EXT | addr); else return (volatile void *) (VasEBoot_MACHINE_PCI_CONFSPACE_3A | addr); } } VasEBoot_uint32_t VasEBoot_pci_read (VasEBoot_pci_address_t addr) { return *(volatile VasEBoot_uint32_t *) config_addr (addr); } VasEBoot_uint16_t VasEBoot_pci_read_word (VasEBoot_pci_address_t addr) { return *(volatile VasEBoot_uint16_t *) config_addr (addr); } VasEBoot_uint8_t VasEBoot_pci_read_byte (VasEBoot_pci_address_t addr) { return *(volatile VasEBoot_uint8_t *) config_addr (addr); } void VasEBoot_pci_write (VasEBoot_pci_address_t addr, VasEBoot_uint32_t data) { *(volatile VasEBoot_uint32_t *) config_addr (addr) = data; } void VasEBoot_pci_write_word (VasEBoot_pci_address_t addr, VasEBoot_uint16_t data) { *(volatile VasEBoot_uint16_t *) config_addr (addr) = data; } void VasEBoot_pci_write_byte (VasEBoot_pci_address_t addr, VasEBoot_uint8_t data) { *(volatile VasEBoot_uint8_t *) config_addr (addr) = data; } static inline void write_bases_2f (void) { int i; VasEBoot_uint32_t reg = 0; for (i = 0; i < VasEBoot_MACHINE_PCI_NUM_WIN; i++) reg |= (((base_win[i] >> VasEBoot_MACHINE_PCI_WIN_SHIFT) & VasEBoot_MACHINE_PCI_WIN_MASK) << (i * VasEBoot_MACHINE_PCI_WIN_MASK_SIZE)); VasEBoot_MACHINE_PCI_IO_CTRL_REG_2F = reg; } volatile void * VasEBoot_pci_device_map_range (VasEBoot_pci_device_t dev __attribute__ ((unused)), VasEBoot_addr_t base, VasEBoot_size_t size) { if (VasEBoot_bonito_type == VasEBoot_BONITO_2F) { int i; VasEBoot_addr_t newbase; /* First try already used registers. */ for (i = 0; i < VasEBoot_MACHINE_PCI_NUM_WIN; i++) if (usage_win[i] && base_win[i] <= base && base_win[i] + sizes_win[i] > base + size) { usage_win[i]++; return (void *) (addr_win[i] | (base & VasEBoot_MACHINE_PCI_WIN_OFFSET_MASK)); } /* Map new register. */ newbase = base & ~VasEBoot_MACHINE_PCI_WIN_OFFSET_MASK; for (i = 0; i < VasEBoot_MACHINE_PCI_NUM_WIN; i++) if (!usage_win[i] && newbase <= base && newbase + sizes_win[i] > base + size) { usage_win[i]++; base_win[i] = newbase; write_bases_2f (); return (void *) (addr_win[i] | (base & VasEBoot_MACHINE_PCI_WIN_OFFSET_MASK)); } VasEBoot_fatal ("Out of PCI windows."); } else { int region = 0; if (base >= 0x10000000 && base + size <= 0x18000000) region = 1; if (base >= 0x1c000000 && base + size <= 0x1f000000) region = 2; if (region == 0) VasEBoot_fatal ("Attempt to map out of regions"); return (void *) (0xa0000000 | base); } } void * VasEBoot_pci_device_map_range_cached (VasEBoot_pci_device_t dev, VasEBoot_addr_t base, VasEBoot_size_t size) { return (void *) (((VasEBoot_addr_t) VasEBoot_pci_device_map_range (dev, base, size)) & ~0x20000000); } void VasEBoot_pci_device_unmap_range (VasEBoot_pci_device_t dev __attribute__ ((unused)), volatile void *mem, VasEBoot_size_t size __attribute__ ((unused))) { if (VasEBoot_bonito_type == VasEBoot_BONITO_2F) { int i; for (i = 0; i < VasEBoot_MACHINE_PCI_NUM_WIN; i++) if (usage_win[i] && addr_win[i] == (((VasEBoot_addr_t) mem | 0x20000000) & ~VasEBoot_MACHINE_PCI_WIN_OFFSET_MASK)) { usage_win[i]--; return; } VasEBoot_fatal ("Tried to unmap not mapped region"); } }