pci-loongson.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Loongson PCI Host Controller Driver
  4. *
  5. * Copyright (C) 2020 Jiaxun Yang <[email protected]>
  6. */
  7. #include <linux/of_device.h>
  8. #include <linux/of_pci.h>
  9. #include <linux/pci.h>
  10. #include <linux/pci_ids.h>
  11. #include <linux/pci-acpi.h>
  12. #include <linux/pci-ecam.h>
  13. #include "../pci.h"
  14. /* Device IDs */
  15. #define DEV_LS2K_PCIE_PORT0 0x1a05
  16. #define DEV_LS7A_PCIE_PORT0 0x7a09
  17. #define DEV_LS7A_PCIE_PORT1 0x7a19
  18. #define DEV_LS7A_PCIE_PORT2 0x7a29
  19. #define DEV_LS7A_PCIE_PORT3 0x7a39
  20. #define DEV_LS7A_PCIE_PORT4 0x7a49
  21. #define DEV_LS7A_PCIE_PORT5 0x7a59
  22. #define DEV_LS7A_PCIE_PORT6 0x7a69
  23. #define DEV_LS2K_APB 0x7a02
  24. #define DEV_LS7A_GMAC 0x7a03
  25. #define DEV_LS7A_DC1 0x7a06
  26. #define DEV_LS7A_LPC 0x7a0c
  27. #define DEV_LS7A_AHCI 0x7a08
  28. #define DEV_LS7A_CONF 0x7a10
  29. #define DEV_LS7A_GNET 0x7a13
  30. #define DEV_LS7A_EHCI 0x7a14
  31. #define DEV_LS7A_DC2 0x7a36
  32. #define DEV_LS7A_HDMI 0x7a37
  33. #define FLAG_CFG0 BIT(0)
  34. #define FLAG_CFG1 BIT(1)
  35. #define FLAG_DEV_FIX BIT(2)
  36. #define FLAG_DEV_HIDDEN BIT(3)
  37. struct loongson_pci_data {
  38. u32 flags;
  39. struct pci_ops *ops;
  40. };
  41. struct loongson_pci {
  42. void __iomem *cfg0_base;
  43. void __iomem *cfg1_base;
  44. struct platform_device *pdev;
  45. const struct loongson_pci_data *data;
  46. };
  47. /* Fixup wrong class code in PCIe bridges */
  48. static void bridge_class_quirk(struct pci_dev *dev)
  49. {
  50. dev->class = PCI_CLASS_BRIDGE_PCI_NORMAL;
  51. }
  52. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  53. DEV_LS7A_PCIE_PORT0, bridge_class_quirk);
  54. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  55. DEV_LS7A_PCIE_PORT1, bridge_class_quirk);
  56. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  57. DEV_LS7A_PCIE_PORT2, bridge_class_quirk);
  58. static void system_bus_quirk(struct pci_dev *pdev)
  59. {
  60. /*
  61. * The address space consumed by these devices is outside the
  62. * resources of the host bridge.
  63. */
  64. pdev->mmio_always_on = 1;
  65. pdev->non_compliant_bars = 1;
  66. }
  67. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  68. DEV_LS2K_APB, system_bus_quirk);
  69. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  70. DEV_LS7A_CONF, system_bus_quirk);
  71. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  72. DEV_LS7A_LPC, system_bus_quirk);
  73. static void loongson_mrrs_quirk(struct pci_dev *pdev)
  74. {
  75. /*
  76. * Some Loongson PCIe ports have h/w limitations of maximum read
  77. * request size. They can't handle anything larger than this. So
  78. * force this limit on any devices attached under these ports.
  79. */
  80. struct pci_host_bridge *bridge = pci_find_host_bridge(pdev->bus);
  81. bridge->no_inc_mrrs = 1;
  82. }
  83. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  84. DEV_LS2K_PCIE_PORT0, loongson_mrrs_quirk);
  85. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  86. DEV_LS7A_PCIE_PORT0, loongson_mrrs_quirk);
  87. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  88. DEV_LS7A_PCIE_PORT1, loongson_mrrs_quirk);
  89. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  90. DEV_LS7A_PCIE_PORT2, loongson_mrrs_quirk);
  91. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  92. DEV_LS7A_PCIE_PORT3, loongson_mrrs_quirk);
  93. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  94. DEV_LS7A_PCIE_PORT4, loongson_mrrs_quirk);
  95. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  96. DEV_LS7A_PCIE_PORT5, loongson_mrrs_quirk);
  97. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  98. DEV_LS7A_PCIE_PORT6, loongson_mrrs_quirk);
  99. static void loongson_pci_pin_quirk(struct pci_dev *pdev)
  100. {
  101. pdev->pin = 1 + (PCI_FUNC(pdev->devfn) & 3);
  102. }
  103. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
  104. DEV_LS7A_DC1, loongson_pci_pin_quirk);
  105. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
  106. DEV_LS7A_DC2, loongson_pci_pin_quirk);
  107. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
  108. DEV_LS7A_GMAC, loongson_pci_pin_quirk);
  109. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
  110. DEV_LS7A_AHCI, loongson_pci_pin_quirk);
  111. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
  112. DEV_LS7A_EHCI, loongson_pci_pin_quirk);
  113. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
  114. DEV_LS7A_GNET, loongson_pci_pin_quirk);
  115. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
  116. DEV_LS7A_HDMI, loongson_pci_pin_quirk);
  117. static struct loongson_pci *pci_bus_to_loongson_pci(struct pci_bus *bus)
  118. {
  119. struct pci_config_window *cfg;
  120. if (acpi_disabled)
  121. return (struct loongson_pci *)(bus->sysdata);
  122. cfg = bus->sysdata;
  123. return (struct loongson_pci *)(cfg->priv);
  124. }
  125. static void __iomem *cfg0_map(struct loongson_pci *priv, struct pci_bus *bus,
  126. unsigned int devfn, int where)
  127. {
  128. unsigned long addroff = 0x0;
  129. unsigned char busnum = bus->number;
  130. if (!pci_is_root_bus(bus)) {
  131. addroff |= BIT(24); /* Type 1 Access */
  132. addroff |= (busnum << 16);
  133. }
  134. addroff |= (devfn << 8) | where;
  135. return priv->cfg0_base + addroff;
  136. }
  137. static void __iomem *cfg1_map(struct loongson_pci *priv, struct pci_bus *bus,
  138. unsigned int devfn, int where)
  139. {
  140. unsigned long addroff = 0x0;
  141. unsigned char busnum = bus->number;
  142. if (!pci_is_root_bus(bus)) {
  143. addroff |= BIT(28); /* Type 1 Access */
  144. addroff |= (busnum << 16);
  145. }
  146. addroff |= (devfn << 8) | (where & 0xff) | ((where & 0xf00) << 16);
  147. return priv->cfg1_base + addroff;
  148. }
  149. static bool pdev_may_exist(struct pci_bus *bus, unsigned int device,
  150. unsigned int function)
  151. {
  152. return !(pci_is_root_bus(bus) &&
  153. (device >= 9 && device <= 20) && (function > 0));
  154. }
  155. static void __iomem *pci_loongson_map_bus(struct pci_bus *bus,
  156. unsigned int devfn, int where)
  157. {
  158. unsigned int device = PCI_SLOT(devfn);
  159. unsigned int function = PCI_FUNC(devfn);
  160. struct loongson_pci *priv = pci_bus_to_loongson_pci(bus);
  161. /*
  162. * Do not read more than one device on the bus other than
  163. * the host bus.
  164. */
  165. if ((priv->data->flags & FLAG_DEV_FIX) && bus->self) {
  166. if (!pci_is_root_bus(bus) && (device > 0))
  167. return NULL;
  168. }
  169. /* Don't access non-existent devices */
  170. if (priv->data->flags & FLAG_DEV_HIDDEN) {
  171. if (!pdev_may_exist(bus, device, function))
  172. return NULL;
  173. }
  174. /* CFG0 can only access standard space */
  175. if (where < PCI_CFG_SPACE_SIZE && priv->cfg0_base)
  176. return cfg0_map(priv, bus, devfn, where);
  177. /* CFG1 can access extended space */
  178. if (where < PCI_CFG_SPACE_EXP_SIZE && priv->cfg1_base)
  179. return cfg1_map(priv, bus, devfn, where);
  180. return NULL;
  181. }
  182. #ifdef CONFIG_OF
  183. static int loongson_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  184. {
  185. int irq;
  186. u8 val;
  187. irq = of_irq_parse_and_map_pci(dev, slot, pin);
  188. if (irq > 0)
  189. return irq;
  190. /* Care i8259 legacy systems */
  191. pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &val);
  192. /* i8259 only have 15 IRQs */
  193. if (val > 15)
  194. return 0;
  195. return val;
  196. }
  197. /* LS2K/LS7A accept 8/16/32-bit PCI config operations */
  198. static struct pci_ops loongson_pci_ops = {
  199. .map_bus = pci_loongson_map_bus,
  200. .read = pci_generic_config_read,
  201. .write = pci_generic_config_write,
  202. };
  203. /* RS780/SR5690 only accept 32-bit PCI config operations */
  204. static struct pci_ops loongson_pci_ops32 = {
  205. .map_bus = pci_loongson_map_bus,
  206. .read = pci_generic_config_read32,
  207. .write = pci_generic_config_write32,
  208. };
  209. static const struct loongson_pci_data ls2k_pci_data = {
  210. .flags = FLAG_CFG1 | FLAG_DEV_FIX | FLAG_DEV_HIDDEN,
  211. .ops = &loongson_pci_ops,
  212. };
  213. static const struct loongson_pci_data ls7a_pci_data = {
  214. .flags = FLAG_CFG1 | FLAG_DEV_FIX | FLAG_DEV_HIDDEN,
  215. .ops = &loongson_pci_ops,
  216. };
  217. static const struct loongson_pci_data rs780e_pci_data = {
  218. .flags = FLAG_CFG0,
  219. .ops = &loongson_pci_ops32,
  220. };
  221. static const struct of_device_id loongson_pci_of_match[] = {
  222. { .compatible = "loongson,ls2k-pci",
  223. .data = &ls2k_pci_data, },
  224. { .compatible = "loongson,ls7a-pci",
  225. .data = &ls7a_pci_data, },
  226. { .compatible = "loongson,rs780e-pci",
  227. .data = &rs780e_pci_data, },
  228. {}
  229. };
  230. static int loongson_pci_probe(struct platform_device *pdev)
  231. {
  232. struct loongson_pci *priv;
  233. struct device *dev = &pdev->dev;
  234. struct device_node *node = dev->of_node;
  235. struct pci_host_bridge *bridge;
  236. struct resource *regs;
  237. if (!node)
  238. return -ENODEV;
  239. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*priv));
  240. if (!bridge)
  241. return -ENODEV;
  242. priv = pci_host_bridge_priv(bridge);
  243. priv->pdev = pdev;
  244. priv->data = of_device_get_match_data(dev);
  245. if (priv->data->flags & FLAG_CFG0) {
  246. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  247. if (!regs)
  248. dev_err(dev, "missing mem resources for cfg0\n");
  249. else {
  250. priv->cfg0_base = devm_pci_remap_cfg_resource(dev, regs);
  251. if (IS_ERR(priv->cfg0_base))
  252. return PTR_ERR(priv->cfg0_base);
  253. }
  254. }
  255. if (priv->data->flags & FLAG_CFG1) {
  256. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  257. if (!regs)
  258. dev_info(dev, "missing mem resource for cfg1\n");
  259. else {
  260. priv->cfg1_base = devm_pci_remap_cfg_resource(dev, regs);
  261. if (IS_ERR(priv->cfg1_base))
  262. priv->cfg1_base = NULL;
  263. }
  264. }
  265. bridge->sysdata = priv;
  266. bridge->ops = priv->data->ops;
  267. bridge->map_irq = loongson_map_irq;
  268. return pci_host_probe(bridge);
  269. }
  270. static struct platform_driver loongson_pci_driver = {
  271. .driver = {
  272. .name = "loongson-pci",
  273. .of_match_table = loongson_pci_of_match,
  274. },
  275. .probe = loongson_pci_probe,
  276. };
  277. builtin_platform_driver(loongson_pci_driver);
  278. #endif
  279. #ifdef CONFIG_ACPI
  280. static int loongson_pci_ecam_init(struct pci_config_window *cfg)
  281. {
  282. struct device *dev = cfg->parent;
  283. struct loongson_pci *priv;
  284. struct loongson_pci_data *data;
  285. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  286. if (!priv)
  287. return -ENOMEM;
  288. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  289. if (!data)
  290. return -ENOMEM;
  291. cfg->priv = priv;
  292. data->flags = FLAG_CFG1 | FLAG_DEV_HIDDEN;
  293. priv->data = data;
  294. priv->cfg1_base = cfg->win - (cfg->busr.start << 16);
  295. return 0;
  296. }
  297. const struct pci_ecam_ops loongson_pci_ecam_ops = {
  298. .bus_shift = 16,
  299. .init = loongson_pci_ecam_init,
  300. .pci_ops = {
  301. .map_bus = pci_loongson_map_bus,
  302. .read = pci_generic_config_read,
  303. .write = pci_generic_config_write,
  304. }
  305. };
  306. #endif