2c7767e50dd35c091e152e23c5af48ffde8e5ae3
[coreboot.git] / src / drivers / oxford / oxpcie / oxpcie_early.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2011 Google Inc
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include <stdint.h>
21 #include <arch/io.h>
22 #include <arch/romcc_io.h>
23 #include <uart8250.h>
24 #include <device/pci_def.h>
25
26 #define PCIE_BRIDGE \
27         PCI_DEV(CONFIG_OXFORD_OXPCIE_BRIDGE_BUS, \
28                 CONFIG_OXFORD_OXPCIE_BRIDGE_DEVICE, \
29                 CONFIG_OXFORD_OXPCIE_BRIDGE_FUNCTION)
30
31 #define OXPCIE_DEVICE \
32         PCI_DEV(CONFIG_OXFORD_OXPCIE_BRIDGE_SUBORDINATE, 0, 0)
33
34 #define OXPCIE_DEVICE_3 \
35         PCI_DEV(CONFIG_OXFORD_OXPCIE_BRIDGE_SUBORDINATE, 0, 3)
36
37 void oxford_init(void)
38 {
39         u16 reg16;
40
41         /* First we reset the secondary bus */
42         reg16 = pci_read_config16(PCIE_BRIDGE, PCI_BRIDGE_CONTROL);
43         reg16 |= (1 << 6); /* SRESET */
44         pci_write_config16(PCIE_BRIDGE, PCI_BRIDGE_CONTROL, reg16);
45
46         /* Assume we don't have to wait here forever */
47
48         /* Read back and clear reset bit. */
49         reg16 = pci_read_config16(PCIE_BRIDGE, PCI_BRIDGE_CONTROL);
50         reg16 &= ~(1 << 6); /* SRESET */
51         pci_write_config16(PCIE_BRIDGE, PCI_BRIDGE_CONTROL, reg16);
52
53         /* Set up subordinate bus number */
54         pci_write_config8(PCIE_BRIDGE, PCI_SECONDARY_BUS, 0x00);
55         pci_write_config8(PCIE_BRIDGE, PCI_SUBORDINATE_BUS, 0x00);
56         pci_write_config8(PCIE_BRIDGE, PCI_SECONDARY_BUS,
57                         CONFIG_OXFORD_OXPCIE_BRIDGE_SUBORDINATE);
58         pci_write_config8(PCIE_BRIDGE, PCI_SUBORDINATE_BUS,
59                         CONFIG_OXFORD_OXPCIE_BRIDGE_SUBORDINATE);
60
61         /* Memory window for the OXPCIe952 card */
62         // XXX is the calculation of base and limit corect?
63         pci_write_config32(PCIE_BRIDGE, PCI_MEMORY_BASE,
64                         ((CONFIG_OXFORD_OXPCIE_BASE_ADDRESS & 0xffff0000) |
65                         ((CONFIG_OXFORD_OXPCIE_BASE_ADDRESS >> 16) & 0xff00)));
66
67         /* Enable memory access through bridge */
68         reg16 = pci_read_config16(PCIE_BRIDGE, PCI_COMMAND);
69         reg16 |= PCI_COMMAND_MEMORY;
70         pci_write_config16(PCIE_BRIDGE, PCI_COMMAND, reg16);
71
72         // FIXME Add a timeout or this will hang forever if
73         // no device is in the slot.
74         u32 id = 0;
75         while ((id == 0) || (id == 0xffffffff))
76                 id = pci_read_config32(OXPCIE_DEVICE, PCI_VENDOR_ID);
77
78         u32 device = OXPCIE_DEVICE; /* unknown default */
79         switch (id) {
80         case 0xc1181415: /* e.g. Startech PEX1S1PMINI */
81                 /* On this device function 0 is the parallel port, and
82                  * function 3 is the serial port. So let's go look for
83                  * the UART.
84                  */
85                 id = pci_read_config32(OXPCIE_DEVICE_3, PCI_VENDOR_ID);
86                 if (id != 0xc11b1415)
87                         return;
88                 device = OXPCIE_DEVICE_3;
89                 break;
90         case 0xc1581415: /* e.g. Startech MPEX2S952 */
91                 device = OXPCIE_DEVICE;
92                 break;
93         }
94
95         /* Setup base address on device */
96         pci_write_config32(device, PCI_BASE_ADDRESS_0,
97                                 CONFIG_OXFORD_OXPCIE_BASE_ADDRESS);
98
99         /* Enable memory on device */
100         reg16 = pci_read_config16(device, PCI_COMMAND);
101         reg16 |= PCI_COMMAND_MEMORY;
102         pci_write_config16(device, PCI_COMMAND, reg16);
103
104         /* Now the UART initialization */
105         u32 uart0_base = CONFIG_OXFORD_OXPCIE_BASE_ADDRESS + 0x1000;
106
107         uart8250_mem_init(uart0_base, (4000000 / CONFIG_TTYS0_BAUD));
108 }
109