2 * Faraday FTPCI100 PCI Bridge Controller Device Driver Implementation
4 * Copyright (C) 2011 Andes Technology Corporation
5 * Gavin Guo, Andes Technology Corporation <gavinguo@andestech.com>
6 * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <asm/types.h> /* u32, u16.... used by pci.h */
29 #include "pci_ftpci100.h"
31 struct ftpci100_data {
32 unsigned int reg_base;
34 unsigned int mem_base;
35 unsigned int mmio_base;
39 static struct pci_config devs[FTPCI100_MAX_FUNCTIONS];
40 static struct pci_controller local_hose;
42 static void setup_pci_bar(unsigned int bus, unsigned int dev, unsigned func,
43 unsigned char header, struct ftpci100_data *priv)
45 struct pci_controller *hose = (struct pci_controller *)&local_hose;
46 unsigned int i, tmp32, bar_no, iovsmem = 1;
49 /* A device is present, add an entry to the array */
50 devs[priv->ndevs].bus = bus;
51 devs[priv->ndevs].dev = dev;
52 devs[priv->ndevs].func = func;
54 dev_nu = PCI_BDF(bus, dev, func);
56 if ((header & 0x7f) == 0x01)
62 /* Allocate address spaces by configuring BARs */
63 for (i = 0; i < bar_no; i++) {
64 pci_hose_write_config_dword(hose, dev_nu,
65 PCI_BASE_ADDRESS_0 + i * 4, 0xffffffff);
66 pci_hose_read_config_dword(hose, dev_nu,
67 PCI_BASE_ADDRESS_0 + i * 4, &tmp32);
75 unsigned int size_mask = ~(tmp32 & 0xfffffffc);
77 if (priv->io_base & size_mask)
78 priv->io_base = (priv->io_base & ~size_mask) + \
81 devs[priv->ndevs].bar[i].addr = priv->io_base;
82 devs[priv->ndevs].bar[i].size = size_mask + 1;
84 pci_hose_write_config_dword(hose, dev_nu,
85 PCI_BASE_ADDRESS_0 + i * 4,
88 debug("Allocated IO address 0x%X-" \
89 "0x%X for Bus %d, Device %d, Function %d\n",
91 priv->io_base + size_mask, bus, dev, func);
93 priv->io_base += size_mask + 1;
96 unsigned int is_64bit = ((tmp32 & 0x6) == 0x4);
97 unsigned int is_pref = tmp32 & 0x8;
98 unsigned int size_mask = ~(tmp32 & 0xfffffff0);
99 unsigned int alloc_base;
100 unsigned int *addr_mem_base;
103 addr_mem_base = &priv->mem_base;
105 addr_mem_base = &priv->mmio_base;
107 alloc_base = *addr_mem_base;
109 if (alloc_base & size_mask)
110 alloc_base = (alloc_base & ~size_mask) \
113 pci_hose_write_config_dword(hose, dev_nu,
114 PCI_BASE_ADDRESS_0 + i * 4, alloc_base);
116 debug("Allocated %s address 0x%X-" \
117 "0x%X for Bus %d, Device %d, Function %d\n",
118 is_pref ? "MEM" : "MMIO", alloc_base,
119 alloc_base + size_mask, bus, dev, func);
121 devs[priv->ndevs].bar[i].addr = alloc_base;
122 devs[priv->ndevs].bar[i].size = size_mask + 1;
124 debug("BAR address BAR size\n");
125 debug("%010x %08d\n",
126 devs[priv->ndevs].bar[0].addr,
127 devs[priv->ndevs].bar[0].size);
129 alloc_base += size_mask + 1;
130 *addr_mem_base = alloc_base;
134 pci_hose_write_config_dword(hose, dev_nu,
135 PCI_BASE_ADDRESS_0 + i * 4, 0x0);
140 /* Enable Bus Master, Memory Space, and IO Space */
141 pci_hose_read_config_dword(hose, dev_nu, PCI_CACHE_LINE_SIZE, &tmp32);
142 pci_hose_write_config_dword(hose, dev_nu, PCI_CACHE_LINE_SIZE, 0x08);
143 pci_hose_read_config_dword(hose, dev_nu, PCI_CACHE_LINE_SIZE, &tmp32);
145 pci_hose_read_config_dword(hose, dev_nu, PCI_COMMAND, &tmp32);
154 pci_hose_write_config_dword(hose, dev_nu, PCI_COMMAND, tmp32);
157 static void pci_bus_scan(struct ftpci100_data *priv)
159 struct pci_controller *hose = (struct pci_controller *)&local_hose;
160 unsigned int bus, dev, func;
164 unsigned char header;
165 unsigned char int_pin;
166 unsigned int niobars;
174 for (bus = 0; bus < MAX_BUS_NUM; bus++)
175 for (dev = 0; dev < MAX_DEV_NUM; dev++)
176 for (func = 0; func < MAX_FUN_NUM; func++) {
177 dev_nu = PCI_BDF(bus, dev, func);
178 pci_hose_read_config_dword(hose, dev_nu,
179 PCI_VENDOR_ID, &data32);
182 * some broken boards return 0 or ~0,
183 * if a slot is empty.
185 if (data32 == 0xffffffff ||
186 data32 == 0x00000000 ||
187 data32 == 0x0000ffff ||
188 data32 == 0xffff0000)
191 pci_hose_read_config_dword(hose, dev_nu,
192 PCI_HEADER_TYPE, &tmp);
193 header = (unsigned char)tmp;
194 setup_pci_bar(bus, dev, func, header, priv);
196 devs[priv->ndevs].v_id = (u16)(data32 & \
199 devs[priv->ndevs].d_id = (u16)((data32 & \
202 /* Figure out what INTX# line the card uses */
203 pci_hose_read_config_byte(hose, dev_nu,
204 PCI_INTERRUPT_PIN, &int_pin);
206 /* assign the appropriate irq line */
207 if (int_pin > PCI_IRQ_LINES) {
208 printf("more irq lines than expect\n");
209 } else if (int_pin != 0) {
210 /* This device uses an interrupt line */
211 devs[priv->ndevs].pin = int_pin;
214 pci_hose_read_config_dword(hose, dev_nu,
215 PCI_CLASS_DEVICE, &data32);
217 debug("%06d %03d %03d " \
219 "%03d %08x %06d %08x\n",
220 priv->ndevs, devs[priv->ndevs].bus,
221 devs[priv->ndevs].dev,
222 devs[priv->ndevs].func,
223 devs[priv->ndevs].d_id,
224 devs[priv->ndevs].v_id,
225 devs[priv->ndevs].pin,
226 devs[priv->ndevs].bar[0].addr,
227 devs[priv->ndevs].bar[0].size,
234 static void ftpci_preinit(struct ftpci100_data *priv)
236 struct ftpci100_ahbc *ftpci100;
237 struct pci_controller *hose = (struct pci_controller *)&local_hose;
241 priv->reg_base = CONFIG_FTPCI100_BASE;
242 priv->io_base = CONFIG_FTPCI100_BASE + CONFIG_FTPCI100_IO_SIZE;
243 priv->mmio_base = CONFIG_FTPCI100_MEM_BASE;
244 priv->mem_base = CONFIG_FTPCI100_MEM_BASE + CONFIG_FTPCI100_MEM_SIZE;
246 ftpci100 = (struct ftpci100_ahbc *)priv->reg_base;
248 pci_config_addr = (u32) &ftpci100->conf;
249 pci_config_data = (u32) &ftpci100->data;
251 /* print device name */
252 printf("FTPCI100\n");
254 /* dump basic configuration */
255 debug("%s: Config addr is %08X, data port is %08X\n",
256 __func__, pci_config_addr, pci_config_data);
258 /* PCI memory space */
259 pci_set_region(hose->regions + 0,
264 hose->region_count++;
267 pci_set_region(hose->regions + 1,
272 hose->region_count++;
274 #if defined(CONFIG_PCI_SYS_BUS)
275 /* PCI System Memory space */
276 pci_set_region(hose->regions + 2,
280 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
281 hose->region_count++;
284 /* setup indirect read/write function */
285 pci_setup_indirect(hose, pci_config_addr, pci_config_data);
288 pci_register_hose(hose);
291 void pci_ftpci_init(void)
293 struct ftpci100_data *priv = NULL;
294 struct pci_controller *hose = (struct pci_controller *)&local_hose;
295 pci_dev_t bridge_num;
297 struct pci_device_id bridge_ids[] = {
298 {FTPCI100_BRIDGE_VENDORID, FTPCI100_BRIDGE_DEVICEID},
302 priv = malloc(sizeof(struct ftpci100_data));
305 printf("%s(): failed to malloc priv\n", __func__);
309 memset(priv, 0, sizeof(struct ftpci100_data));
313 debug("Device bus dev func deviceID vendorID pin address" \
319 * Setup the PCI Bridge Window to 1GB,
320 * it will cause USB OHCI Host controller Unrecoverable Error
323 bridge_num = pci_find_devices(bridge_ids, 0);
324 if (bridge_num == -1) {
325 printf("PCI Bridge not found\n");
328 pci_hose_write_config_dword(hose, bridge_num, PCI_MEM_BASE_SIZE1,
329 FTPCI100_BASE_ADR_SIZE(1024));