18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * linux/arch/arm/mach-sa1100/pci-nanoengine.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * PCI functions for BSE nanoEngine PCI
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2010 Marcelo Roberto Jimenez <mroberto@cpti.cetuc.puc-rio.br>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/irq.h>
118c2ecf20Sopenharmony_ci#include <linux/pci.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <asm/mach/pci.h>
148c2ecf20Sopenharmony_ci#include <asm/mach-types.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <mach/nanoengine.h>
178c2ecf20Sopenharmony_ci#include <mach/hardware.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic void __iomem *nanoengine_pci_map_bus(struct pci_bus *bus,
208c2ecf20Sopenharmony_ci					    unsigned int devfn, int where)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	if (bus->number != 0 || (devfn >> 3) != 0)
238c2ecf20Sopenharmony_ci		return NULL;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	return (void __iomem *)NANO_PCI_CONFIG_SPACE_VIRT +
268c2ecf20Sopenharmony_ci		((bus->number << 16) | (devfn << 8) | (where & ~3));
278c2ecf20Sopenharmony_ci}
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic struct pci_ops pci_nano_ops = {
308c2ecf20Sopenharmony_ci	.map_bus = nanoengine_pci_map_bus,
318c2ecf20Sopenharmony_ci	.read	= pci_generic_config_read32,
328c2ecf20Sopenharmony_ci	.write	= pci_generic_config_write32,
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic int __init pci_nanoengine_map_irq(const struct pci_dev *dev, u8 slot,
368c2ecf20Sopenharmony_ci	u8 pin)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	return NANOENGINE_IRQ_GPIO_PCI;
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic struct resource pci_io_ports =
428c2ecf20Sopenharmony_ci	DEFINE_RES_IO_NAMED(0x400, 0x400, "PCI IO");
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic struct resource pci_non_prefetchable_memory = {
458c2ecf20Sopenharmony_ci	.name	= "PCI non-prefetchable",
468c2ecf20Sopenharmony_ci	.start	= NANO_PCI_MEM_RW_PHYS,
478c2ecf20Sopenharmony_ci	/* nanoEngine documentation says there is a 1 Megabyte window here,
488c2ecf20Sopenharmony_ci	 * but PCI reports just 128 + 8 kbytes. */
498c2ecf20Sopenharmony_ci	.end	= NANO_PCI_MEM_RW_PHYS + NANO_PCI_MEM_RW_SIZE - 1,
508c2ecf20Sopenharmony_ci/*	.end	= NANO_PCI_MEM_RW_PHYS + SZ_128K + SZ_8K - 1,*/
518c2ecf20Sopenharmony_ci	.flags	= IORESOURCE_MEM,
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/*
558c2ecf20Sopenharmony_ci * nanoEngine PCI reports 1 Megabyte of prefetchable memory, but it
568c2ecf20Sopenharmony_ci * overlaps with previously defined memory.
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * Here is what happens:
598c2ecf20Sopenharmony_ci *
608c2ecf20Sopenharmony_ci# dmesg
618c2ecf20Sopenharmony_ci...
628c2ecf20Sopenharmony_cipci 0000:00:00.0: [8086:1209] type 0 class 0x000200
638c2ecf20Sopenharmony_cipci 0000:00:00.0: reg 10: [mem 0x00021000-0x00021fff]
648c2ecf20Sopenharmony_cipci 0000:00:00.0: reg 14: [io  0x0000-0x003f]
658c2ecf20Sopenharmony_cipci 0000:00:00.0: reg 18: [mem 0x00000000-0x0001ffff]
668c2ecf20Sopenharmony_cipci 0000:00:00.0: reg 30: [mem 0x00000000-0x000fffff pref]
678c2ecf20Sopenharmony_cipci 0000:00:00.0: supports D1 D2
688c2ecf20Sopenharmony_cipci 0000:00:00.0: PME# supported from D0 D1 D2 D3hot
698c2ecf20Sopenharmony_cipci 0000:00:00.0: PME# disabled
708c2ecf20Sopenharmony_ciPCI: bus0: Fast back to back transfers enabled
718c2ecf20Sopenharmony_cipci 0000:00:00.0: BAR 6: can't assign mem pref (size 0x100000)
728c2ecf20Sopenharmony_cipci 0000:00:00.0: BAR 2: assigned [mem 0x18600000-0x1861ffff]
738c2ecf20Sopenharmony_cipci 0000:00:00.0: BAR 2: set to [mem 0x18600000-0x1861ffff] (PCI address [0x0-0x1ffff])
748c2ecf20Sopenharmony_cipci 0000:00:00.0: BAR 0: assigned [mem 0x18620000-0x18620fff]
758c2ecf20Sopenharmony_cipci 0000:00:00.0: BAR 0: set to [mem 0x18620000-0x18620fff] (PCI address [0x20000-0x20fff])
768c2ecf20Sopenharmony_cipci 0000:00:00.0: BAR 1: assigned [io  0x0400-0x043f]
778c2ecf20Sopenharmony_cipci 0000:00:00.0: BAR 1: set to [io  0x0400-0x043f] (PCI address [0x0-0x3f])
788c2ecf20Sopenharmony_ci *
798c2ecf20Sopenharmony_ci * On the other hand, if we do not request the prefetchable memory resource,
808c2ecf20Sopenharmony_ci * linux will alloc it first and the two non-prefetchable memory areas that
818c2ecf20Sopenharmony_ci * are our real interest will not be mapped. So we choose to map it to an
828c2ecf20Sopenharmony_ci * unused area. It gets recognized as expansion ROM, but becomes disabled.
838c2ecf20Sopenharmony_ci *
848c2ecf20Sopenharmony_ci * Here is what happens then:
858c2ecf20Sopenharmony_ci *
868c2ecf20Sopenharmony_ci# dmesg
878c2ecf20Sopenharmony_ci...
888c2ecf20Sopenharmony_cipci 0000:00:00.0: [8086:1209] type 0 class 0x000200
898c2ecf20Sopenharmony_cipci 0000:00:00.0: reg 10: [mem 0x00021000-0x00021fff]
908c2ecf20Sopenharmony_cipci 0000:00:00.0: reg 14: [io  0x0000-0x003f]
918c2ecf20Sopenharmony_cipci 0000:00:00.0: reg 18: [mem 0x00000000-0x0001ffff]
928c2ecf20Sopenharmony_cipci 0000:00:00.0: reg 30: [mem 0x00000000-0x000fffff pref]
938c2ecf20Sopenharmony_cipci 0000:00:00.0: supports D1 D2
948c2ecf20Sopenharmony_cipci 0000:00:00.0: PME# supported from D0 D1 D2 D3hot
958c2ecf20Sopenharmony_cipci 0000:00:00.0: PME# disabled
968c2ecf20Sopenharmony_ciPCI: bus0: Fast back to back transfers enabled
978c2ecf20Sopenharmony_cipci 0000:00:00.0: BAR 6: assigned [mem 0x78000000-0x780fffff pref]
988c2ecf20Sopenharmony_cipci 0000:00:00.0: BAR 2: assigned [mem 0x18600000-0x1861ffff]
998c2ecf20Sopenharmony_cipci 0000:00:00.0: BAR 2: set to [mem 0x18600000-0x1861ffff] (PCI address [0x0-0x1ffff])
1008c2ecf20Sopenharmony_cipci 0000:00:00.0: BAR 0: assigned [mem 0x18620000-0x18620fff]
1018c2ecf20Sopenharmony_cipci 0000:00:00.0: BAR 0: set to [mem 0x18620000-0x18620fff] (PCI address [0x20000-0x20fff])
1028c2ecf20Sopenharmony_cipci 0000:00:00.0: BAR 1: assigned [io  0x0400-0x043f]
1038c2ecf20Sopenharmony_cipci 0000:00:00.0: BAR 1: set to [io  0x0400-0x043f] (PCI address [0x0-0x3f])
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci# lspci -vv -s 0000:00:00.0
1068c2ecf20Sopenharmony_ci00:00.0 Class 0200: Device 8086:1209 (rev 09)
1078c2ecf20Sopenharmony_ci        Control: I/O+ Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr+ Stepping- SERR+ FastB2B- DisINTx-
1088c2ecf20Sopenharmony_ci        Status: Cap+ 66MHz- UDF- FastB2B+ ParErr- DEVSEL=medium >TAbort- <TAbort- <MAbort- >SERR+ <PERR+ INTx-
1098c2ecf20Sopenharmony_ci        Latency: 0 (2000ns min, 14000ns max), Cache Line Size: 32 bytes
1108c2ecf20Sopenharmony_ci        Interrupt: pin A routed to IRQ 0
1118c2ecf20Sopenharmony_ci        Region 0: Memory at 18620000 (32-bit, non-prefetchable) [size=4K]
1128c2ecf20Sopenharmony_ci        Region 1: I/O ports at 0400 [size=64]
1138c2ecf20Sopenharmony_ci        Region 2: [virtual] Memory at 18600000 (32-bit, non-prefetchable) [size=128K]
1148c2ecf20Sopenharmony_ci        [virtual] Expansion ROM at 78000000 [disabled] [size=1M]
1158c2ecf20Sopenharmony_ci        Capabilities: [dc] Power Management version 2
1168c2ecf20Sopenharmony_ci                Flags: PMEClk- DSI+ D1+ D2+ AuxCurrent=0mA PME(D0+,D1+,D2+,D3hot+,D3cold-)
1178c2ecf20Sopenharmony_ci                Status: D0 NoSoftRst- PME-Enable- DSel=0 DScale=2 PME-
1188c2ecf20Sopenharmony_ci        Kernel driver in use: e100
1198c2ecf20Sopenharmony_ci        Kernel modules: e100
1208c2ecf20Sopenharmony_ci *
1218c2ecf20Sopenharmony_ci */
1228c2ecf20Sopenharmony_cistatic struct resource pci_prefetchable_memory = {
1238c2ecf20Sopenharmony_ci	.name	= "PCI prefetchable",
1248c2ecf20Sopenharmony_ci	.start	= 0x78000000,
1258c2ecf20Sopenharmony_ci	.end	= 0x78000000 + NANO_PCI_MEM_RW_SIZE - 1,
1268c2ecf20Sopenharmony_ci	.flags	= IORESOURCE_MEM  | IORESOURCE_PREFETCH,
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic int __init pci_nanoengine_setup_resources(struct pci_sys_data *sys)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	if (request_resource(&ioport_resource, &pci_io_ports)) {
1328c2ecf20Sopenharmony_ci		printk(KERN_ERR "PCI: unable to allocate io port region\n");
1338c2ecf20Sopenharmony_ci		return -EBUSY;
1348c2ecf20Sopenharmony_ci	}
1358c2ecf20Sopenharmony_ci	if (request_resource(&iomem_resource, &pci_non_prefetchable_memory)) {
1368c2ecf20Sopenharmony_ci		release_resource(&pci_io_ports);
1378c2ecf20Sopenharmony_ci		printk(KERN_ERR "PCI: unable to allocate non prefetchable\n");
1388c2ecf20Sopenharmony_ci		return -EBUSY;
1398c2ecf20Sopenharmony_ci	}
1408c2ecf20Sopenharmony_ci	if (request_resource(&iomem_resource, &pci_prefetchable_memory)) {
1418c2ecf20Sopenharmony_ci		release_resource(&pci_io_ports);
1428c2ecf20Sopenharmony_ci		release_resource(&pci_non_prefetchable_memory);
1438c2ecf20Sopenharmony_ci		printk(KERN_ERR "PCI: unable to allocate prefetchable\n");
1448c2ecf20Sopenharmony_ci		return -EBUSY;
1458c2ecf20Sopenharmony_ci	}
1468c2ecf20Sopenharmony_ci	pci_add_resource_offset(&sys->resources, &pci_io_ports, sys->io_offset);
1478c2ecf20Sopenharmony_ci	pci_add_resource_offset(&sys->resources,
1488c2ecf20Sopenharmony_ci				&pci_non_prefetchable_memory, sys->mem_offset);
1498c2ecf20Sopenharmony_ci	pci_add_resource_offset(&sys->resources,
1508c2ecf20Sopenharmony_ci				&pci_prefetchable_memory, sys->mem_offset);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	return 1;
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ciint __init pci_nanoengine_setup(int nr, struct pci_sys_data *sys)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	int ret = 0;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	pcibios_min_io = 0;
1608c2ecf20Sopenharmony_ci	pcibios_min_mem = 0;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	if (nr == 0) {
1638c2ecf20Sopenharmony_ci		sys->mem_offset = NANO_PCI_MEM_RW_PHYS;
1648c2ecf20Sopenharmony_ci		sys->io_offset = 0x400;
1658c2ecf20Sopenharmony_ci		ret = pci_nanoengine_setup_resources(sys);
1668c2ecf20Sopenharmony_ci		/* Enable alternate memory bus master mode, see
1678c2ecf20Sopenharmony_ci		 * "Intel StrongARM SA1110 Developer's Manual",
1688c2ecf20Sopenharmony_ci		 * section 10.8, "Alternate Memory Bus Master Mode". */
1698c2ecf20Sopenharmony_ci		GPDR = (GPDR & ~GPIO_MBREQ) | GPIO_MBGNT;
1708c2ecf20Sopenharmony_ci		GAFR |= GPIO_MBGNT | GPIO_MBREQ;
1718c2ecf20Sopenharmony_ci		TUCR |= TUCR_MBGPIO;
1728c2ecf20Sopenharmony_ci	}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	return ret;
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic struct hw_pci nanoengine_pci __initdata = {
1788c2ecf20Sopenharmony_ci	.map_irq		= pci_nanoengine_map_irq,
1798c2ecf20Sopenharmony_ci	.nr_controllers		= 1,
1808c2ecf20Sopenharmony_ci	.ops			= &pci_nano_ops,
1818c2ecf20Sopenharmony_ci	.setup			= pci_nanoengine_setup,
1828c2ecf20Sopenharmony_ci};
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic int __init nanoengine_pci_init(void)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	if (machine_is_nanoengine())
1878c2ecf20Sopenharmony_ci		pci_common_init(&nanoengine_pci);
1888c2ecf20Sopenharmony_ci	return 0;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cisubsys_initcall(nanoengine_pci_init);
192