Blame view

drivers/bcma/driver_pci_host.c 17.4 KB
9352f69c9   Rafał Miłecki   bcma: detect PCI ...
1
2
3
4
  /*
   * Broadcom specific AMBA
   * PCI Core in hostmode
   *
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
5
6
7
8
   * Copyright 2005 - 2011, Broadcom Corporation
   * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
   * Copyright 2011, 2012, Hauke Mehrtens <hauke@hauke-m.de>
   *
9352f69c9   Rafał Miłecki   bcma: detect PCI ...
9
10
11
12
   * Licensed under the GNU/GPL. See COPYING for details.
   */
  
  #include "bcma_private.h"
58f743ee0   Paul Gortmaker   bcma: fix build e...
13
  #include <linux/pci.h>
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
14
  #include <linux/export.h>
9352f69c9   Rafał Miłecki   bcma: detect PCI ...
15
  #include <linux/bcma/bcma.h>
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
16
17
18
19
20
21
22
23
24
25
  #include <asm/paccess.h>
  
  /* Probe a 32bit value on the bus and catch bus exceptions.
   * Returns nonzero on a bus exception.
   * This is MIPS specific */
  #define mips_busprobe32(val, addr)	get_dbe((val), ((u32 *)(addr)))
  
  /* Assume one-hot slot wiring */
  #define BCMA_PCI_SLOT_MAX	16
  #define	PCI_CONFIG_SPACE_SIZE	256
