pci-loongson.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Loongson PCI Host Controller Driver
  4. *
  5. * Copyright (C) 2020 Jiaxun Yang <jiaxun.yang@flygoat.com>
  6. */
  7. #include <linux/of.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. /*
  74. * Some Loongson PCIe ports have hardware limitations on their Maximum Read
  75. * Request Size. They can't handle anything larger than this. Sane
  76. * firmware will set proper MRRS at boot, so we only need no_inc_mrrs for
  77. * bridges. However, some MIPS Loongson firmware doesn't set MRRS properly,
  78. * so we have to enforce maximum safe MRRS, which is 256 bytes.
  79. */
  80. #ifdef CONFIG_MIPS
  81. static void loongson_set_min_mrrs_quirk(struct pci_dev *pdev)
  82. {
  83. struct pci_bus *bus = pdev->bus;
  84. struct pci_dev *bridge;
  85. static const struct pci_device_id bridge_devids[] = {
  86. { PCI_VDEVICE(LOONGSON, DEV_LS2K_PCIE_PORT0) },
  87. { PCI_VDEVICE(LOONGSON, DEV_LS7A_PCIE_PORT0) },
  88. { PCI_VDEVICE(LOONGSON, DEV_LS7A_PCIE_PORT1) },
  89. { PCI_VDEVICE(LOONGSON, DEV_LS7A_PCIE_PORT2) },
  90. { PCI_VDEVICE(LOONGSON, DEV_LS7A_PCIE_PORT3) },
  91. { PCI_VDEVICE(LOONGSON, DEV_LS7A_PCIE_PORT4) },
  92. { PCI_VDEVICE(LOONGSON, DEV_LS7A_PCIE_PORT5) },
  93. { PCI_VDEVICE(LOONGSON, DEV_LS7A_PCIE_PORT6) },
  94. { 0, },
  95. };
  96. /* look for the matching bridge */
  97. while (!pci_is_root_bus(bus)) {
  98. bridge = bus->self;
  99. bus = bus->parent;
  100. if (pci_match_id(bridge_devids, bridge)) {
  101. if (pcie_get_readrq(pdev) > 256) {
  102. pci_info(pdev, "limiting MRRS to 256\n");
  103. pcie_set_readrq(pdev, 256);
  104. }
  105. break;
  106. }
  107. }
  108. }
  109. DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, loongson_set_min_mrrs_quirk);
  110. #endif
  111. static void loongson_mrrs_quirk(struct pci_dev *pdev)
  112. {
  113. struct pci_host_bridge *bridge = pci_find_host_bridge(pdev->bus);
  114. bridge->no_inc_mrrs = 1;
  115. }
  116. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  117. DEV_LS2K_PCIE_PORT0, loongson_mrrs_quirk);
  118. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  119. DEV_LS7A_PCIE_PORT0, loongson_mrrs_quirk);
  120. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  121. DEV_LS7A_PCIE_PORT1, loongson_mrrs_quirk);
  122. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  123. DEV_LS7A_PCIE_PORT2, loongson_mrrs_quirk);
  124. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  125. DEV_LS7A_PCIE_PORT3, loongson_mrrs_quirk);
  126. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  127. DEV_LS7A_PCIE_PORT4, loongson_mrrs_quirk);
  128. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  129. DEV_LS7A_PCIE_PORT5, loongson_mrrs_quirk);
  130. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_LOONGSON,
  131. DEV_LS7A_PCIE_PORT6, loongson_mrrs_quirk);
  132. static void loongson_pci_pin_quirk(struct pci_dev *pdev)
  133. {
  134. pdev->pin = 1 + (PCI_FUNC(pdev->devfn) & 3);
  135. }
  136. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
  137. DEV_LS7A_DC1, loongson_pci_pin_quirk);
  138. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
  139. DEV_LS7A_DC2, loongson_pci_pin_quirk);
  140. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
  141. DEV_LS7A_GMAC, loongson_pci_pin_quirk);
  142. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
  143. DEV_LS7A_AHCI, loongson_pci_pin_quirk);
  144. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
  145. DEV_LS7A_EHCI, loongson_pci_pin_quirk);
  146. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
  147. DEV_LS7A_GNET, loongson_pci_pin_quirk);
  148. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON,
  149. DEV_LS7A_HDMI, loongson_pci_pin_quirk);
  150. static void loongson_pci_msi_quirk(struct pci_dev *dev)
  151. {
  152. u16 val, class = dev->class >> 8;
  153. if (class != PCI_CLASS_BRIDGE_HOST)
  154. return;
  155. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &val);
  156. val |= PCI_MSI_FLAGS_ENABLE;
  157. pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, val);
  158. }
  159. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON, DEV_LS7A_PCIE_PORT5, loongson_pci_msi_quirk);
  160. static struct loongson_pci *pci_bus_to_loongson_pci(struct pci_bus *bus)
  161. {
  162. struct pci_config_window *cfg;
  163. if (acpi_disabled)
  164. return (struct loongson_pci *)(bus->sysdata);
  165. cfg = bus->sysdata;
  166. return (struct loongson_pci *)(cfg->priv);
  167. }
  168. static void __iomem *cfg0_map(struct loongson_pci *priv, struct pci_bus *bus,
  169. unsigned int devfn, int where)
  170. {
  171. unsigned long addroff = 0x0;
  172. unsigned char busnum = bus->number;
  173. if (!pci_is_root_bus(bus)) {
  174. addroff |= BIT(24); /* Type 1 Access */
  175. addroff |= (busnum << 16);
  176. }
  177. addroff |= (devfn << 8) | where;
  178. return priv->cfg0_base + addroff;
  179. }
  180. static void __iomem *cfg1_map(struct loongson_pci *priv, struct pci_bus *bus,
  181. unsigned int devfn, int where)
  182. {
  183. unsigned long addroff = 0x0;
  184. unsigned char busnum = bus->number;
  185. if (!pci_is_root_bus(bus)) {
  186. addroff |= BIT(28); /* Type 1 Access */
  187. addroff |= (busnum << 16);
  188. }
  189. addroff |= (devfn << 8) | (where & 0xff) | ((where & 0xf00) << 16);
  190. return priv->cfg1_base + addroff;
  191. }
  192. static bool pdev_may_exist(struct pci_bus *bus, unsigned int device,
  193. unsigned int function)
  194. {
  195. return !(pci_is_root_bus(bus) &&
  196. (device >= 9 && device <= 20) && (function > 0));
  197. }
  198. static void __iomem *pci_loongson_map_bus(struct pci_bus *bus,
  199. unsigned int devfn, int where)
  200. {
  201. unsigned int device = PCI_SLOT(devfn);
  202. unsigned int function = PCI_FUNC(devfn);
  203. struct loongson_pci *priv = pci_bus_to_loongson_pci(bus);
  204. /*
  205. * Do not read more than one device on the bus other than
  206. * the host bus.
  207. */
  208. if ((priv->data->flags & FLAG_DEV_FIX) && bus->self) {
  209. if (!pci_is_root_bus(bus) && (device > 0))
  210. return NULL;
  211. }
  212. /* Don't access non-existent devices */
  213. if (priv->data->flags & FLAG_DEV_HIDDEN) {
  214. if (!pdev_may_exist(bus, device, function))
  215. return NULL;
  216. }
  217. /* CFG0 can only access standard space */
  218. if (where < PCI_CFG_SPACE_SIZE && priv->cfg0_base)
  219. return cfg0_map(priv, bus, devfn, where);
  220. /* CFG1 can access extended space */
  221. if (where < PCI_CFG_SPACE_EXP_SIZE && priv->cfg1_base)
  222. return cfg1_map(priv, bus, devfn, where);
  223. return NULL;
  224. }
  225. #ifdef CONFIG_OF
  226. static int loongson_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  227. {
  228. int irq;
  229. u8 val;
  230. irq = of_irq_parse_and_map_pci(dev, slot, pin);
  231. if (irq > 0)
  232. return irq;
  233. /* Care i8259 legacy systems */
  234. pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &val);
  235. /* i8259 only have 15 IRQs */
  236. if (val > 15)
  237. return 0;
  238. return val;
  239. }
  240. /* LS2K/LS7A accept 8/16/32-bit PCI config operations */
  241. static struct pci_ops loongson_pci_ops = {
  242. .map_bus = pci_loongson_map_bus,
  243. .read = pci_generic_config_read,
  244. .write = pci_generic_config_write,
  245. };
  246. /* RS780/SR5690 only accept 32-bit PCI config operations */
  247. static struct pci_ops loongson_pci_ops32 = {
  248. .map_bus = pci_loongson_map_bus,
  249. .read = pci_generic_config_read32,
  250. .write = pci_generic_config_write32,
  251. };
  252. static const struct loongson_pci_data ls2k_pci_data = {
  253. .flags = FLAG_CFG1 | FLAG_DEV_FIX | FLAG_DEV_HIDDEN,
  254. .ops = &loongson_pci_ops,
  255. };
  256. static const struct loongson_pci_data ls7a_pci_data = {
  257. .flags = FLAG_CFG1 | FLAG_DEV_FIX | FLAG_DEV_HIDDEN,
  258. .ops = &loongson_pci_ops,
  259. };
  260. static const struct loongson_pci_data rs780e_pci_data = {
  261. .flags = FLAG_CFG0,
  262. .ops = &loongson_pci_ops32,
  263. };
  264. static const struct of_device_id loongson_pci_of_match[] = {
  265. { .compatible = "loongson,ls2k-pci",
  266. .data = &ls2k_pci_data, },
  267. { .compatible = "loongson,ls7a-pci",
  268. .data = &ls7a_pci_data, },
  269. { .compatible = "loongson,rs780e-pci",
  270. .data = &rs780e_pci_data, },
  271. {}
  272. };
  273. static int loongson_pci_probe(struct platform_device *pdev)
  274. {
  275. struct loongson_pci *priv;
  276. struct device *dev = &pdev->dev;
  277. struct device_node *node = dev->of_node;
  278. struct pci_host_bridge *bridge;
  279. struct resource *regs;
  280. if (!node)
  281. return -ENODEV;
  282. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*priv));
  283. if (!bridge)
  284. return -ENODEV;
  285. priv = pci_host_bridge_priv(bridge);
  286. priv->pdev = pdev;
  287. priv->data = of_device_get_match_data(dev);
  288. if (priv->data->flags & FLAG_CFG0) {
  289. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  290. if (!regs)
  291. dev_err(dev, "missing mem resources for cfg0\n");
  292. else {
  293. priv->cfg0_base = devm_pci_remap_cfg_resource(dev, regs);
  294. if (IS_ERR(priv->cfg0_base))
  295. return PTR_ERR(priv->cfg0_base);
  296. }
  297. }
  298. if (priv->data->flags & FLAG_CFG1) {
  299. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  300. if (!regs)
  301. dev_info(dev, "missing mem resource for cfg1\n");
  302. else {
  303. priv->cfg1_base = devm_pci_remap_cfg_resource(dev, regs);
  304. if (IS_ERR(priv->cfg1_base))
  305. priv->cfg1_base = NULL;
  306. }
  307. }
  308. bridge->sysdata = priv;
  309. bridge->ops = priv->data->ops;
  310. bridge->map_irq = loongson_map_irq;
  311. return pci_host_probe(bridge);
  312. }
  313. static struct platform_driver loongson_pci_driver = {
  314. .driver = {
  315. .name = "loongson-pci",
  316. .of_match_table = loongson_pci_of_match,
  317. },
  318. .probe = loongson_pci_probe,
  319. };
  320. builtin_platform_driver(loongson_pci_driver);
  321. #endif
  322. #ifdef CONFIG_ACPI
  323. static int loongson_pci_ecam_init(struct pci_config_window *cfg)
  324. {
  325. struct device *dev = cfg->parent;
  326. struct loongson_pci *priv;
  327. struct loongson_pci_data *data;
  328. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  329. if (!priv)
  330. return -ENOMEM;
  331. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  332. if (!data)
  333. return -ENOMEM;
  334. cfg->priv = priv;
  335. data->flags = FLAG_CFG1 | FLAG_DEV_HIDDEN;
  336. priv->data = data;
  337. priv->cfg1_base = cfg->win - (cfg->busr.start << 16);
  338. return 0;
  339. }
  340. const struct pci_ecam_ops loongson_pci_ecam_ops = {
  341. .bus_shift = 16,
  342. .init = loongson_pci_ecam_init,
  343. .pci_ops = {
  344. .map_bus = pci_loongson_map_bus,
  345. .read = pci_generic_config_read,
  346. .write = pci_generic_config_write,
  347. }
  348. };
  349. #endif