pci-bcm1480.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2001,2002,2005 Broadcom Corporation
  4. * Copyright (C) 2004 by Ralf Baechle ([email protected])
  5. */
  6. /*
  7. * BCM1x80/1x55-specific PCI support
  8. *
  9. * This module provides the glue between Linux's PCI subsystem
  10. * and the hardware. We basically provide glue for accessing
  11. * configuration space, and set up the translation for I/O
  12. * space accesses.
  13. *
  14. * To access configuration space, we use ioremap. In the 32-bit
  15. * kernel, this consumes either 4 or 8 page table pages, and 16MB of
  16. * kernel mapped memory. Hopefully neither of these should be a huge
  17. * problem.
  18. *
  19. * XXX: AT THIS TIME, ONLY the NATIVE PCI-X INTERFACE IS SUPPORTED.
  20. */
  21. #include <linux/types.h>
  22. #include <linux/pci.h>
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/mm.h>
  26. #include <linux/console.h>
  27. #include <linux/tty.h>
  28. #include <linux/vt.h>
  29. #include <asm/sibyte/bcm1480_regs.h>
  30. #include <asm/sibyte/bcm1480_scd.h>
  31. #include <asm/sibyte/board.h>
  32. #include <asm/io.h>
  33. /*
  34. * Macros for calculating offsets into config space given a device
  35. * structure or dev/fun/reg
  36. */
  37. #define CFGOFFSET(bus, devfn, where) (((bus)<<16)+((devfn)<<8)+(where))
  38. #define CFGADDR(bus, devfn, where) CFGOFFSET((bus)->number, (devfn), where)
  39. static void *cfg_space;
  40. #define PCI_BUS_ENABLED 1
  41. #define PCI_DEVICE_MODE 2
  42. static int bcm1480_bus_status;
  43. #define PCI_BRIDGE_DEVICE 0
  44. /*
  45. * Read/write 32-bit values in config space.
  46. */
  47. static inline u32 READCFG32(u32 addr)
  48. {
  49. return *(u32 *)(cfg_space + (addr&~3));
  50. }
  51. static inline void WRITECFG32(u32 addr, u32 data)
  52. {
  53. *(u32 *)(cfg_space + (addr & ~3)) = data;
  54. }
  55. int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  56. {
  57. if (pin == 0)
  58. return -1;
  59. return K_BCM1480_INT_PCI_INTA - 1 + pin;
  60. }
  61. /* Do platform specific device initialization at pci_enable_device() time */
  62. int pcibios_plat_dev_init(struct pci_dev *dev)
  63. {
  64. return 0;
  65. }
  66. /*
  67. * Some checks before doing config cycles:
  68. * In PCI Device Mode, hide everything on bus 0 except the LDT host
  69. * bridge. Otherwise, access is controlled by bridge MasterEn bits.
  70. */
  71. static int bcm1480_pci_can_access(struct pci_bus *bus, int devfn)
  72. {
  73. u32 devno;
  74. if (!(bcm1480_bus_status & (PCI_BUS_ENABLED | PCI_DEVICE_MODE)))
  75. return 0;
  76. if (bus->number == 0) {
  77. devno = PCI_SLOT(devfn);
  78. if (bcm1480_bus_status & PCI_DEVICE_MODE)
  79. return 0;
  80. else
  81. return 1;
  82. } else
  83. return 1;
  84. }
  85. /*
  86. * Read/write access functions for various sizes of values
  87. * in config space. Return all 1's for disallowed accesses
  88. * for a kludgy but adequate simulation of master aborts.
  89. */
  90. static int bcm1480_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  91. int where, int size, u32 * val)
  92. {
  93. u32 data = 0;
  94. if ((size == 2) && (where & 1))
  95. return PCIBIOS_BAD_REGISTER_NUMBER;
  96. else if ((size == 4) && (where & 3))
  97. return PCIBIOS_BAD_REGISTER_NUMBER;
  98. if (bcm1480_pci_can_access(bus, devfn))
  99. data = READCFG32(CFGADDR(bus, devfn, where));
  100. else
  101. data = 0xFFFFFFFF;
  102. if (size == 1)
  103. *val = (data >> ((where & 3) << 3)) & 0xff;
  104. else if (size == 2)
  105. *val = (data >> ((where & 3) << 3)) & 0xffff;
  106. else
  107. *val = data;
  108. return PCIBIOS_SUCCESSFUL;
  109. }
  110. static int bcm1480_pcibios_write(struct pci_bus *bus, unsigned int devfn,
  111. int where, int size, u32 val)
  112. {
  113. u32 cfgaddr = CFGADDR(bus, devfn, where);
  114. u32 data = 0;
  115. if ((size == 2) && (where & 1))
  116. return PCIBIOS_BAD_REGISTER_NUMBER;
  117. else if ((size == 4) && (where & 3))
  118. return PCIBIOS_BAD_REGISTER_NUMBER;
  119. if (!bcm1480_pci_can_access(bus, devfn))
  120. return PCIBIOS_BAD_REGISTER_NUMBER;
  121. data = READCFG32(cfgaddr);
  122. if (size == 1)
  123. data = (data & ~(0xff << ((where & 3) << 3))) |
  124. (val << ((where & 3) << 3));
  125. else if (size == 2)
  126. data = (data & ~(0xffff << ((where & 3) << 3))) |
  127. (val << ((where & 3) << 3));
  128. else
  129. data = val;
  130. WRITECFG32(cfgaddr, data);
  131. return PCIBIOS_SUCCESSFUL;
  132. }
  133. struct pci_ops bcm1480_pci_ops = {
  134. .read = bcm1480_pcibios_read,
  135. .write = bcm1480_pcibios_write,
  136. };
  137. static struct resource bcm1480_mem_resource = {
  138. .name = "BCM1480 PCI MEM",
  139. .start = A_BCM1480_PHYS_PCI_MEM_MATCH_BYTES,
  140. .end = A_BCM1480_PHYS_PCI_MEM_MATCH_BYTES + 0xfffffffUL,
  141. .flags = IORESOURCE_MEM,
  142. };
  143. static struct resource bcm1480_io_resource = {
  144. .name = "BCM1480 PCI I/O",
  145. .start = A_BCM1480_PHYS_PCI_IO_MATCH_BYTES,
  146. .end = A_BCM1480_PHYS_PCI_IO_MATCH_BYTES + 0x1ffffffUL,
  147. .flags = IORESOURCE_IO,
  148. };
  149. struct pci_controller bcm1480_controller = {
  150. .pci_ops = &bcm1480_pci_ops,
  151. .mem_resource = &bcm1480_mem_resource,
  152. .io_resource = &bcm1480_io_resource,
  153. .io_offset = A_BCM1480_PHYS_PCI_IO_MATCH_BYTES,
  154. };
  155. static int __init bcm1480_pcibios_init(void)
  156. {
  157. uint32_t cmdreg;
  158. uint64_t reg;
  159. /* CFE will assign PCI resources */
  160. pci_set_flags(PCI_PROBE_ONLY);
  161. /* Avoid ISA compat ranges. */
  162. PCIBIOS_MIN_IO = 0x00008000UL;
  163. PCIBIOS_MIN_MEM = 0x01000000UL;
  164. /* Set I/O resource limits. - unlimited for now to accommodate HT */
  165. ioport_resource.end = 0xffffffffUL;
  166. iomem_resource.end = 0xffffffffUL;
  167. cfg_space = ioremap(A_BCM1480_PHYS_PCI_CFG_MATCH_BITS, 16*1024*1024);
  168. /*
  169. * See if the PCI bus has been configured by the firmware.
  170. */
  171. reg = __raw_readq(IOADDR(A_SCD_SYSTEM_CFG));
  172. if (!(reg & M_BCM1480_SYS_PCI_HOST)) {
  173. bcm1480_bus_status |= PCI_DEVICE_MODE;
  174. } else {
  175. cmdreg = READCFG32(CFGOFFSET(0, PCI_DEVFN(PCI_BRIDGE_DEVICE, 0),
  176. PCI_COMMAND));
  177. if (!(cmdreg & PCI_COMMAND_MASTER)) {
  178. printk
  179. ("PCI: Skipping PCI probe. Bus is not initialized.\n");
  180. iounmap(cfg_space);
  181. return 1; /* XXX */
  182. }
  183. bcm1480_bus_status |= PCI_BUS_ENABLED;
  184. }
  185. /* turn on ExpMemEn */
  186. cmdreg = READCFG32(CFGOFFSET(0, PCI_DEVFN(PCI_BRIDGE_DEVICE, 0), 0x40));
  187. WRITECFG32(CFGOFFSET(0, PCI_DEVFN(PCI_BRIDGE_DEVICE, 0), 0x40),
  188. cmdreg | 0x10);
  189. cmdreg = READCFG32(CFGOFFSET(0, PCI_DEVFN(PCI_BRIDGE_DEVICE, 0), 0x40));
  190. /*
  191. * Establish mappings in KSEG2 (kernel virtual) to PCI I/O
  192. * space. Use "match bytes" policy to make everything look
  193. * little-endian. So, you need to also set
  194. * CONFIG_SWAP_IO_SPACE, but this is the combination that
  195. * works correctly with most of Linux's drivers.
  196. * XXX ehs: Should this happen in PCI Device mode?
  197. */
  198. bcm1480_controller.io_map_base = (unsigned long)
  199. ioremap(A_BCM1480_PHYS_PCI_IO_MATCH_BYTES, 65536);
  200. bcm1480_controller.io_map_base -= bcm1480_controller.io_offset;
  201. set_io_port_base(bcm1480_controller.io_map_base);
  202. register_pci_controller(&bcm1480_controller);
  203. #ifdef CONFIG_VGA_CONSOLE
  204. console_lock();
  205. do_take_over_console(&vga_con, 0, MAX_NR_CONSOLES-1, 1);
  206. console_unlock();
  207. #endif
  208. return 0;
  209. }
  210. arch_initcall(bcm1480_pcibios_init);