0f58a01dd   Greg Kroah-Hartman   Drivers: bcma: re...
26
  bool bcma_core_pci_is_in_hostmode(struct bcma_drv_pci *pc)
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
27
28
29
30
31
32
33
34
35
  {
  	struct bcma_bus *bus = pc->core->bus;
  	u16 chipid_top;
  	u32 tmp;
  
  	chipid_top = (bus->chipinfo.id & 0xFF00);
  	if (chipid_top != 0x4700 &&
  	    chipid_top != 0x5300)
  		return false;
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  	bcma_core_enable(pc->core, 0);
  
  	return !mips_busprobe32(tmp, pc->core->io_addr);
  }
  
  static u32 bcma_pcie_read_config(struct bcma_drv_pci *pc, u32 address)
  {
  	pcicore_write32(pc, BCMA_CORE_PCI_CONFIG_ADDR, address);
  	pcicore_read32(pc, BCMA_CORE_PCI_CONFIG_ADDR);
  	return pcicore_read32(pc, BCMA_CORE_PCI_CONFIG_DATA);
  }
  
  static void bcma_pcie_write_config(struct bcma_drv_pci *pc, u32 address,
  				   u32 data)
  {
  	pcicore_write32(pc, BCMA_CORE_PCI_CONFIG_ADDR, address);
  	pcicore_read32(pc, BCMA_CORE_PCI_CONFIG_ADDR);
  	pcicore_write32(pc, BCMA_CORE_PCI_CONFIG_DATA, data);
  }
  
  static u32 bcma_get_cfgspace_addr(struct bcma_drv_pci *pc, unsigned int dev,
  			     unsigned int func, unsigned int off)
  {
  	u32 addr = 0;
  
  	/* Issue config commands only when the data link is up (atleast
  	 * one external pcie device is present).
  	 */
  	if (dev >= 2 || !(bcma_pcie_read(pc, BCMA_CORE_PCI_DLLP_LSREG)
  			  & BCMA_CORE_PCI_DLLP_LSREG_LINKUP))
  		goto out;
  
  	/* Type 0 transaction */
  	/* Slide the PCI window to the appropriate slot */
  	pcicore_write32(pc, BCMA_CORE_PCI_SBTOPCI1, BCMA_CORE_PCI_SBTOPCI_CFG0);
  	/* Calculate the address */
  	addr = pc->host_controller->host_cfg_addr;
  	addr |= (dev << BCMA_CORE_PCI_CFG_SLOT_SHIFT);
  	addr |= (func << BCMA_CORE_PCI_CFG_FUN_SHIFT);
  	addr |= (off & ~3);
  
  out:
  	return addr;
  }
  
  static int bcma_extpci_read_config(struct bcma_drv_pci *pc, unsigned int dev,
  				  unsigned int func, unsigned int off,
  				  void *buf, int len)
  {
  	int err = -EINVAL;
  	u32 addr, val;
  	void __iomem *mmio = 0;
  
  	WARN_ON(!pc->hostmode);
  	if (unlikely(len != 1 && len != 2 && len != 4))
  		goto out;
  	if (dev == 0) {
  		/* we support only two functions on device 0 */
  		if (func > 1)
a35ab937c   Nathan Hintz   bcma: jump to 'ou...
95
  			goto out;
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
96
97
98
99
100
101
  
  		/* accesses to config registers with offsets >= 256
  		 * requires indirect access.
  		 */
  		if (off >= PCI_CONFIG_SPACE_SIZE) {
  			addr = (func << 12);
b09e9abd0   Nathan Hintz   bcma: add support...
102
  			addr |= (off & 0x0FFC);
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
103
104
105
106
  			val = bcma_pcie_read_config(pc, addr);
  		} else {
  			addr = BCMA_CORE_PCI_PCICFG0;
  			addr |= (func << 8);
660b9caaa   Nathan Hintz   bcma: use consist...
107
  			addr |= (off & 0xFC);
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
108
109
110
111
112
113
114
  			val = pcicore_read32(pc, addr);
  		}
  	} else {
  		addr = bcma_get_cfgspace_addr(pc, dev, func, off);
  		if (unlikely(!addr))
  			goto out;
  		err = -ENOMEM;
c61cab3a6   Nathan Hintz   bcma: reads/write...
115
  		mmio = ioremap_nocache(addr, sizeof(val));
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
116
117
118
119
  		if (!mmio)
  			goto out;
  
  		if (mips_busprobe32(val, mmio)) {
660b9caaa   Nathan Hintz   bcma: use consist...
120
  			val = 0xFFFFFFFF;
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
121
122
  			goto unmap;
  		}
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  	}
  	val >>= (8 * (off & 3));
  
  	switch (len) {
  	case 1:
  		*((u8 *)buf) = (u8)val;
  		break;
  	case 2:
  		*((u16 *)buf) = (u16)val;
  		break;
  	case 4:
  		*((u32 *)buf) = (u32)val;
  		break;
  	}
  	err = 0;
  unmap:
  	if (mmio)
  		iounmap(mmio);
  out:
  	return err;
  }
  
  static int bcma_extpci_write_config(struct bcma_drv_pci *pc, unsigned int dev,
  				   unsigned int func, unsigned int off,
  				   const void *buf, int len)
  {
  	int err = -EINVAL;
447d7e25b   Nathan Hintz   bcma: don't map/u...
150
  	u32 addr, val;
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
151
152
153
154
155
156
157
  	void __iomem *mmio = 0;
  	u16 chipid = pc->core->bus->chipinfo.id;
  
  	WARN_ON(!pc->hostmode);
  	if (unlikely(len != 1 && len != 2 && len != 4))
  		goto out;
  	if (dev == 0) {
a35ab937c   Nathan Hintz   bcma: jump to 'ou...
158
159
160
  		/* we support only two functions on device 0 */
  		if (func > 1)
  			goto out;
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
161
162
163
  		/* accesses to config registers with offsets >= 256
  		 * requires indirect access.
  		 */
b09e9abd0   Nathan Hintz   bcma: add support...
164
165
166
167
168
  		if (off >= PCI_CONFIG_SPACE_SIZE) {
  			addr = (func << 12);
  			addr |= (off & 0x0FFC);
  			val = bcma_pcie_read_config(pc, addr);
  		} else {
447d7e25b   Nathan Hintz   bcma: don't map/u...
169
  			addr = BCMA_CORE_PCI_PCICFG0;
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
170
  			addr |= (func << 8);
660b9caaa   Nathan Hintz   bcma: use consist...
171
  			addr |= (off & 0xFC);
447d7e25b   Nathan Hintz   bcma: don't map/u...
172
  			val = pcicore_read32(pc, addr);
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
173
174
175
176
177
178
  		}
  	} else {
  		addr = bcma_get_cfgspace_addr(pc, dev, func, off);
  		if (unlikely(!addr))
  			goto out;
  		err = -ENOMEM;
c61cab3a6   Nathan Hintz   bcma: reads/write...
179
  		mmio = ioremap_nocache(addr, sizeof(val));
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
180
181
182
183
  		if (!mmio)
  			goto out;
  
  		if (mips_busprobe32(val, mmio)) {
660b9caaa   Nathan Hintz   bcma: use consist...
184
  			val = 0xFFFFFFFF;
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
185
186
187
188
189
190
  			goto unmap;
  		}
  	}
  
  	switch (len) {
  	case 1:
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
191
192
193
194
  		val &= ~(0xFF << (8 * (off & 3)));
  		val |= *((const u8 *)buf) << (8 * (off & 3));
  		break;
  	case 2:
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
195
196
197
198
199
200
201
  		val &= ~(0xFFFF << (8 * (off & 3)));
  		val |= *((const u16 *)buf) << (8 * (off & 3));
  		break;
  	case 4:
  		val = *((const u32 *)buf);
  		break;
  	}
447d7e25b   Nathan Hintz   bcma: don't map/u...
202
  	if (dev == 0) {
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
203
204
205
  		/* accesses to config registers with offsets >= 256
  		 * requires indirect access.
  		 */
b09e9abd0   Nathan Hintz   bcma: add support...
206
  		if (off >= PCI_CONFIG_SPACE_SIZE)
447d7e25b   Nathan Hintz   bcma: don't map/u...
207
  			bcma_pcie_write_config(pc, addr, val);
b09e9abd0   Nathan Hintz   bcma: add support...
208
  		else
447d7e25b   Nathan Hintz   bcma: don't map/u...
209
  			pcicore_write32(pc, addr, val);
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
210
211
  	} else {
  		writel(val, mmio);
4b4f5be2e   Hauke Mehrtens   bcma: add constan...
212
213
  		if (chipid == BCMA_CHIP_ID_BCM4716 ||
  		    chipid == BCMA_CHIP_ID_BCM4748)
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  			readl(mmio);
  	}
  
  	err = 0;
  unmap:
  	if (mmio)
  		iounmap(mmio);
  out:
  	return err;
  }
  
  static int bcma_core_pci_hostmode_read_config(struct pci_bus *bus,
  					      unsigned int devfn,
  					      int reg, int size, u32 *val)
  {
  	unsigned long flags;
  	int err;
  	struct bcma_drv_pci *pc;
  	struct bcma_drv_pci_host *pc_host;
  
  	pc_host = container_of(bus->ops, struct bcma_drv_pci_host, pci_ops);
  	pc = pc_host->pdev;
  
  	spin_lock_irqsave(&pc_host->cfgspace_lock, flags);
  	err = bcma_extpci_read_config(pc, PCI_SLOT(devfn),
  				     PCI_FUNC(devfn), reg, val, size);
  	spin_unlock_irqrestore(&pc_host->cfgspace_lock, flags);
  
  	return err ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
  }
  
  static int bcma_core_pci_hostmode_write_config(struct pci_bus *bus,
  					       unsigned int devfn,
  					       int reg, int size, u32 val)
  {
  	unsigned long flags;
  	int err;
  	struct bcma_drv_pci *pc;
  	struct bcma_drv_pci_host *pc_host;
  
  	pc_host = container_of(bus->ops, struct bcma_drv_pci_host, pci_ops);
  	pc = pc_host->pdev;
  
  	spin_lock_irqsave(&pc_host->cfgspace_lock, flags);
  	err = bcma_extpci_write_config(pc, PCI_SLOT(devfn),
  				      PCI_FUNC(devfn), reg, &val, size);
  	spin_unlock_irqrestore(&pc_host->cfgspace_lock, flags);
  
  	return err ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
  }
  
  /* return cap_offset if requested capability exists in the PCI config space */
