pci-nanoengine.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * linux/arch/arm/mach-sa1100/pci-nanoengine.c
  3. *
  4. * PCI functions for BSE nanoEngine PCI
  5. *
  6. * Copyright (C) 2010 Marcelo Roberto Jimenez <mroberto@cpti.cetuc.puc-rio.br>
  7. *
  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.
  12. *
  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.
  17. *
  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
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/irq.h>
  24. #include <linux/pci.h>
  25. #include <linux/spinlock.h>
  26. #include <asm/mach/pci.h>
  27. #include <asm/mach-types.h>
  28. #include <mach/nanoengine.h>
  29. static DEFINE_SPINLOCK(nano_lock);
  30. static int nanoengine_get_pci_address(struct pci_bus *bus,
  31. unsigned int devfn, int where, unsigned long *address)
  32. {
  33. int ret = PCIBIOS_DEVICE_NOT_FOUND;
  34. unsigned int busnr = bus->number;
  35. *address = NANO_PCI_CONFIG_SPACE_VIRT +
  36. ((bus->number << 16) | (devfn << 8) | (where & ~3));
  37. ret = (busnr > 255 || devfn > 255 || where > 255) ?
  38. PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
  39. return ret;
  40. }
  41. static int nanoengine_read_config(struct pci_bus *bus, unsigned int devfn, int where,
  42. int size, u32 *val)
  43. {
  44. int ret;
  45. unsigned long address;
  46. unsigned long flags;
  47. u32 v;
  48. /* nanoEngine PCI bridge does not return -1 for a non-existing
  49. * device. We must fake the answer. We know that the only valid
  50. * device is device zero at bus 0, which is the network chip. */
  51. if (bus->number != 0 || (devfn >> 3) != 0) {
  52. v = -1;
  53. nanoengine_get_pci_address(bus, devfn, where, &address);
  54. goto exit_function;
  55. }
  56. spin_lock_irqsave(&nano_lock, flags);
  57. ret = nanoengine_get_pci_address(bus, devfn, where, &address);
  58. if (ret != PCIBIOS_SUCCESSFUL)
  59. return ret;
  60. v = __raw_readl(address);
  61. spin_unlock_irqrestore(&nano_lock, flags);
  62. v >>= ((where & 3) * 8);
  63. v &= (unsigned long)(-1) >> ((4 - size) * 8);
  64. exit_function:
  65. *val = v;
  66. return PCIBIOS_SUCCESSFUL;
  67. }
  68. static int nanoengine_write_config(struct pci_bus *bus, unsigned int devfn, int where,
  69. int size, u32 val)
  70. {
  71. int ret;
  72. unsigned long address;
  73. unsigned long flags;
  74. unsigned shift;
  75. u32 v;
  76. shift = (where & 3) * 8;
  77. spin_lock_irqsave(&nano_lock, flags);
  78. ret = nanoengine_get_pci_address(bus, devfn, where, &address);
  79. if (ret != PCIBIOS_SUCCESSFUL)
  80. return ret;
  81. v = __raw_readl(address);
  82. switch (size) {
  83. case 1:
  84. v &= ~(0xFF << shift);
  85. v |= val << shift;
  86. break;
  87. case 2:
  88. v &= ~(0xFFFF << shift);
  89. v |= val << shift;
  90. break;
  91. case 4:
  92. v = val;
  93. break;
  94. }
  95. __raw_writel(v, address);
  96. spin_unlock_irqrestore(&nano_lock, flags);
  97. return PCIBIOS_SUCCESSFUL;
  98. }
  99. static struct pci_ops pci_nano_ops = {
  100. .read = nanoengine_read_config,
  101. .write = nanoengine_write_config,
  102. };
  103. static int __init pci_nanoengine_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
  104. {
  105. return NANOENGINE_IRQ_GPIO_PCI;
  106. }
  107. struct pci_bus * __init pci_nanoengine_scan_bus(int nr, struct pci_sys_data *sys)
  108. {
  109. return pci_scan_bus(sys->busnr, &pci_nano_ops, sys);
  110. }
  111. static struct resource pci_io_ports = {
  112. .name = "PCI IO",
  113. .start = 0x400,
  114. .end = 0x7FF,
  115. .flags = IORESOURCE_IO,
  116. };
  117. static struct resource pci_non_prefetchable_memory = {
  118. .name = "PCI non-prefetchable",
  119. .start = NANO_PCI_MEM_RW_PHYS,
  120. /* nanoEngine documentation says there is a 1 Megabyte window here,
  121. * but PCI reports just 128 + 8 kbytes. */
  122. .end = NANO_PCI_MEM_RW_PHYS + NANO_PCI_MEM_RW_SIZE - 1,
  123. /* .end = NANO_PCI_MEM_RW_PHYS + SZ_128K + SZ_8K - 1,*/
  124. .flags = IORESOURCE_MEM,
  125. };
  126. /*
  127. * nanoEngine PCI reports 1 Megabyte of prefetchable memory, but it
  128. * overlaps with previously defined memory.
  129. *
  130. * Here is what happens:
  131. *
  132. # dmesg
  133. ...
  134. pci 0000:00:00.0: [8086:1209] type 0 class 0x000200
  135. pci 0000:00:00.0: reg 10: [mem 0x00021000-0x00021fff]
  136. pci 0000:00:00.0: reg 14: [io 0x0000-0x003f]
  137. pci 0000:00:00.0: reg 18: [mem 0x00000000-0x0001ffff]
  138. pci 0000:00:00.0: reg 30: [mem 0x00000000-0x000fffff pref]
  139. pci 0000:00:00.0: supports D1 D2
  140. pci 0000:00:00.0: PME# supported from D0 D1 D2 D3hot
  141. pci 0000:00:00.0: PME# disabled
  142. PCI: bus0: Fast back to back transfers enabled
  143. pci 0000:00:00.0: BAR 6: can't assign mem pref (size 0x100000)
  144. pci 0000:00:00.0: BAR 2: assigned [mem 0x18600000-0x1861ffff]
  145. pci 0000:00:00.0: BAR 2: set to [mem 0x18600000-0x1861ffff] (PCI address [0x0-0x1ffff])
  146. pci 0000:00:00.0: BAR 0: assigned [mem 0x18620000-0x18620fff]
  147. pci 0000:00:00.0: BAR 0: set to [mem 0x18620000-0x18620fff] (PCI address [0x20000-0x20fff])
  148. pci 0000:00:00.0: BAR 1: assigned [io 0x0400-0x043f]
  149. pci 0000:00:00.0: BAR 1: set to [io 0x0400-0x043f] (PCI address [0x0-0x3f])
  150. *
  151. * On the other hand, if we do not request the prefetchable memory resource,
  152. * linux will alloc it first and the two non-prefetchable memory areas that
  153. * are our real interest will not be mapped. So we choose to map it to an
  154. * unused area. It gets recognized as expansion ROM, but becomes disabled.
  155. *
  156. * Here is what happens then:
  157. *
  158. # dmesg
  159. ...
  160. pci 0000:00:00.0: [8086:1209] type 0 class 0x000200
  161. pci 0000:00:00.0: reg 10: [mem 0x00021000-0x00021fff]
  162. pci 0000:00:00.0: reg 14: [io 0x0000-0x003f]
  163. pci 0000:00:00.0: reg 18: [mem 0x00000000-0x0001ffff]
  164. pci 0000:00:00.0: reg 30: [mem 0x00000000-0x000fffff pref]
  165. pci 0000:00:00.0: supports D1 D2
  166. pci 0000:00:00.0: PME# supported from D0 D1 D2 D3hot
  167. pci 0000:00:00.0: PME# disabled
  168. PCI: bus0: Fast back to back transfers enabled
  169. pci 0000:00:00.0: BAR 6: assigned [mem 0x78000000-0x780fffff pref]
  170. pci 0000:00:00.0: BAR 2: assigned [mem 0x18600000-0x1861ffff]
  171. pci 0000:00:00.0: BAR 2: set to [mem 0x18600000-0x1861ffff] (PCI address [0x0-0x1ffff])
  172. pci 0000:00:00.0: BAR 0: assigned [mem 0x18620000-0x18620fff]
  173. pci 0000:00:00.0: BAR 0: set to [mem 0x18620000-0x18620fff] (PCI address [0x20000-0x20fff])
  174. pci 0000:00:00.0: BAR 1: assigned [io 0x0400-0x043f]
  175. pci 0000:00:00.0: BAR 1: set to [io 0x0400-0x043f] (PCI address [0x0-0x3f])
  176. # lspci -vv -s 0000:00:00.0
  177. 00:00.0 Class 0200: Device 8086:1209 (rev 09)
  178. Control: I/O+ Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr+ Stepping- SERR+ FastB2B- DisINTx-
  179. Status: Cap+ 66MHz- UDF- FastB2B+ ParErr- DEVSEL=medium >TAbort- <TAbort- <MAbort- >SERR+ <PERR+ INTx-
  180. Latency: 0 (2000ns min, 14000ns max), Cache Line Size: 32 bytes
  181. Interrupt: pin A routed to IRQ 0
  182. Region 0: Memory at 18620000 (32-bit, non-prefetchable) [size=4K]
  183. Region 1: I/O ports at 0400 [size=64]
  184. Region 2: [virtual] Memory at 18600000 (32-bit, non-prefetchable) [size=128K]
  185. [virtual] Expansion ROM at 78000000 [disabled] [size=1M]
  186. Capabilities: [dc] Power Management version 2
  187. Flags: PMEClk- DSI+ D1+ D2+ AuxCurrent=0mA PME(D0+,D1+,D2+,D3hot+,D3cold-)
  188. Status: D0 NoSoftRst- PME-Enable- DSel=0 DScale=2 PME-
  189. Kernel driver in use: e100
  190. Kernel modules: e100
  191. *
  192. */
  193. static struct resource pci_prefetchable_memory = {
  194. .name = "PCI prefetchable",
  195. .start = 0x78000000,
  196. .end = 0x78000000 + NANO_PCI_MEM_RW_SIZE - 1,
  197. .flags = IORESOURCE_MEM | IORESOURCE_PREFETCH,
  198. };
  199. static int __init pci_nanoengine_setup_resources(struct resource **resource)
  200. {
  201. if (request_resource(&ioport_resource, &pci_io_ports)) {
  202. printk(KERN_ERR "PCI: unable to allocate io port region\n");
  203. return -EBUSY;
  204. }
  205. if (request_resource(&iomem_resource, &pci_non_prefetchable_memory)) {
  206. release_resource(&pci_io_ports);
  207. printk(KERN_ERR "PCI: unable to allocate non prefetchable\n");
  208. return -EBUSY;
  209. }
  210. if (request_resource(&iomem_resource, &pci_prefetchable_memory)) {
  211. release_resource(&pci_io_ports);
  212. release_resource(&pci_non_prefetchable_memory);
  213. printk(KERN_ERR "PCI: unable to allocate prefetchable\n");
  214. return -EBUSY;
  215. }
  216. resource[0] = &pci_io_ports;
  217. resource[1] = &pci_non_prefetchable_memory;
  218. resource[2] = &pci_prefetchable_memory;
  219. return 1;
  220. }
  221. int __init pci_nanoengine_setup(int nr, struct pci_sys_data *sys)
  222. {
  223. int ret = 0;
  224. if (nr == 0) {
  225. sys->mem_offset = NANO_PCI_MEM_RW_PHYS;
  226. sys->io_offset = 0x400;
  227. ret = pci_nanoengine_setup_resources(sys->resource);
  228. /* Enable alternate memory bus master mode, see
  229. * "Intel StrongARM SA1110 Developer's Manual",
  230. * section 10.8, "Alternate Memory Bus Master Mode". */
  231. GPDR = (GPDR & ~GPIO_MBREQ) | GPIO_MBGNT;
  232. GAFR |= GPIO_MBGNT | GPIO_MBREQ;
  233. TUCR |= TUCR_MBGPIO;
  234. }
  235. return ret;
  236. }
  237. static struct hw_pci nanoengine_pci __initdata = {
  238. .map_irq = pci_nanoengine_map_irq,
  239. .nr_controllers = 1,
  240. .scan = pci_nanoengine_scan_bus,
  241. .setup = pci_nanoengine_setup,
  242. };
  243. static int __init nanoengine_pci_init(void)
  244. {
  245. if (machine_is_nanoengine())
  246. pci_common_init(&nanoengine_pci);
  247. return 0;
  248. }
  249. subsys_initcall(nanoengine_pci_init);