2010-08-05 11:53:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2010 Stefan Lankes, Chair for Operating Systems,
|
|
|
|
* RWTH Aachen University
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* This file is part of MetalSVM.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <metalsvm/stdio.h>
|
|
|
|
#include <metalsvm/string.h>
|
2010-11-27 17:28:32 +00:00
|
|
|
#include <metalsvm/errno.h>
|
2010-08-05 11:53:02 +00:00
|
|
|
#include <asm/irqflags.h>
|
|
|
|
#include <asm/io.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_PCI
|
|
|
|
|
|
|
|
#include <asm/pci.h>
|
|
|
|
#include "pcihdr.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PCI configuration registers
|
|
|
|
*/
|
|
|
|
#define PCI_CFID 0x00 /* Configuration ID */
|
|
|
|
#define PCI_CFCS 0x04 /* Configurtion Command/Status */
|
|
|
|
#define PCI_CFRV 0x08 /* Configuration Revision */
|
|
|
|
#define PCI_CFLT 0x0c /* Configuration Latency Timer */
|
|
|
|
#define PCI_CBIO 0x10 /* Configuration Base IO Address */
|
|
|
|
#define PCI_CFIT 0x3c /* Configuration Interrupt */
|
|
|
|
#define PCI_CFDA 0x40 /* Configuration Driver Area */
|
|
|
|
|
|
|
|
#define PHYS_IO_MEM_START 0
|
|
|
|
#define PCI_MEM 0
|
|
|
|
#define PCI_INTA 0
|
2010-11-27 17:28:32 +00:00
|
|
|
#define PCI_NSLOTS 22
|
|
|
|
#define PCI_NBUS 0
|
2010-08-05 11:53:02 +00:00
|
|
|
|
2010-11-27 17:28:32 +00:00
|
|
|
#define PCI_CONF_ADDR_REG 0xcf8
|
|
|
|
#define PCI_CONF_FRWD_REG 0xcf8
|
|
|
|
#define PCI_CONF_DATA_REG 0xcfc
|
2010-08-05 11:53:02 +00:00
|
|
|
|
2010-11-27 17:28:32 +00:00
|
|
|
#define PCI_IO_CONF_START 0xc000
|
2010-08-05 11:53:02 +00:00
|
|
|
|
2010-11-27 17:28:32 +00:00
|
|
|
#define MAX_BUS 16
|
|
|
|
#define MAX_SLOTS 32
|
2010-08-05 11:53:02 +00:00
|
|
|
|
2010-11-27 17:28:32 +00:00
|
|
|
static uint32_t mechanism = 0;
|
|
|
|
static uint32_t adapters[MAX_BUS][MAX_SLOTS] = {[0 ... MAX_BUS-1][0 ... MAX_SLOTS-1] = -1};
|
2010-08-05 11:53:02 +00:00
|
|
|
|
2010-11-27 17:28:32 +00:00
|
|
|
static void pci_conf_write(uint32_t bus, uint32_t slot, uint32_t off, uint32_t val)
|
2010-08-05 11:53:02 +00:00
|
|
|
{
|
|
|
|
if (mechanism == 1) {
|
|
|
|
outportl(PCI_CONF_FRWD_REG, bus);
|
|
|
|
outportl(PCI_CONF_ADDR_REG, 0xf0);
|
|
|
|
outportl(PCI_IO_CONF_START | (slot << 8) | off, val);
|
|
|
|
} else {
|
|
|
|
outportl(PCI_CONF_ADDR_REG,
|
|
|
|
(0x80000000 | (bus << 16) | (slot << 11) | off));
|
|
|
|
outportl(PCI_CONF_DATA_REG, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-27 17:28:32 +00:00
|
|
|
static uint32_t pci_conf_read(uint32_t bus, uint32_t slot, uint32_t off)
|
2010-08-05 11:53:02 +00:00
|
|
|
{
|
2010-11-27 17:28:32 +00:00
|
|
|
uint32_t data = -1;
|
2010-08-05 11:53:02 +00:00
|
|
|
|
|
|
|
outportl(PCI_CONF_ADDR_REG,
|
|
|
|
(0x80000000 | (bus << 16) | (slot << 11) | off));
|
|
|
|
data = inportl(PCI_CONF_DATA_REG);
|
|
|
|
|
|
|
|
if ((data == 0xffffffff) && (slot < 0x10)) {
|
|
|
|
outportl(PCI_CONF_FRWD_REG, bus);
|
|
|
|
outportl(PCI_CONF_ADDR_REG, 0xf0);
|
|
|
|
data = inportl(PCI_IO_CONF_START | (slot << 8) | off);
|
|
|
|
if (data == 0xffffffff)
|
2010-11-27 17:28:32 +00:00
|
|
|
return data;
|
2010-08-05 11:53:02 +00:00
|
|
|
if (!mechanism)
|
|
|
|
mechanism = 1;
|
|
|
|
} else if (!mechanism)
|
|
|
|
mechanism = 2;
|
|
|
|
|
2010-11-27 17:28:32 +00:00
|
|
|
return data;
|
2010-08-05 11:53:02 +00:00
|
|
|
}
|
|
|
|
|
2010-11-27 17:28:32 +00:00
|
|
|
static inline uint32_t pci_what_irq(uint32_t bus, uint32_t slot)
|
2010-08-05 11:53:02 +00:00
|
|
|
{
|
2010-11-29 02:36:40 +00:00
|
|
|
return pci_conf_read(bus, slot, PCI_CFIT) & 0xFF;
|
|
|
|
}
|
|
|
|
|
2012-08-01 09:50:07 +02:00
|
|
|
static inline uint32_t pci_what_iobase(uint32_t bus, uint32_t slot, uint32_t nr)
|
2010-11-29 02:36:40 +00:00
|
|
|
{
|
2012-08-01 09:50:07 +02:00
|
|
|
return pci_conf_read(bus, slot, PCI_CBIO + nr*4) & 0xFFFFFFFC;
|
2010-08-05 11:53:02 +00:00
|
|
|
}
|
|
|
|
|
2012-08-01 09:50:07 +02:00
|
|
|
static inline uint32_t pci_what_size(uint32_t bus, uint32_t slot, uint32_t nr)
|
2012-07-26 09:07:20 +02:00
|
|
|
{
|
|
|
|
uint32_t tmp, ret;
|
|
|
|
|
|
|
|
// backup the original value
|
2012-08-01 09:50:07 +02:00
|
|
|
tmp = pci_conf_read(bus, slot, PCI_CBIO + nr*4);
|
2012-07-26 09:07:20 +02:00
|
|
|
|
|
|
|
// determine size
|
2012-08-01 09:50:07 +02:00
|
|
|
pci_conf_write(bus, slot, PCI_CBIO + nr*4, 0xFFFFFFFF);
|
|
|
|
ret = ~pci_conf_read(bus, slot, PCI_CBIO + nr*4) + 1;
|
2012-07-26 09:07:20 +02:00
|
|
|
|
|
|
|
// restore original value
|
2012-08-01 09:50:07 +02:00
|
|
|
pci_conf_write(bus, slot, PCI_CBIO + nr*4, tmp);
|
2012-07-26 09:07:20 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-11-27 17:28:32 +00:00
|
|
|
int pci_init(void)
|
2010-08-05 11:53:02 +00:00
|
|
|
{
|
2010-11-27 17:28:32 +00:00
|
|
|
uint32_t slot, bus;
|
2010-08-05 11:53:02 +00:00
|
|
|
|
|
|
|
for (bus = 0; bus < MAX_BUS; bus++)
|
|
|
|
for (slot = 0; slot < MAX_SLOTS; slot++)
|
|
|
|
adapters[bus][slot] = pci_conf_read(bus, slot, PCI_CFID);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-26 09:07:20 +02:00
|
|
|
int pci_get_device_info(uint32_t vendor_id, uint32_t device_id, pci_info_t* info)
|
2010-08-05 11:53:02 +00:00
|
|
|
{
|
2012-08-01 09:50:07 +02:00
|
|
|
uint32_t slot, bus, i;
|
2010-11-27 17:28:32 +00:00
|
|
|
|
2012-07-26 09:07:20 +02:00
|
|
|
if (!info)
|
2010-11-27 17:28:32 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!mechanism)
|
|
|
|
pci_init();
|
|
|
|
|
|
|
|
for (bus = 0; bus < MAX_BUS; bus++) {
|
|
|
|
for (slot = 0; slot < MAX_SLOTS; slot++) {
|
|
|
|
if (adapters[bus][slot] != -1) {
|
|
|
|
if (((adapters[bus][slot] & 0xffff) == vendor_id) &&
|
|
|
|
(((adapters[bus][slot] & 0xffff0000) >> 16) == device_id)) {
|
2012-08-01 09:50:07 +02:00
|
|
|
for(i=0; i<6; i++) {
|
|
|
|
info->base[i] = pci_what_iobase(bus, slot, i);
|
|
|
|
info->size[i] = (info->base[i]) ? pci_what_size(bus, slot, i) : 0;
|
|
|
|
}
|
2012-07-26 09:07:20 +02:00
|
|
|
info->irq = pci_what_irq(bus, slot);
|
2010-11-27 17:28:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
2010-08-05 11:53:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int print_pci_adapters(void)
|
|
|
|
{
|
2010-11-27 17:28:32 +00:00
|
|
|
uint32_t slot, bus;
|
|
|
|
uint32_t i, counter = 0;
|
2010-08-05 11:53:02 +00:00
|
|
|
|
2010-11-27 17:28:32 +00:00
|
|
|
if (!mechanism)
|
|
|
|
pci_init();
|
2010-08-05 11:53:02 +00:00
|
|
|
|
|
|
|
for (bus = 0; bus < MAX_BUS; bus++) {
|
|
|
|
for (slot = 0; slot < MAX_SLOTS; slot++) {
|
|
|
|
|
|
|
|
if (adapters[bus][slot] != -1) {
|
|
|
|
counter++;
|
|
|
|
kprintf("%d) Vendor ID: 0x%x Device Id: 0x%x\n",
|
|
|
|
counter, adapters[bus][slot] & 0xffff,
|
|
|
|
(adapters[bus][slot] & 0xffff0000) >> 16);
|
|
|
|
|
2010-11-27 17:28:32 +00:00
|
|
|
for (i=0; i<PCI_VENTABLE_LEN; i++) {
|
2010-08-05 11:53:02 +00:00
|
|
|
if ((adapters[bus][slot] & 0xffff) ==
|
2010-11-27 17:28:32 +00:00
|
|
|
(uint32_t)PciVenTable[i].VenId)
|
2010-08-05 11:53:02 +00:00
|
|
|
kprintf("\tVendor is %s\n",
|
|
|
|
PciVenTable[i].VenShort);
|
|
|
|
}
|
|
|
|
|
2010-11-27 17:28:32 +00:00
|
|
|
for (i=0; i<PCI_DEVTABLE_LEN; i++) {
|
2010-08-05 11:53:02 +00:00
|
|
|
if ((adapters[bus][slot] & 0xffff) ==
|
2010-11-27 17:28:32 +00:00
|
|
|
(uint32_t)PciDevTable[i].VenId) {
|
2010-08-05 11:53:02 +00:00
|
|
|
if (((adapters[bus][slot] & 0xffff0000) >> 16) ==
|
|
|
|
PciDevTable[i].DevId) {
|
|
|
|
kprintf
|
|
|
|
("\tChip: %s ChipDesc: %s\n",
|
|
|
|
PciDevTable[i].Chip,
|
|
|
|
PciDevTable[i].ChipDesc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|