0f58a01dd   Greg Kroah-Hartman   Drivers: bcma: re...
266
267
268
  static u8 bcma_find_pci_capability(struct bcma_drv_pci *pc, unsigned int dev,
  				   unsigned int func, u8 req_cap_id,
  				   unsigned char *buf, u32 *buflen)
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
269
270
271
272
273
274
275
276
277
  {
  	u8 cap_id;
  	u8 cap_ptr = 0;
  	u32 bufsize;
  	u8 byte_val;
  
  	/* check for Header type 0 */
  	bcma_extpci_read_config(pc, dev, func, PCI_HEADER_TYPE, &byte_val,
  				sizeof(u8));
660b9caaa   Nathan Hintz   bcma: use consist...
278
  	if ((byte_val & 0x7F) != PCI_HEADER_TYPE_NORMAL)
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
  		return cap_ptr;
  
  	/* check if the capability pointer field exists */
  	bcma_extpci_read_config(pc, dev, func, PCI_STATUS, &byte_val,
  				sizeof(u8));
  	if (!(byte_val & PCI_STATUS_CAP_LIST))
  		return cap_ptr;
  
  	/* check if the capability pointer is 0x00 */
  	bcma_extpci_read_config(pc, dev, func, PCI_CAPABILITY_LIST, &cap_ptr,
  				sizeof(u8));
  	if (cap_ptr == 0x00)
  		return cap_ptr;
  
  	/* loop thr'u the capability list and see if the requested capabilty
  	 * exists */
  	bcma_extpci_read_config(pc, dev, func, cap_ptr, &cap_id, sizeof(u8));
  	while (cap_id != req_cap_id) {
  		bcma_extpci_read_config(pc, dev, func, cap_ptr + 1, &cap_ptr,
  					sizeof(u8));
  		if (cap_ptr == 0x00)
  			return cap_ptr;
  		bcma_extpci_read_config(pc, dev, func, cap_ptr, &cap_id,
  					sizeof(u8));
  	}
  
  	/* found the caller requested capability */
  	if ((buf != NULL) && (buflen != NULL)) {
  		u8 cap_data;
  
  		bufsize = *buflen;
  		if (!bufsize)
  			return cap_ptr;
  
  		*buflen = 0;
  
  		/* copy the cpability data excluding cap ID and next ptr */
  		cap_data = cap_ptr + 2;
  		if ((bufsize + cap_data)  > PCI_CONFIG_SPACE_SIZE)
  			bufsize = PCI_CONFIG_SPACE_SIZE - cap_data;
  		*buflen = bufsize;
  		while (bufsize--) {
  			bcma_extpci_read_config(pc, dev, func, cap_data, buf,
  						sizeof(u8));
  			cap_data++;
  			buf++;
  		}
  	}
  
  	return cap_ptr;
  }
  
  /* If the root port is capable of returning Config Request
   * Retry Status (CRS) Completion Status to software then
   * enable the feature.
   */
