1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2014e4678SGavin Guo /*
3014e4678SGavin Guo * Faraday FTPCI100 PCI Bridge Controller Device Driver Implementation
4014e4678SGavin Guo *
5014e4678SGavin Guo * Copyright (C) 2011 Andes Technology Corporation
6014e4678SGavin Guo * Gavin Guo, Andes Technology Corporation <gavinguo@andestech.com>
7014e4678SGavin Guo * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
8014e4678SGavin Guo */
9014e4678SGavin Guo #include <common.h>
10014e4678SGavin Guo #include <malloc.h>
11014e4678SGavin Guo #include <pci.h>
12014e4678SGavin Guo
138599515fSGabor Juhos #include <faraday/ftpci100.h>
148599515fSGabor Juhos
15014e4678SGavin Guo #include <asm/io.h>
16014e4678SGavin Guo #include <asm/types.h> /* u32, u16.... used by pci.h */
17014e4678SGavin Guo
18014e4678SGavin Guo struct ftpci100_data {
19014e4678SGavin Guo unsigned int reg_base;
20014e4678SGavin Guo unsigned int io_base;
21014e4678SGavin Guo unsigned int mem_base;
22014e4678SGavin Guo unsigned int mmio_base;
23014e4678SGavin Guo unsigned int ndevs;
24014e4678SGavin Guo };
25014e4678SGavin Guo
26014e4678SGavin Guo static struct pci_config devs[FTPCI100_MAX_FUNCTIONS];
27014e4678SGavin Guo static struct pci_controller local_hose;
28014e4678SGavin Guo
setup_pci_bar(unsigned int bus,unsigned int dev,unsigned func,unsigned char header,struct ftpci100_data * priv)29014e4678SGavin Guo static void setup_pci_bar(unsigned int bus, unsigned int dev, unsigned func,
30014e4678SGavin Guo unsigned char header, struct ftpci100_data *priv)
31014e4678SGavin Guo {
32014e4678SGavin Guo struct pci_controller *hose = (struct pci_controller *)&local_hose;
33014e4678SGavin Guo unsigned int i, tmp32, bar_no, iovsmem = 1;
34014e4678SGavin Guo pci_dev_t dev_nu;
35014e4678SGavin Guo
36014e4678SGavin Guo /* A device is present, add an entry to the array */
37014e4678SGavin Guo devs[priv->ndevs].bus = bus;
38014e4678SGavin Guo devs[priv->ndevs].dev = dev;
39014e4678SGavin Guo devs[priv->ndevs].func = func;
40014e4678SGavin Guo
41014e4678SGavin Guo dev_nu = PCI_BDF(bus, dev, func);
42014e4678SGavin Guo
43014e4678SGavin Guo if ((header & 0x7f) == 0x01)
44014e4678SGavin Guo /* PCI-PCI Bridge */
45014e4678SGavin Guo bar_no = 2;
46014e4678SGavin Guo else
47014e4678SGavin Guo bar_no = 6;
48014e4678SGavin Guo
49014e4678SGavin Guo /* Allocate address spaces by configuring BARs */
50014e4678SGavin Guo for (i = 0; i < bar_no; i++) {
51014e4678SGavin Guo pci_hose_write_config_dword(hose, dev_nu,
52014e4678SGavin Guo PCI_BASE_ADDRESS_0 + i * 4, 0xffffffff);
53014e4678SGavin Guo pci_hose_read_config_dword(hose, dev_nu,
54014e4678SGavin Guo PCI_BASE_ADDRESS_0 + i * 4, &tmp32);
55014e4678SGavin Guo
56014e4678SGavin Guo if (tmp32 == 0x0)
57014e4678SGavin Guo continue;
58014e4678SGavin Guo
59014e4678SGavin Guo /* IO space */
60014e4678SGavin Guo if (tmp32 & 0x1) {
61014e4678SGavin Guo iovsmem = 0;
62014e4678SGavin Guo unsigned int size_mask = ~(tmp32 & 0xfffffffc);
63014e4678SGavin Guo
64014e4678SGavin Guo if (priv->io_base & size_mask)
65014e4678SGavin Guo priv->io_base = (priv->io_base & ~size_mask) + \
66014e4678SGavin Guo size_mask + 1;
67014e4678SGavin Guo
68014e4678SGavin Guo devs[priv->ndevs].bar[i].addr = priv->io_base;
69014e4678SGavin Guo devs[priv->ndevs].bar[i].size = size_mask + 1;
70014e4678SGavin Guo
71014e4678SGavin Guo pci_hose_write_config_dword(hose, dev_nu,
72014e4678SGavin Guo PCI_BASE_ADDRESS_0 + i * 4,
73014e4678SGavin Guo priv->io_base);
74014e4678SGavin Guo
75014e4678SGavin Guo debug("Allocated IO address 0x%X-" \
76014e4678SGavin Guo "0x%X for Bus %d, Device %d, Function %d\n",
77014e4678SGavin Guo priv->io_base,
78014e4678SGavin Guo priv->io_base + size_mask, bus, dev, func);
79014e4678SGavin Guo
80014e4678SGavin Guo priv->io_base += size_mask + 1;
81014e4678SGavin Guo } else {
82014e4678SGavin Guo /* Memory space */
83014e4678SGavin Guo unsigned int is_64bit = ((tmp32 & 0x6) == 0x4);
84014e4678SGavin Guo unsigned int is_pref = tmp32 & 0x8;
85014e4678SGavin Guo unsigned int size_mask = ~(tmp32 & 0xfffffff0);
86014e4678SGavin Guo unsigned int alloc_base;
87014e4678SGavin Guo unsigned int *addr_mem_base;
88014e4678SGavin Guo
89014e4678SGavin Guo if (is_pref)
90014e4678SGavin Guo addr_mem_base = &priv->mem_base;
91014e4678SGavin Guo else
92014e4678SGavin Guo addr_mem_base = &priv->mmio_base;
93014e4678SGavin Guo
94014e4678SGavin Guo alloc_base = *addr_mem_base;
95014e4678SGavin Guo
96014e4678SGavin Guo if (alloc_base & size_mask)
97014e4678SGavin Guo alloc_base = (alloc_base & ~size_mask) \
98014e4678SGavin Guo + size_mask + 1;
99014e4678SGavin Guo
100014e4678SGavin Guo pci_hose_write_config_dword(hose, dev_nu,
101014e4678SGavin Guo PCI_BASE_ADDRESS_0 + i * 4, alloc_base);
102014e4678SGavin Guo
103014e4678SGavin Guo debug("Allocated %s address 0x%X-" \
104014e4678SGavin Guo "0x%X for Bus %d, Device %d, Function %d\n",
105014e4678SGavin Guo is_pref ? "MEM" : "MMIO", alloc_base,
106014e4678SGavin Guo alloc_base + size_mask, bus, dev, func);
107014e4678SGavin Guo
108014e4678SGavin Guo devs[priv->ndevs].bar[i].addr = alloc_base;
109014e4678SGavin Guo devs[priv->ndevs].bar[i].size = size_mask + 1;
110014e4678SGavin Guo
111014e4678SGavin Guo debug("BAR address BAR size\n");
112014e4678SGavin Guo debug("%010x %08d\n",
113014e4678SGavin Guo devs[priv->ndevs].bar[0].addr,
114014e4678SGavin Guo devs[priv->ndevs].bar[0].size);
115014e4678SGavin Guo
116014e4678SGavin Guo alloc_base += size_mask + 1;
117014e4678SGavin Guo *addr_mem_base = alloc_base;
118014e4678SGavin Guo
119014e4678SGavin Guo if (is_64bit) {
120014e4678SGavin Guo i++;
121014e4678SGavin Guo pci_hose_write_config_dword(hose, dev_nu,
122014e4678SGavin Guo PCI_BASE_ADDRESS_0 + i * 4, 0x0);
123014e4678SGavin Guo }
124014e4678SGavin Guo }
125014e4678SGavin Guo }
126014e4678SGavin Guo
127014e4678SGavin Guo /* Enable Bus Master, Memory Space, and IO Space */
128014e4678SGavin Guo pci_hose_read_config_dword(hose, dev_nu, PCI_CACHE_LINE_SIZE, &tmp32);
129014e4678SGavin Guo pci_hose_write_config_dword(hose, dev_nu, PCI_CACHE_LINE_SIZE, 0x08);
130014e4678SGavin Guo pci_hose_read_config_dword(hose, dev_nu, PCI_CACHE_LINE_SIZE, &tmp32);
131014e4678SGavin Guo
132014e4678SGavin Guo pci_hose_read_config_dword(hose, dev_nu, PCI_COMMAND, &tmp32);
133014e4678SGavin Guo
134014e4678SGavin Guo tmp32 &= 0xffff;
135014e4678SGavin Guo
136014e4678SGavin Guo if (iovsmem == 0)
137014e4678SGavin Guo tmp32 |= 0x5;
138014e4678SGavin Guo else
139014e4678SGavin Guo tmp32 |= 0x6;
140014e4678SGavin Guo
141014e4678SGavin Guo pci_hose_write_config_dword(hose, dev_nu, PCI_COMMAND, tmp32);
142014e4678SGavin Guo }
143014e4678SGavin Guo
pci_bus_scan(struct ftpci100_data * priv)144014e4678SGavin Guo static void pci_bus_scan(struct ftpci100_data *priv)
145014e4678SGavin Guo {
146014e4678SGavin Guo struct pci_controller *hose = (struct pci_controller *)&local_hose;
147014e4678SGavin Guo unsigned int bus, dev, func;
148014e4678SGavin Guo pci_dev_t dev_nu;
149014e4678SGavin Guo unsigned int data32;
150014e4678SGavin Guo unsigned int tmp;
151014e4678SGavin Guo unsigned char header;
152014e4678SGavin Guo unsigned char int_pin;
153014e4678SGavin Guo unsigned int niobars;
154014e4678SGavin Guo unsigned int nmbars;
155014e4678SGavin Guo
156014e4678SGavin Guo priv->ndevs = 1;
157014e4678SGavin Guo
158014e4678SGavin Guo nmbars = 0;
159014e4678SGavin Guo niobars = 0;
160014e4678SGavin Guo
161014e4678SGavin Guo for (bus = 0; bus < MAX_BUS_NUM; bus++)
162014e4678SGavin Guo for (dev = 0; dev < MAX_DEV_NUM; dev++)
163014e4678SGavin Guo for (func = 0; func < MAX_FUN_NUM; func++) {
164014e4678SGavin Guo dev_nu = PCI_BDF(bus, dev, func);
165014e4678SGavin Guo pci_hose_read_config_dword(hose, dev_nu,
166014e4678SGavin Guo PCI_VENDOR_ID, &data32);
167014e4678SGavin Guo
168014e4678SGavin Guo /*
169014e4678SGavin Guo * some broken boards return 0 or ~0,
170014e4678SGavin Guo * if a slot is empty.
171014e4678SGavin Guo */
172014e4678SGavin Guo if (data32 == 0xffffffff ||
173014e4678SGavin Guo data32 == 0x00000000 ||
174014e4678SGavin Guo data32 == 0x0000ffff ||
175014e4678SGavin Guo data32 == 0xffff0000)
176014e4678SGavin Guo continue;
177014e4678SGavin Guo
178014e4678SGavin Guo pci_hose_read_config_dword(hose, dev_nu,
179014e4678SGavin Guo PCI_HEADER_TYPE, &tmp);
180014e4678SGavin Guo header = (unsigned char)tmp;
181014e4678SGavin Guo setup_pci_bar(bus, dev, func, header, priv);
182014e4678SGavin Guo
183014e4678SGavin Guo devs[priv->ndevs].v_id = (u16)(data32 & \
184014e4678SGavin Guo 0x0000ffff);
185014e4678SGavin Guo
186014e4678SGavin Guo devs[priv->ndevs].d_id = (u16)((data32 & \
187014e4678SGavin Guo 0xffff0000) >> 16);
188014e4678SGavin Guo
189014e4678SGavin Guo /* Figure out what INTX# line the card uses */
190014e4678SGavin Guo pci_hose_read_config_byte(hose, dev_nu,
191014e4678SGavin Guo PCI_INTERRUPT_PIN, &int_pin);
192014e4678SGavin Guo
193014e4678SGavin Guo /* assign the appropriate irq line */
194014e4678SGavin Guo if (int_pin > PCI_IRQ_LINES) {
195014e4678SGavin Guo printf("more irq lines than expect\n");
196014e4678SGavin Guo } else if (int_pin != 0) {
197014e4678SGavin Guo /* This device uses an interrupt line */
198014e4678SGavin Guo devs[priv->ndevs].pin = int_pin;
199014e4678SGavin Guo }
200014e4678SGavin Guo
201014e4678SGavin Guo pci_hose_read_config_dword(hose, dev_nu,
202014e4678SGavin Guo PCI_CLASS_DEVICE, &data32);
203014e4678SGavin Guo
204014e4678SGavin Guo debug("%06d %03d %03d " \
205014e4678SGavin Guo "%04d %08x %08x " \
206014e4678SGavin Guo "%03d %08x %06d %08x\n",
207014e4678SGavin Guo priv->ndevs, devs[priv->ndevs].bus,
208014e4678SGavin Guo devs[priv->ndevs].dev,
209014e4678SGavin Guo devs[priv->ndevs].func,
210014e4678SGavin Guo devs[priv->ndevs].d_id,
211014e4678SGavin Guo devs[priv->ndevs].v_id,
212014e4678SGavin Guo devs[priv->ndevs].pin,
213014e4678SGavin Guo devs[priv->ndevs].bar[0].addr,
214014e4678SGavin Guo devs[priv->ndevs].bar[0].size,
215014e4678SGavin Guo data32 >> 8);
216014e4678SGavin Guo
217014e4678SGavin Guo priv->ndevs++;
218014e4678SGavin Guo }
219014e4678SGavin Guo }
220014e4678SGavin Guo
ftpci_preinit(struct ftpci100_data * priv)221014e4678SGavin Guo static void ftpci_preinit(struct ftpci100_data *priv)
222014e4678SGavin Guo {
223014e4678SGavin Guo struct ftpci100_ahbc *ftpci100;
224014e4678SGavin Guo struct pci_controller *hose = (struct pci_controller *)&local_hose;
225014e4678SGavin Guo u32 pci_config_addr;
226014e4678SGavin Guo u32 pci_config_data;
227014e4678SGavin Guo
228014e4678SGavin Guo priv->reg_base = CONFIG_FTPCI100_BASE;
229014e4678SGavin Guo priv->io_base = CONFIG_FTPCI100_BASE + CONFIG_FTPCI100_IO_SIZE;
230014e4678SGavin Guo priv->mmio_base = CONFIG_FTPCI100_MEM_BASE;
231014e4678SGavin Guo priv->mem_base = CONFIG_FTPCI100_MEM_BASE + CONFIG_FTPCI100_MEM_SIZE;
232014e4678SGavin Guo
233014e4678SGavin Guo ftpci100 = (struct ftpci100_ahbc *)priv->reg_base;
234014e4678SGavin Guo
235014e4678SGavin Guo pci_config_addr = (u32) &ftpci100->conf;
236014e4678SGavin Guo pci_config_data = (u32) &ftpci100->data;
237014e4678SGavin Guo
238014e4678SGavin Guo /* print device name */
239014e4678SGavin Guo printf("FTPCI100\n");
240014e4678SGavin Guo
241014e4678SGavin Guo /* dump basic configuration */
242014e4678SGavin Guo debug("%s: Config addr is %08X, data port is %08X\n",
243014e4678SGavin Guo __func__, pci_config_addr, pci_config_data);
244014e4678SGavin Guo
245014e4678SGavin Guo /* PCI memory space */
246014e4678SGavin Guo pci_set_region(hose->regions + 0,
247014e4678SGavin Guo CONFIG_PCI_MEM_BUS,
248014e4678SGavin Guo CONFIG_PCI_MEM_PHYS,
249014e4678SGavin Guo CONFIG_PCI_MEM_SIZE,
250014e4678SGavin Guo PCI_REGION_MEM);
251014e4678SGavin Guo hose->region_count++;
252014e4678SGavin Guo
253014e4678SGavin Guo /* PCI IO space */
254014e4678SGavin Guo pci_set_region(hose->regions + 1,
255014e4678SGavin Guo CONFIG_PCI_IO_BUS,
256014e4678SGavin Guo CONFIG_PCI_IO_PHYS,
257014e4678SGavin Guo CONFIG_PCI_IO_SIZE,
258014e4678SGavin Guo PCI_REGION_IO);
259014e4678SGavin Guo hose->region_count++;
260014e4678SGavin Guo
261014e4678SGavin Guo #if defined(CONFIG_PCI_SYS_BUS)
262014e4678SGavin Guo /* PCI System Memory space */
263014e4678SGavin Guo pci_set_region(hose->regions + 2,
264014e4678SGavin Guo CONFIG_PCI_SYS_BUS,
265014e4678SGavin Guo CONFIG_PCI_SYS_PHYS,
266014e4678SGavin Guo CONFIG_PCI_SYS_SIZE,
267014e4678SGavin Guo PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
268014e4678SGavin Guo hose->region_count++;
269014e4678SGavin Guo #endif
270014e4678SGavin Guo
271014e4678SGavin Guo /* setup indirect read/write function */
272014e4678SGavin Guo pci_setup_indirect(hose, pci_config_addr, pci_config_data);
273014e4678SGavin Guo
274014e4678SGavin Guo /* register hose */
275014e4678SGavin Guo pci_register_hose(hose);
276014e4678SGavin Guo }
277014e4678SGavin Guo
pci_ftpci_init(void)278014e4678SGavin Guo void pci_ftpci_init(void)
279014e4678SGavin Guo {
280014e4678SGavin Guo struct ftpci100_data *priv = NULL;
281014e4678SGavin Guo struct pci_controller *hose = (struct pci_controller *)&local_hose;
282014e4678SGavin Guo pci_dev_t bridge_num;
283014e4678SGavin Guo
284014e4678SGavin Guo struct pci_device_id bridge_ids[] = {
285014e4678SGavin Guo {FTPCI100_BRIDGE_VENDORID, FTPCI100_BRIDGE_DEVICEID},
286014e4678SGavin Guo {0, 0}
287014e4678SGavin Guo };
288014e4678SGavin Guo
289014e4678SGavin Guo priv = malloc(sizeof(struct ftpci100_data));
290014e4678SGavin Guo
291014e4678SGavin Guo if (!priv) {
292014e4678SGavin Guo printf("%s(): failed to malloc priv\n", __func__);
293014e4678SGavin Guo return;
294014e4678SGavin Guo }
295014e4678SGavin Guo
296014e4678SGavin Guo memset(priv, 0, sizeof(struct ftpci100_data));
297014e4678SGavin Guo
298014e4678SGavin Guo ftpci_preinit(priv);
299014e4678SGavin Guo
300014e4678SGavin Guo debug("Device bus dev func deviceID vendorID pin address" \
301014e4678SGavin Guo " size class\n");
302014e4678SGavin Guo
303014e4678SGavin Guo pci_bus_scan(priv);
304014e4678SGavin Guo
305014e4678SGavin Guo /*
306014e4678SGavin Guo * Setup the PCI Bridge Window to 1GB,
307014e4678SGavin Guo * it will cause USB OHCI Host controller Unrecoverable Error
308014e4678SGavin Guo * if it is not set.
309014e4678SGavin Guo */
310014e4678SGavin Guo bridge_num = pci_find_devices(bridge_ids, 0);
311014e4678SGavin Guo if (bridge_num == -1) {
312014e4678SGavin Guo printf("PCI Bridge not found\n");
313014e4678SGavin Guo return;
314014e4678SGavin Guo }
315014e4678SGavin Guo pci_hose_write_config_dword(hose, bridge_num, PCI_MEM_BASE_SIZE1,
316014e4678SGavin Guo FTPCI100_BASE_ADR_SIZE(1024));
317014e4678SGavin Guo }
318