0f58a01dd   Greg Kroah-Hartman   Drivers: bcma: re...
335
  static void bcma_core_pci_enable_crs(struct bcma_drv_pci *pc)
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
336
  {
3d9d8af33   Rafał Miłecki   bcma: use custom ...
337
  	struct bcma_bus *bus = pc->core->bus;
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
  	u8 cap_ptr, root_ctrl, root_cap, dev;
  	u16 val16;
  	int i;
  
  	cap_ptr = bcma_find_pci_capability(pc, 0, 0, PCI_CAP_ID_EXP, NULL,
  					   NULL);
  	root_cap = cap_ptr + PCI_EXP_RTCAP;
  	bcma_extpci_read_config(pc, 0, 0, root_cap, &val16, sizeof(u16));
  	if (val16 & BCMA_CORE_PCI_RC_CRS_VISIBILITY) {
  		/* Enable CRS software visibility */
  		root_ctrl = cap_ptr + PCI_EXP_RTCTL;
  		val16 = PCI_EXP_RTCTL_CRSSVE;
  		bcma_extpci_read_config(pc, 0, 0, root_ctrl, &val16,
  					sizeof(u16));
  
  		/* Initiate a configuration request to read the vendor id
  		 * field of the device function's config space header after
  		 * 100 ms wait time from the end of Reset. If the device is
  		 * not done with its internal initialization, it must at
  		 * least return a completion TLP, with a completion status
  		 * of "Configuration Request Retry Status (CRS)". The root
  		 * complex must complete the request to the host by returning
  		 * a read-data value of 0001h for the Vendor ID field and
  		 * all 1s for any additional bytes included in the request.
  		 * Poll using the config reads for max wait time of 1 sec or
  		 * until we receive the successful completion status. Repeat
  		 * the procedure for all the devices.
  		 */
  		for (dev = 1; dev < BCMA_PCI_SLOT_MAX; dev++) {
  			for (i = 0; i < 100000; i++) {
  				bcma_extpci_read_config(pc, dev, 0,
  							PCI_VENDOR_ID, &val16,
  							sizeof(val16));
  				if (val16 != 0x1)
  					break;
  				udelay(10);
  			}
  			if (val16 == 0x1)
3d9d8af33   Rafał Miłecki   bcma: use custom ...
376
377
378
  				bcma_err(bus, "PCI: Broken device in slot %d
  ",
  					 dev);
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
379
380
381
  		}
  	}
  }
9352f69c9   Rafał Miłecki   bcma: detect PCI ...
382

0f58a01dd   Greg Kroah-Hartman   Drivers: bcma: re...
383
  void bcma_core_pci_hostmode_init(struct bcma_drv_pci *pc)
9352f69c9   Rafał Miłecki   bcma: detect PCI ...
384
  {
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
385
386
387
388
389
  	struct bcma_bus *bus = pc->core->bus;
  	struct bcma_drv_pci_host *pc_host;
  	u32 tmp;
  	u32 pci_membase_1G;
  	unsigned long io_map_base;
3d9d8af33   Rafał Miłecki   bcma: use custom ...
390
391
  	bcma_info(bus, "PCIEcore in host mode found
  ");
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
392

2b4766c30   Hauke Mehrtens   bcma: do not init...
393
394
395
396
397
  	if (bus->sprom.boardflags_lo & BCMA_CORE_PCI_BFL_NOPCI) {
  		bcma_info(bus, "This PCIE core is disabled and not working
  ");
  		return;
  	}
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
398
399
  	pc_host = kzalloc(sizeof(*pc_host), GFP_KERNEL);
  	if (!pc_host)  {
3d9d8af33   Rafał Miłecki   bcma: use custom ...
400
  		bcma_err(bus, "can not allocate memory");
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
401
402
  		return;
  	}
f45dd363b   Hauke Mehrtens   bcma: init spin lock
403
  	spin_lock_init(&pc_host->cfgspace_lock);
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
  	pc->host_controller = pc_host;
  	pc_host->pci_controller.io_resource = &pc_host->io_resource;
  	pc_host->pci_controller.mem_resource = &pc_host->mem_resource;
  	pc_host->pci_controller.pci_ops = &pc_host->pci_ops;
  	pc_host->pdev = pc;
  
  	pci_membase_1G = BCMA_SOC_PCI_DMA;
  	pc_host->host_cfg_addr = BCMA_SOC_PCI_CFG;
  
  	pc_host->pci_ops.read = bcma_core_pci_hostmode_read_config;
  	pc_host->pci_ops.write = bcma_core_pci_hostmode_write_config;
  
  	pc_host->mem_resource.name = "BCMA PCIcore external memory",
  	pc_host->mem_resource.start = BCMA_SOC_PCI_DMA;
  	pc_host->mem_resource.end = BCMA_SOC_PCI_DMA + BCMA_SOC_PCI_DMA_SZ - 1;
  	pc_host->mem_resource.flags = IORESOURCE_MEM | IORESOURCE_PCI_FIXED;
  
  	pc_host->io_resource.name = "BCMA PCIcore external I/O",
  	pc_host->io_resource.start = 0x100;
  	pc_host->io_resource.end = 0x7FF;
  	pc_host->io_resource.flags = IORESOURCE_IO | IORESOURCE_PCI_FIXED;
  
  	/* Reset RC */
1fd41a65f   Rafał Miłecki   bcma: change dela...
427
  	usleep_range(3000, 5000);
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
428
  	pcicore_write32(pc, BCMA_CORE_PCI_CTL, BCMA_CORE_PCI_CTL_RST_OE);
990debe2c   Nathan Hintz   bcma: update pci ...
429
  	msleep(50);
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
430
431
432
433
434
435
436
437
438
  	pcicore_write32(pc, BCMA_CORE_PCI_CTL, BCMA_CORE_PCI_CTL_RST |
  			BCMA_CORE_PCI_CTL_RST_OE);
  
  	/* 64 MB I/O access window. On 4716, use
  	 * sbtopcie0 to access the device registers. We
  	 * can't use address match 2 (1 GB window) region
  	 * as mips can't generate 64-bit address on the
  	 * backplane.
  	 */
4b4f5be2e   Hauke Mehrtens   bcma: add constan...
439
440
  	if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4716 ||
  	    bus->chipinfo.id == BCMA_CHIP_ID_BCM4748) {
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
441
442
443
444
445
  		pc_host->mem_resource.start = BCMA_SOC_PCI_MEM;
  		pc_host->mem_resource.end = BCMA_SOC_PCI_MEM +
  					    BCMA_SOC_PCI_MEM_SZ - 1;
  		pcicore_write32(pc, BCMA_CORE_PCI_SBTOPCI0,
  				BCMA_CORE_PCI_SBTOPCI_MEM | BCMA_SOC_PCI_MEM);
4b4f5be2e   Hauke Mehrtens   bcma: add constan...
446
  	} else if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706) {
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
447
448
449
450
451
452
453
  		tmp = BCMA_CORE_PCI_SBTOPCI_MEM;
  		tmp |= BCMA_CORE_PCI_SBTOPCI_PREF;
  		tmp |= BCMA_CORE_PCI_SBTOPCI_BURST;
  		if (pc->core->core_unit == 0) {
  			pc_host->mem_resource.start = BCMA_SOC_PCI_MEM;
  			pc_host->mem_resource.end = BCMA_SOC_PCI_MEM +
  						    BCMA_SOC_PCI_MEM_SZ - 1;
dfae71436   Hauke Mehrtens   bcma: add an extr...
454
455
  			pc_host->io_resource.start = 0x100;
  			pc_host->io_resource.end = 0x47F;
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
456
457
458
459
460
461
462
  			pci_membase_1G = BCMA_SOC_PCIE_DMA_H32;
  			pcicore_write32(pc, BCMA_CORE_PCI_SBTOPCI0,
  					tmp | BCMA_SOC_PCI_MEM);
  		} else if (pc->core->core_unit == 1) {
  			pc_host->mem_resource.start = BCMA_SOC_PCI1_MEM;
  			pc_host->mem_resource.end = BCMA_SOC_PCI1_MEM +
  						    BCMA_SOC_PCI_MEM_SZ - 1;
dfae71436   Hauke Mehrtens   bcma: add an extr...
463
464
  			pc_host->io_resource.start = 0x480;
  			pc_host->io_resource.end = 0x7FF;
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
  			pci_membase_1G = BCMA_SOC_PCIE1_DMA_H32;
  			pc_host->host_cfg_addr = BCMA_SOC_PCI1_CFG;
  			pcicore_write32(pc, BCMA_CORE_PCI_SBTOPCI0,
  					tmp | BCMA_SOC_PCI1_MEM);
  		}
  	} else
  		pcicore_write32(pc, BCMA_CORE_PCI_SBTOPCI0,
  				BCMA_CORE_PCI_SBTOPCI_IO);
  
  	/* 64 MB configuration access window */
  	pcicore_write32(pc, BCMA_CORE_PCI_SBTOPCI1, BCMA_CORE_PCI_SBTOPCI_CFG0);
  
  	/* 1 GB memory access window */
  	pcicore_write32(pc, BCMA_CORE_PCI_SBTOPCI2,
  			BCMA_CORE_PCI_SBTOPCI_MEM | pci_membase_1G);
  
  
  	/* As per PCI Express Base Spec 1.1 we need to wait for
  	 * at least 100 ms from the end of a reset (cold/warm/hot)
  	 * before issuing configuration requests to PCI Express
  	 * devices.
  	 */
1fd41a65f   Rafał Miłecki   bcma: change dela...
487
  	msleep(100);
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
488
489
  
  	bcma_core_pci_enable_crs(pc);
990debe2c   Nathan Hintz   bcma: update pci ...
490
491
492
493
494
495
496
497
498
499
  	if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706 ||
  	    bus->chipinfo.id == BCMA_CHIP_ID_BCM4716) {
  		u16 val16;
  		bcma_extpci_read_config(pc, 0, 0, BCMA_CORE_PCI_CFG_DEVCTRL,
  					&val16, sizeof(val16));
  		val16 |= (2 << 5);	/* Max payload size of 512 */
  		val16 |= (2 << 12);	/* MRRS 512 */
  		bcma_extpci_write_config(pc, 0, 0, BCMA_CORE_PCI_CFG_DEVCTRL,
  					 &val16, sizeof(val16));
  	}
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
500
501
502
503
504
505
506
507
508
509
  	/* Enable PCI bridge BAR0 memory & master access */
  	tmp = PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
  	bcma_extpci_write_config(pc, 0, 0, PCI_COMMAND, &tmp, sizeof(tmp));
  
  	/* Enable PCI interrupts */
  	pcicore_write32(pc, BCMA_CORE_PCI_IMASK, BCMA_CORE_PCI_IMASK_INTA);
  
  	/* Ok, ready to run, register it to the system.
  	 * The following needs change, if we want to port hostmode
  	 * to non-MIPS platform. */
4acabf454   Nathan Hintz   bcma: Account for...
510
511
  	io_map_base = (unsigned long)ioremap_nocache(pc_host->mem_resource.start,
  						     resource_size(&pc_host->mem_resource));
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
512
513
514
515
  	pc_host->pci_controller.io_map_base = io_map_base;
  	set_io_port_base(pc_host->pci_controller.io_map_base);
  	/* Give some time to the PCI controller to configure itself with the new
  	 * values. Not waiting at this point causes crashes of the machine. */
1fd41a65f   Rafał Miłecki   bcma: change dela...
516
  	usleep_range(10000, 15000);
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
  	register_pci_controller(&pc_host->pci_controller);
  	return;
  }
  
  /* Early PCI fixup for a device on the PCI-core bridge. */
  static void bcma_core_pci_fixup_pcibridge(struct pci_dev *dev)
  {
  	if (dev->bus->ops->read != bcma_core_pci_hostmode_read_config) {
  		/* This is not a device on the PCI-core bridge. */
  		return;
  	}
  	if (PCI_SLOT(dev->devfn) != 0)
  		return;
  
  	pr_info("PCI: Fixing up bridge %s
  ", pci_name(dev));
  
  	/* Enable PCI bridge bus mastering and memory space */
  	pci_set_master(dev);
  	if (pcibios_enable_device(dev, ~0) < 0) {
  		pr_err("PCI: BCMA bridge enable failed
  ");
  		return;
  	}
  
  	/* Enable PCI bridge BAR1 prefetch and burst */
  	pci_write_config_dword(dev, BCMA_PCI_BAR1_CONTROL, 3);
  }
  DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, bcma_core_pci_fixup_pcibridge);
  
  /* Early PCI fixup for all PCI-cores to set the correct memory address. */
  static void bcma_core_pci_fixup_addresses(struct pci_dev *dev)
  {
  	struct resource *res;
4a7267c9a   Hauke Mehrtens   bcma: handle retu...
551
  	int pos, err;
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
552
553
554
555
556
557
558
559
560
561
562
563
564
  
  	if (dev->bus->ops->read != bcma_core_pci_hostmode_read_config) {
  		/* This is not a device on the PCI-core bridge. */
  		return;
  	}
  	if (PCI_SLOT(dev->devfn) == 0)
  		return;
  
  	pr_info("PCI: Fixing up addresses %s
  ", pci_name(dev));
  
  	for (pos = 0; pos < 6; pos++) {
  		res = &dev->resource[pos];
4a7267c9a   Hauke Mehrtens   bcma: handle retu...
565
566
567
568
569
570
571
  		if (res->flags & (IORESOURCE_IO | IORESOURCE_MEM)) {
  			err = pci_assign_resource(dev, pos);
  			if (err)
  				pr_err("PCI: Problem fixing up the addresses on %s
  ",
  				       pci_name(dev));
  		}
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
572
573
574
575
576
577
578
579
580
  	}
  }
  DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, bcma_core_pci_fixup_addresses);
  
  /* This function is called when doing a pci_enable_device().
   * We must first check if the device is a device on the PCI-core bridge. */
  int bcma_core_pci_plat_dev_init(struct pci_dev *dev)
  {
  	struct bcma_drv_pci_host *pc_host;
f4a83e578   Hauke Mehrtens   bcma: change max ...
581
  	int readrq;
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
582
583
584
585
586
587
588
589
590
591
592
593
  
  	if (dev->bus->ops->read != bcma_core_pci_hostmode_read_config) {
  		/* This is not a device on the PCI-core bridge. */
  		return -ENODEV;
  	}
  	pc_host = container_of(dev->bus->ops, struct bcma_drv_pci_host,
  			       pci_ops);
  
  	pr_info("PCI: Fixing up device %s
  ", pci_name(dev));
  
  	/* Fix up interrupt lines */
85eb92e81   Hauke Mehrtens   bcma: make it pos...
594
  	dev->irq = bcma_core_irq(pc_host->pdev->core, 0);
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
595
  	pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
f4a83e578   Hauke Mehrtens   bcma: change max ...
596
597
598
599
600
601
  	readrq = pcie_get_readrq(dev);
  	if (readrq > 128) {
  		pr_info("change PCIe max read request size from %i to 128
  ", readrq);
  		pcie_set_readrq(dev, 128);
  	}
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
  	return 0;
  }
  EXPORT_SYMBOL(bcma_core_pci_plat_dev_init);
  
  /* PCI device IRQ mapping. */
  int bcma_core_pci_pcibios_map_irq(const struct pci_dev *dev)
  {
  	struct bcma_drv_pci_host *pc_host;
  
  	if (dev->bus->ops->read != bcma_core_pci_hostmode_read_config) {
  		/* This is not a device on the PCI-core bridge. */
  		return -ENODEV;
  	}
  
  	pc_host = container_of(dev->bus->ops, struct bcma_drv_pci_host,
  			       pci_ops);
85eb92e81   Hauke Mehrtens   bcma: make it pos...
618
  	return bcma_core_irq(pc_host->pdev->core, 0);
9352f69c9   Rafał Miłecki   bcma: detect PCI ...
619
  }
49dc95771   Hauke Mehrtens   bcma: add PCIe ho...
620
  EXPORT_SYMBOL(bcma_core_pci_pcibios_map_irq);