pcie-aspeed.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2025 Aspeed Technology Inc.
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/clk.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/irq.h>
  9. #include <linux/irqdomain.h>
  10. #include <linux/irqchip/chained_irq.h>
  11. #include <linux/irqchip/irq-msi-lib.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/module.h>
  15. #include <linux/msi.h>
  16. #include <linux/mutex.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_pci.h>
  20. #include <linux/pci.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/phy/pcie.h>
  23. #include <linux/phy/phy.h>
  24. #include <linux/regmap.h>
  25. #include <linux/reset.h>
  26. #include "../pci.h"
  27. #define MAX_MSI_HOST_IRQS 64
  28. #define ASPEED_RESET_RC_WAIT_MS 10
  29. /* AST2600 AHBC Registers */
  30. #define ASPEED_AHBC_KEY 0x00
  31. #define ASPEED_AHBC_UNLOCK_KEY 0xaeed1a03
  32. #define ASPEED_AHBC_UNLOCK 0x01
  33. #define ASPEED_AHBC_ADDR_MAPPING 0x8c
  34. #define ASPEED_PCIE_RC_MEMORY_EN BIT(5)
  35. /* AST2600 H2X Controller Registers */
  36. #define ASPEED_H2X_INT_STS 0x08
  37. #define ASPEED_PCIE_TX_IDLE_CLEAR BIT(0)
  38. #define ASPEED_PCIE_INTX_STS GENMASK(3, 0)
  39. #define ASPEED_H2X_HOST_RX_DESC_DATA 0x0c
  40. #define ASPEED_H2X_TX_DESC0 0x10
  41. #define ASPEED_H2X_TX_DESC1 0x14
  42. #define ASPEED_H2X_TX_DESC2 0x18
  43. #define ASPEED_H2X_TX_DESC3 0x1c
  44. #define ASPEED_H2X_TX_DESC_DATA 0x20
  45. #define ASPEED_H2X_STS 0x24
  46. #define ASPEED_PCIE_TX_IDLE BIT(31)
  47. #define ASPEED_PCIE_STATUS_OF_TX GENMASK(25, 24)
  48. #define ASPEED_PCIE_RC_H_TX_COMPLETE BIT(25)
  49. #define ASPEED_PCIE_TRIGGER_TX BIT(0)
  50. #define ASPEED_H2X_AHB_ADDR_CONFIG0 0x60
  51. #define ASPEED_AHB_REMAP_LO_ADDR(x) (x & GENMASK(15, 4))
  52. #define ASPEED_AHB_MASK_LO_ADDR(x) FIELD_PREP(GENMASK(31, 20), x)
  53. #define ASPEED_H2X_AHB_ADDR_CONFIG1 0x64
  54. #define ASPEED_AHB_REMAP_HI_ADDR(x) (x)
  55. #define ASPEED_H2X_AHB_ADDR_CONFIG2 0x68
  56. #define ASPEED_AHB_MASK_HI_ADDR(x) (x)
  57. #define ASPEED_H2X_DEV_CTRL 0xc0
  58. #define ASPEED_PCIE_RX_DMA_EN BIT(9)
  59. #define ASPEED_PCIE_RX_LINEAR BIT(8)
  60. #define ASPEED_PCIE_RX_MSI_SEL BIT(7)
  61. #define ASPEED_PCIE_RX_MSI_EN BIT(6)
  62. #define ASPEED_PCIE_UNLOCK_RX_BUFF BIT(4)
  63. #define ASPEED_PCIE_WAIT_RX_TLP_CLR BIT(2)
  64. #define ASPEED_PCIE_RC_RX_ENABLE BIT(1)
  65. #define ASPEED_PCIE_RC_ENABLE BIT(0)
  66. #define ASPEED_H2X_DEV_STS 0xc8
  67. #define ASPEED_PCIE_RC_RX_DONE_ISR BIT(4)
  68. #define ASPEED_H2X_DEV_RX_DESC_DATA 0xcc
  69. #define ASPEED_H2X_DEV_RX_DESC1 0xd4
  70. #define ASPEED_H2X_DEV_TX_TAG 0xfc
  71. #define ASPEED_RC_TLP_TX_TAG_NUM 0x28
  72. /* AST2700 H2X */
  73. #define ASPEED_H2X_CTRL 0x00
  74. #define ASPEED_H2X_BRIDGE_EN BIT(0)
  75. #define ASPEED_H2X_BRIDGE_DIRECT_EN BIT(1)
  76. #define ASPEED_H2X_CFGE_INT_STS 0x08
  77. #define ASPEED_CFGE_TX_IDLE BIT(0)
  78. #define ASPEED_CFGE_RX_BUSY BIT(1)
  79. #define ASPEED_H2X_CFGI_TLP 0x20
  80. #define ASPEED_CFGI_BYTE_EN_MASK GENMASK(19, 16)
  81. #define ASPEED_CFGI_BYTE_EN(x) \
  82. FIELD_PREP(ASPEED_CFGI_BYTE_EN_MASK, (x))
  83. #define ASPEED_H2X_CFGI_WR_DATA 0x24
  84. #define ASPEED_CFGI_WRITE BIT(20)
  85. #define ASPEED_H2X_CFGI_CTRL 0x28
  86. #define ASPEED_CFGI_TLP_FIRE BIT(0)
  87. #define ASPEED_H2X_CFGI_RET_DATA 0x2c
  88. #define ASPEED_H2X_CFGE_TLP_1ST 0x30
  89. #define ASPEED_H2X_CFGE_TLP_NEXT 0x34
  90. #define ASPEED_H2X_CFGE_CTRL 0x38
  91. #define ASPEED_CFGE_TLP_FIRE BIT(0)
  92. #define ASPEED_H2X_CFGE_RET_DATA 0x3c
  93. #define ASPEED_H2X_REMAP_PREF_ADDR 0x70
  94. #define ASPEED_REMAP_PREF_ADDR_63_32(x) (x)
  95. #define ASPEED_H2X_REMAP_PCI_ADDR_HI 0x74
  96. #define ASPEED_REMAP_PCI_ADDR_63_32(x) (((x) >> 32) & GENMASK(31, 0))
  97. #define ASPEED_H2X_REMAP_PCI_ADDR_LO 0x78
  98. #define ASPEED_REMAP_PCI_ADDR_31_12(x) ((x) & GENMASK(31, 12))
  99. /* AST2700 SCU */
  100. #define ASPEED_SCU_60 0x60
  101. #define ASPEED_RC_E2M_PATH_EN BIT(0)
  102. #define ASPEED_RC_H2XS_PATH_EN BIT(16)
  103. #define ASPEED_RC_H2XD_PATH_EN BIT(17)
  104. #define ASPEED_RC_H2XX_PATH_EN BIT(18)
  105. #define ASPEED_RC_UPSTREAM_MEM_EN BIT(19)
  106. #define ASPEED_SCU_64 0x64
  107. #define ASPEED_RC0_DECODE_DMA_BASE(x) FIELD_PREP(GENMASK(7, 0), x)
  108. #define ASPEED_RC0_DECODE_DMA_LIMIT(x) FIELD_PREP(GENMASK(15, 8), x)
  109. #define ASPEED_RC1_DECODE_DMA_BASE(x) FIELD_PREP(GENMASK(23, 16), x)
  110. #define ASPEED_RC1_DECODE_DMA_LIMIT(x) FIELD_PREP(GENMASK(31, 24), x)
  111. #define ASPEED_SCU_70 0x70
  112. #define ASPEED_DISABLE_EP_FUNC 0
  113. /* Macro to combine Fmt and Type into the 8-bit field */
  114. #define ASPEED_TLP_FMT_TYPE(fmt, type) ((((fmt) & 0x7) << 5) | ((type) & 0x1f))
  115. #define ASPEED_TLP_COMMON_FIELDS GENMASK(31, 24)
  116. /* Completion status */
  117. #define CPL_STS(x) FIELD_GET(GENMASK(15, 13), (x))
  118. /* TLP configuration type 0 and type 1 */
  119. #define CFG0_READ_FMTTYPE \
  120. FIELD_PREP(ASPEED_TLP_COMMON_FIELDS, \
  121. ASPEED_TLP_FMT_TYPE(PCIE_TLP_FMT_3DW_NO_DATA, \
  122. PCIE_TLP_TYPE_CFG0_RD))
  123. #define CFG0_WRITE_FMTTYPE \
  124. FIELD_PREP(ASPEED_TLP_COMMON_FIELDS, \
  125. ASPEED_TLP_FMT_TYPE(PCIE_TLP_FMT_3DW_DATA, \
  126. PCIE_TLP_TYPE_CFG0_WR))
  127. #define CFG1_READ_FMTTYPE \
  128. FIELD_PREP(ASPEED_TLP_COMMON_FIELDS, \
  129. ASPEED_TLP_FMT_TYPE(PCIE_TLP_FMT_3DW_NO_DATA, \
  130. PCIE_TLP_TYPE_CFG1_RD))
  131. #define CFG1_WRITE_FMTTYPE \
  132. FIELD_PREP(ASPEED_TLP_COMMON_FIELDS, \
  133. ASPEED_TLP_FMT_TYPE(PCIE_TLP_FMT_3DW_DATA, \
  134. PCIE_TLP_TYPE_CFG1_WR))
  135. #define CFG_PAYLOAD_SIZE 0x01 /* 1 DWORD */
  136. #define TLP_HEADER_BYTE_EN(x, y) ((GENMASK((x) - 1, 0) << ((y) % 4)))
  137. #define TLP_GET_VALUE(x, y, z) \
  138. (((x) >> ((((z) % 4)) * 8)) & GENMASK((8 * (y)) - 1, 0))
  139. #define TLP_SET_VALUE(x, y, z) \
  140. ((((x) & GENMASK((8 * (y)) - 1, 0)) << ((((z) % 4)) * 8)))
  141. #define AST2600_TX_DESC1_VALUE 0x00002000
  142. #define AST2700_TX_DESC1_VALUE 0x00401000
  143. /**
  144. * struct aspeed_pcie_port - PCIe port information
  145. * @list: port list
  146. * @pcie: pointer to PCIe host info
  147. * @clk: pointer to the port clock gate
  148. * @phy: pointer to PCIe PHY
  149. * @perst: pointer to port reset control
  150. * @slot: port slot
  151. */
  152. struct aspeed_pcie_port {
  153. struct list_head list;
  154. struct aspeed_pcie *pcie;
  155. struct clk *clk;
  156. struct phy *phy;
  157. struct reset_control *perst;
  158. u32 slot;
  159. };
  160. /**
  161. * struct aspeed_pcie - PCIe RC information
  162. * @host: pointer to PCIe host bridge
  163. * @dev: pointer to device structure
  164. * @reg: PCIe host register base address
  165. * @ahbc: pointer to AHHC register map
  166. * @cfg: pointer to Aspeed PCIe configuration register map
  167. * @platform: platform specific information
  168. * @ports: list of PCIe ports
  169. * @tx_tag: current TX tag for the port
  170. * @root_bus_nr: bus number of the host bridge
  171. * @h2xrst: pointer to H2X reset control
  172. * @intx_domain: IRQ domain for INTx interrupts
  173. * @msi_domain: IRQ domain for MSI interrupts
  174. * @lock: mutex to protect MSI bitmap variable
  175. * @msi_irq_in_use: bitmap to track used MSI host IRQs
  176. * @clear_msi_twice: AST2700 workaround to clear MSI status twice
  177. */
  178. struct aspeed_pcie {
  179. struct pci_host_bridge *host;
  180. struct device *dev;
  181. void __iomem *reg;
  182. struct regmap *ahbc;
  183. struct regmap *cfg;
  184. const struct aspeed_pcie_rc_platform *platform;
  185. struct list_head ports;
  186. u8 tx_tag;
  187. u8 root_bus_nr;
  188. struct reset_control *h2xrst;
  189. struct irq_domain *intx_domain;
  190. struct irq_domain *msi_domain;
  191. struct mutex lock;
  192. DECLARE_BITMAP(msi_irq_in_use, MAX_MSI_HOST_IRQS);
  193. bool clear_msi_twice; /* AST2700 workaround */
  194. };
  195. /**
  196. * struct aspeed_pcie_rc_platform - Platform information
  197. * @setup: initialization function
  198. * @pcie_map_ranges: function to map PCIe address ranges
  199. * @reg_intx_en: INTx enable register offset
  200. * @reg_intx_sts: INTx status register offset
  201. * @reg_msi_en: MSI enable register offset
  202. * @reg_msi_sts: MSI enable register offset
  203. * @msi_address: HW fixed MSI address
  204. */
  205. struct aspeed_pcie_rc_platform {
  206. int (*setup)(struct platform_device *pdev);
  207. void (*pcie_map_ranges)(struct aspeed_pcie *pcie, u64 pci_addr);
  208. int reg_intx_en;
  209. int reg_intx_sts;
  210. int reg_msi_en;
  211. int reg_msi_sts;
  212. u32 msi_address;
  213. };
  214. static void aspeed_pcie_intx_irq_ack(struct irq_data *d)
  215. {
  216. struct aspeed_pcie *pcie = irq_data_get_irq_chip_data(d);
  217. int intx_en = pcie->platform->reg_intx_en;
  218. u32 en;
  219. en = readl(pcie->reg + intx_en);
  220. en |= BIT(d->hwirq);
  221. writel(en, pcie->reg + intx_en);
  222. }
  223. static void aspeed_pcie_intx_irq_mask(struct irq_data *d)
  224. {
  225. struct aspeed_pcie *pcie = irq_data_get_irq_chip_data(d);
  226. int intx_en = pcie->platform->reg_intx_en;
  227. u32 en;
  228. en = readl(pcie->reg + intx_en);
  229. en &= ~BIT(d->hwirq);
  230. writel(en, pcie->reg + intx_en);
  231. }
  232. static void aspeed_pcie_intx_irq_unmask(struct irq_data *d)
  233. {
  234. struct aspeed_pcie *pcie = irq_data_get_irq_chip_data(d);
  235. int intx_en = pcie->platform->reg_intx_en;
  236. u32 en;
  237. en = readl(pcie->reg + intx_en);
  238. en |= BIT(d->hwirq);
  239. writel(en, pcie->reg + intx_en);
  240. }
  241. static struct irq_chip aspeed_intx_irq_chip = {
  242. .name = "INTx",
  243. .irq_ack = aspeed_pcie_intx_irq_ack,
  244. .irq_mask = aspeed_pcie_intx_irq_mask,
  245. .irq_unmask = aspeed_pcie_intx_irq_unmask,
  246. };
  247. static int aspeed_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  248. irq_hw_number_t hwirq)
  249. {
  250. irq_set_chip_and_handler(irq, &aspeed_intx_irq_chip, handle_level_irq);
  251. irq_set_chip_data(irq, domain->host_data);
  252. irq_set_status_flags(irq, IRQ_LEVEL);
  253. return 0;
  254. }
  255. static const struct irq_domain_ops aspeed_intx_domain_ops = {
  256. .map = aspeed_pcie_intx_map,
  257. };
  258. static irqreturn_t aspeed_pcie_intr_handler(int irq, void *dev_id)
  259. {
  260. struct aspeed_pcie *pcie = dev_id;
  261. const struct aspeed_pcie_rc_platform *platform = pcie->platform;
  262. unsigned long status;
  263. unsigned long intx;
  264. u32 bit;
  265. int i;
  266. intx = FIELD_GET(ASPEED_PCIE_INTX_STS,
  267. readl(pcie->reg + platform->reg_intx_sts));
  268. for_each_set_bit(bit, &intx, PCI_NUM_INTX)
  269. generic_handle_domain_irq(pcie->intx_domain, bit);
  270. for (i = 0; i < 2; i++) {
  271. int msi_sts_reg = platform->reg_msi_sts + (i * 4);
  272. status = readl(pcie->reg + msi_sts_reg);
  273. writel(status, pcie->reg + msi_sts_reg);
  274. /*
  275. * AST2700 workaround:
  276. * The MSI status needs to clear one more time.
  277. */
  278. if (pcie->clear_msi_twice)
  279. writel(status, pcie->reg + msi_sts_reg);
  280. for_each_set_bit(bit, &status, 32) {
  281. bit += (i * 32);
  282. generic_handle_domain_irq(pcie->msi_domain, bit);
  283. }
  284. }
  285. return IRQ_HANDLED;
  286. }
  287. static u32 aspeed_pcie_get_bdf_offset(struct pci_bus *bus, unsigned int devfn,
  288. int where)
  289. {
  290. return ((bus->number) << 24) | (PCI_SLOT(devfn) << 19) |
  291. (PCI_FUNC(devfn) << 16) | (where & ~3);
  292. }
  293. static int aspeed_ast2600_conf(struct pci_bus *bus, unsigned int devfn,
  294. int where, int size, u32 *val, u32 fmt_type,
  295. bool write)
  296. {
  297. struct aspeed_pcie *pcie = bus->sysdata;
  298. u32 bdf_offset, cfg_val, isr;
  299. int ret;
  300. bdf_offset = aspeed_pcie_get_bdf_offset(bus, devfn, where);
  301. /* Driver may set unlock RX buffer before triggering next TX config */
  302. cfg_val = readl(pcie->reg + ASPEED_H2X_DEV_CTRL);
  303. writel(ASPEED_PCIE_UNLOCK_RX_BUFF | cfg_val,
  304. pcie->reg + ASPEED_H2X_DEV_CTRL);
  305. cfg_val = fmt_type | CFG_PAYLOAD_SIZE;
  306. writel(cfg_val, pcie->reg + ASPEED_H2X_TX_DESC0);
  307. cfg_val = AST2600_TX_DESC1_VALUE |
  308. FIELD_PREP(GENMASK(11, 8), pcie->tx_tag) |
  309. TLP_HEADER_BYTE_EN(size, where);
  310. writel(cfg_val, pcie->reg + ASPEED_H2X_TX_DESC1);
  311. writel(bdf_offset, pcie->reg + ASPEED_H2X_TX_DESC2);
  312. writel(0, pcie->reg + ASPEED_H2X_TX_DESC3);
  313. if (write)
  314. writel(TLP_SET_VALUE(*val, size, where),
  315. pcie->reg + ASPEED_H2X_TX_DESC_DATA);
  316. cfg_val = readl(pcie->reg + ASPEED_H2X_STS);
  317. cfg_val |= ASPEED_PCIE_TRIGGER_TX;
  318. writel(cfg_val, pcie->reg + ASPEED_H2X_STS);
  319. ret = readl_poll_timeout(pcie->reg + ASPEED_H2X_STS, cfg_val,
  320. (cfg_val & ASPEED_PCIE_TX_IDLE), 0, 50);
  321. if (ret) {
  322. dev_err(pcie->dev,
  323. "%02x:%02x.%d CR tx timeout sts: 0x%08x\n",
  324. bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn), cfg_val);
  325. ret = PCIBIOS_SET_FAILED;
  326. PCI_SET_ERROR_RESPONSE(val);
  327. goto out;
  328. }
  329. cfg_val = readl(pcie->reg + ASPEED_H2X_INT_STS);
  330. cfg_val |= ASPEED_PCIE_TX_IDLE_CLEAR;
  331. writel(cfg_val, pcie->reg + ASPEED_H2X_INT_STS);
  332. cfg_val = readl(pcie->reg + ASPEED_H2X_STS);
  333. switch (cfg_val & ASPEED_PCIE_STATUS_OF_TX) {
  334. case ASPEED_PCIE_RC_H_TX_COMPLETE:
  335. ret = readl_poll_timeout(pcie->reg + ASPEED_H2X_DEV_STS, isr,
  336. (isr & ASPEED_PCIE_RC_RX_DONE_ISR), 0,
  337. 50);
  338. if (ret) {
  339. dev_err(pcie->dev,
  340. "%02x:%02x.%d CR rx timeout sts: 0x%08x\n",
  341. bus->number, PCI_SLOT(devfn),
  342. PCI_FUNC(devfn), isr);
  343. ret = PCIBIOS_SET_FAILED;
  344. PCI_SET_ERROR_RESPONSE(val);
  345. goto out;
  346. }
  347. if (!write) {
  348. cfg_val = readl(pcie->reg + ASPEED_H2X_DEV_RX_DESC1);
  349. if (CPL_STS(cfg_val) != PCIE_CPL_STS_SUCCESS) {
  350. ret = PCIBIOS_SET_FAILED;
  351. PCI_SET_ERROR_RESPONSE(val);
  352. goto out;
  353. } else {
  354. *val = readl(pcie->reg +
  355. ASPEED_H2X_DEV_RX_DESC_DATA);
  356. }
  357. }
  358. break;
  359. case ASPEED_PCIE_STATUS_OF_TX:
  360. ret = PCIBIOS_SET_FAILED;
  361. PCI_SET_ERROR_RESPONSE(val);
  362. goto out;
  363. default:
  364. *val = readl(pcie->reg + ASPEED_H2X_HOST_RX_DESC_DATA);
  365. break;
  366. }
  367. cfg_val = readl(pcie->reg + ASPEED_H2X_DEV_CTRL);
  368. cfg_val |= ASPEED_PCIE_UNLOCK_RX_BUFF;
  369. writel(cfg_val, pcie->reg + ASPEED_H2X_DEV_CTRL);
  370. *val = TLP_GET_VALUE(*val, size, where);
  371. ret = PCIBIOS_SUCCESSFUL;
  372. out:
  373. cfg_val = readl(pcie->reg + ASPEED_H2X_DEV_STS);
  374. writel(cfg_val, pcie->reg + ASPEED_H2X_DEV_STS);
  375. pcie->tx_tag = (pcie->tx_tag + 1) % 0x8;
  376. return ret;
  377. }
  378. static int aspeed_ast2600_rd_conf(struct pci_bus *bus, unsigned int devfn,
  379. int where, int size, u32 *val)
  380. {
  381. /*
  382. * AST2600 has only one Root Port on the root bus.
  383. */
  384. if (PCI_SLOT(devfn) != 8)
  385. return PCIBIOS_DEVICE_NOT_FOUND;
  386. return aspeed_ast2600_conf(bus, devfn, where, size, val,
  387. CFG0_READ_FMTTYPE, false);
  388. }
  389. static int aspeed_ast2600_child_rd_conf(struct pci_bus *bus, unsigned int devfn,
  390. int where, int size, u32 *val)
  391. {
  392. return aspeed_ast2600_conf(bus, devfn, where, size, val,
  393. CFG1_READ_FMTTYPE, false);
  394. }
  395. static int aspeed_ast2600_wr_conf(struct pci_bus *bus, unsigned int devfn,
  396. int where, int size, u32 val)
  397. {
  398. /*
  399. * AST2600 has only one Root Port on the root bus.
  400. */
  401. if (PCI_SLOT(devfn) != 8)
  402. return PCIBIOS_DEVICE_NOT_FOUND;
  403. return aspeed_ast2600_conf(bus, devfn, where, size, &val,
  404. CFG0_WRITE_FMTTYPE, true);
  405. }
  406. static int aspeed_ast2600_child_wr_conf(struct pci_bus *bus, unsigned int devfn,
  407. int where, int size, u32 val)
  408. {
  409. return aspeed_ast2600_conf(bus, devfn, where, size, &val,
  410. CFG1_WRITE_FMTTYPE, true);
  411. }
  412. static int aspeed_ast2700_config(struct pci_bus *bus, unsigned int devfn,
  413. int where, int size, u32 *val, bool write)
  414. {
  415. struct aspeed_pcie *pcie = bus->sysdata;
  416. u32 cfg_val;
  417. cfg_val = ASPEED_CFGI_BYTE_EN(TLP_HEADER_BYTE_EN(size, where)) |
  418. (where & ~3);
  419. if (write)
  420. cfg_val |= ASPEED_CFGI_WRITE;
  421. writel(cfg_val, pcie->reg + ASPEED_H2X_CFGI_TLP);
  422. writel(TLP_SET_VALUE(*val, size, where),
  423. pcie->reg + ASPEED_H2X_CFGI_WR_DATA);
  424. writel(ASPEED_CFGI_TLP_FIRE, pcie->reg + ASPEED_H2X_CFGI_CTRL);
  425. *val = readl(pcie->reg + ASPEED_H2X_CFGI_RET_DATA);
  426. *val = TLP_GET_VALUE(*val, size, where);
  427. return PCIBIOS_SUCCESSFUL;
  428. }
  429. static int aspeed_ast2700_child_config(struct pci_bus *bus, unsigned int devfn,
  430. int where, int size, u32 *val,
  431. bool write)
  432. {
  433. struct aspeed_pcie *pcie = bus->sysdata;
  434. u32 bdf_offset, status, cfg_val;
  435. int ret;
  436. bdf_offset = aspeed_pcie_get_bdf_offset(bus, devfn, where);
  437. cfg_val = CFG_PAYLOAD_SIZE;
  438. if (write)
  439. cfg_val |= (bus->number == (pcie->root_bus_nr + 1)) ?
  440. CFG0_WRITE_FMTTYPE :
  441. CFG1_WRITE_FMTTYPE;
  442. else
  443. cfg_val |= (bus->number == (pcie->root_bus_nr + 1)) ?
  444. CFG0_READ_FMTTYPE :
  445. CFG1_READ_FMTTYPE;
  446. writel(cfg_val, pcie->reg + ASPEED_H2X_CFGE_TLP_1ST);
  447. cfg_val = AST2700_TX_DESC1_VALUE |
  448. FIELD_PREP(GENMASK(11, 8), pcie->tx_tag) |
  449. TLP_HEADER_BYTE_EN(size, where);
  450. writel(cfg_val, pcie->reg + ASPEED_H2X_CFGE_TLP_NEXT);
  451. writel(bdf_offset, pcie->reg + ASPEED_H2X_CFGE_TLP_NEXT);
  452. if (write)
  453. writel(TLP_SET_VALUE(*val, size, where),
  454. pcie->reg + ASPEED_H2X_CFGE_TLP_NEXT);
  455. writel(ASPEED_CFGE_TX_IDLE | ASPEED_CFGE_RX_BUSY,
  456. pcie->reg + ASPEED_H2X_CFGE_INT_STS);
  457. writel(ASPEED_CFGE_TLP_FIRE, pcie->reg + ASPEED_H2X_CFGE_CTRL);
  458. ret = readl_poll_timeout(pcie->reg + ASPEED_H2X_CFGE_INT_STS, status,
  459. (status & ASPEED_CFGE_TX_IDLE), 0, 50);
  460. if (ret) {
  461. dev_err(pcie->dev,
  462. "%02x:%02x.%d CR tx timeout sts: 0x%08x\n",
  463. bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn), status);
  464. ret = PCIBIOS_SET_FAILED;
  465. PCI_SET_ERROR_RESPONSE(val);
  466. goto out;
  467. }
  468. ret = readl_poll_timeout(pcie->reg + ASPEED_H2X_CFGE_INT_STS, status,
  469. (status & ASPEED_CFGE_RX_BUSY), 0, 50);
  470. if (ret) {
  471. dev_err(pcie->dev,
  472. "%02x:%02x.%d CR rx timeout sts: 0x%08x\n",
  473. bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn), status);
  474. ret = PCIBIOS_SET_FAILED;
  475. PCI_SET_ERROR_RESPONSE(val);
  476. goto out;
  477. }
  478. *val = readl(pcie->reg + ASPEED_H2X_CFGE_RET_DATA);
  479. *val = TLP_GET_VALUE(*val, size, where);
  480. ret = PCIBIOS_SUCCESSFUL;
  481. out:
  482. writel(status, pcie->reg + ASPEED_H2X_CFGE_INT_STS);
  483. pcie->tx_tag = (pcie->tx_tag + 1) % 0xf;
  484. return ret;
  485. }
  486. static int aspeed_ast2700_rd_conf(struct pci_bus *bus, unsigned int devfn,
  487. int where, int size, u32 *val)
  488. {
  489. /*
  490. * AST2700 has only one Root Port on the root bus.
  491. */
  492. if (devfn != 0)
  493. return PCIBIOS_DEVICE_NOT_FOUND;
  494. return aspeed_ast2700_config(bus, devfn, where, size, val, false);
  495. }
  496. static int aspeed_ast2700_child_rd_conf(struct pci_bus *bus, unsigned int devfn,
  497. int where, int size, u32 *val)
  498. {
  499. return aspeed_ast2700_child_config(bus, devfn, where, size, val, false);
  500. }
  501. static int aspeed_ast2700_wr_conf(struct pci_bus *bus, unsigned int devfn,
  502. int where, int size, u32 val)
  503. {
  504. /*
  505. * AST2700 has only one Root Port on the root bus.
  506. */
  507. if (devfn != 0)
  508. return PCIBIOS_DEVICE_NOT_FOUND;
  509. return aspeed_ast2700_config(bus, devfn, where, size, &val, true);
  510. }
  511. static int aspeed_ast2700_child_wr_conf(struct pci_bus *bus, unsigned int devfn,
  512. int where, int size, u32 val)
  513. {
  514. return aspeed_ast2700_child_config(bus, devfn, where, size, &val, true);
  515. }
  516. static struct pci_ops aspeed_ast2600_pcie_ops = {
  517. .read = aspeed_ast2600_rd_conf,
  518. .write = aspeed_ast2600_wr_conf,
  519. };
  520. static struct pci_ops aspeed_ast2600_pcie_child_ops = {
  521. .read = aspeed_ast2600_child_rd_conf,
  522. .write = aspeed_ast2600_child_wr_conf,
  523. };
  524. static struct pci_ops aspeed_ast2700_pcie_ops = {
  525. .read = aspeed_ast2700_rd_conf,
  526. .write = aspeed_ast2700_wr_conf,
  527. };
  528. static struct pci_ops aspeed_ast2700_pcie_child_ops = {
  529. .read = aspeed_ast2700_child_rd_conf,
  530. .write = aspeed_ast2700_child_wr_conf,
  531. };
  532. static void aspeed_irq_compose_msi_msg(struct irq_data *data,
  533. struct msi_msg *msg)
  534. {
  535. struct aspeed_pcie *pcie = irq_data_get_irq_chip_data(data);
  536. msg->address_hi = 0;
  537. msg->address_lo = pcie->platform->msi_address;
  538. msg->data = data->hwirq;
  539. }
  540. static struct irq_chip aspeed_msi_bottom_irq_chip = {
  541. .name = "ASPEED MSI",
  542. .irq_compose_msi_msg = aspeed_irq_compose_msi_msg,
  543. };
  544. static int aspeed_irq_msi_domain_alloc(struct irq_domain *domain,
  545. unsigned int virq, unsigned int nr_irqs,
  546. void *args)
  547. {
  548. struct aspeed_pcie *pcie = domain->host_data;
  549. int bit;
  550. int i;
  551. guard(mutex)(&pcie->lock);
  552. bit = bitmap_find_free_region(pcie->msi_irq_in_use, MAX_MSI_HOST_IRQS,
  553. get_count_order(nr_irqs));
  554. if (bit < 0)
  555. return -ENOSPC;
  556. for (i = 0; i < nr_irqs; i++) {
  557. irq_domain_set_info(domain, virq + i, bit + i,
  558. &aspeed_msi_bottom_irq_chip,
  559. domain->host_data, handle_simple_irq, NULL,
  560. NULL);
  561. }
  562. return 0;
  563. }
  564. static void aspeed_irq_msi_domain_free(struct irq_domain *domain,
  565. unsigned int virq, unsigned int nr_irqs)
  566. {
  567. struct irq_data *data = irq_domain_get_irq_data(domain, virq);
  568. struct aspeed_pcie *pcie = irq_data_get_irq_chip_data(data);
  569. guard(mutex)(&pcie->lock);
  570. bitmap_release_region(pcie->msi_irq_in_use, data->hwirq,
  571. get_count_order(nr_irqs));
  572. }
  573. static const struct irq_domain_ops aspeed_msi_domain_ops = {
  574. .alloc = aspeed_irq_msi_domain_alloc,
  575. .free = aspeed_irq_msi_domain_free,
  576. };
  577. #define ASPEED_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \
  578. MSI_FLAG_USE_DEF_CHIP_OPS | \
  579. MSI_FLAG_NO_AFFINITY)
  580. #define ASPEED_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK | \
  581. MSI_FLAG_MULTI_PCI_MSI | \
  582. MSI_FLAG_PCI_MSIX)
  583. static const struct msi_parent_ops aspeed_msi_parent_ops = {
  584. .required_flags = ASPEED_MSI_FLAGS_REQUIRED,
  585. .supported_flags = ASPEED_MSI_FLAGS_SUPPORTED,
  586. .bus_select_token = DOMAIN_BUS_PCI_MSI,
  587. .chip_flags = MSI_CHIP_FLAG_SET_ACK,
  588. .prefix = "ASPEED-",
  589. .init_dev_msi_info = msi_lib_init_dev_msi_info,
  590. };
  591. static int aspeed_pcie_msi_init(struct aspeed_pcie *pcie)
  592. {
  593. writel(~0, pcie->reg + pcie->platform->reg_msi_en);
  594. writel(~0, pcie->reg + pcie->platform->reg_msi_en + 0x04);
  595. writel(~0, pcie->reg + pcie->platform->reg_msi_sts);
  596. writel(~0, pcie->reg + pcie->platform->reg_msi_sts + 0x04);
  597. struct irq_domain_info info = {
  598. .fwnode = dev_fwnode(pcie->dev),
  599. .ops = &aspeed_msi_domain_ops,
  600. .host_data = pcie,
  601. .size = MAX_MSI_HOST_IRQS,
  602. };
  603. pcie->msi_domain = msi_create_parent_irq_domain(&info,
  604. &aspeed_msi_parent_ops);
  605. if (!pcie->msi_domain)
  606. return dev_err_probe(pcie->dev, -ENOMEM,
  607. "failed to create MSI domain\n");
  608. return 0;
  609. }
  610. static void aspeed_pcie_msi_free(struct aspeed_pcie *pcie)
  611. {
  612. if (pcie->msi_domain) {
  613. irq_domain_remove(pcie->msi_domain);
  614. pcie->msi_domain = NULL;
  615. }
  616. }
  617. static void aspeed_pcie_irq_domain_free(void *d)
  618. {
  619. struct aspeed_pcie *pcie = d;
  620. if (pcie->intx_domain) {
  621. irq_domain_remove(pcie->intx_domain);
  622. pcie->intx_domain = NULL;
  623. }
  624. aspeed_pcie_msi_free(pcie);
  625. }
  626. static int aspeed_pcie_init_irq_domain(struct aspeed_pcie *pcie)
  627. {
  628. int ret;
  629. pcie->intx_domain = irq_domain_add_linear(pcie->dev->of_node,
  630. PCI_NUM_INTX,
  631. &aspeed_intx_domain_ops,
  632. pcie);
  633. if (!pcie->intx_domain) {
  634. ret = dev_err_probe(pcie->dev, -ENOMEM,
  635. "failed to get INTx IRQ domain\n");
  636. goto err;
  637. }
  638. writel(0, pcie->reg + pcie->platform->reg_intx_en);
  639. writel(~0, pcie->reg + pcie->platform->reg_intx_sts);
  640. ret = aspeed_pcie_msi_init(pcie);
  641. if (ret)
  642. goto err;
  643. return 0;
  644. err:
  645. aspeed_pcie_irq_domain_free(pcie);
  646. return ret;
  647. }
  648. static int aspeed_pcie_port_init(struct aspeed_pcie_port *port)
  649. {
  650. struct aspeed_pcie *pcie = port->pcie;
  651. struct device *dev = pcie->dev;
  652. int ret;
  653. ret = clk_prepare_enable(port->clk);
  654. if (ret)
  655. return dev_err_probe(dev, ret,
  656. "failed to set clock for slot (%d)\n",
  657. port->slot);
  658. ret = phy_init(port->phy);
  659. if (ret)
  660. return dev_err_probe(dev, ret,
  661. "failed to init phy pcie for slot (%d)\n",
  662. port->slot);
  663. ret = phy_set_mode_ext(port->phy, PHY_MODE_PCIE, PHY_MODE_PCIE_RC);
  664. if (ret)
  665. return dev_err_probe(dev, ret,
  666. "failed to set phy mode for slot (%d)\n",
  667. port->slot);
  668. reset_control_deassert(port->perst);
  669. msleep(PCIE_RESET_CONFIG_WAIT_MS);
  670. return 0;
  671. }
  672. static void aspeed_host_reset(struct aspeed_pcie *pcie)
  673. {
  674. reset_control_assert(pcie->h2xrst);
  675. mdelay(ASPEED_RESET_RC_WAIT_MS);
  676. reset_control_deassert(pcie->h2xrst);
  677. }
  678. static void aspeed_pcie_map_ranges(struct aspeed_pcie *pcie)
  679. {
  680. struct pci_host_bridge *bridge = pcie->host;
  681. struct resource_entry *window;
  682. resource_list_for_each_entry(window, &bridge->windows) {
  683. u64 pci_addr;
  684. if (resource_type(window->res) != IORESOURCE_MEM)
  685. continue;
  686. pci_addr = window->res->start - window->offset;
  687. pcie->platform->pcie_map_ranges(pcie, pci_addr);
  688. break;
  689. }
  690. }
  691. static void aspeed_ast2600_pcie_map_ranges(struct aspeed_pcie *pcie,
  692. u64 pci_addr)
  693. {
  694. u32 pci_addr_lo = pci_addr & GENMASK(31, 0);
  695. u32 pci_addr_hi = (pci_addr >> 32) & GENMASK(31, 0);
  696. pci_addr_lo >>= 16;
  697. writel(ASPEED_AHB_REMAP_LO_ADDR(pci_addr_lo) |
  698. ASPEED_AHB_MASK_LO_ADDR(0xe00),
  699. pcie->reg + ASPEED_H2X_AHB_ADDR_CONFIG0);
  700. writel(ASPEED_AHB_REMAP_HI_ADDR(pci_addr_hi),
  701. pcie->reg + ASPEED_H2X_AHB_ADDR_CONFIG1);
  702. writel(ASPEED_AHB_MASK_HI_ADDR(~0),
  703. pcie->reg + ASPEED_H2X_AHB_ADDR_CONFIG2);
  704. }
  705. static int aspeed_ast2600_setup(struct platform_device *pdev)
  706. {
  707. struct aspeed_pcie *pcie = platform_get_drvdata(pdev);
  708. struct device *dev = pcie->dev;
  709. pcie->ahbc = syscon_regmap_lookup_by_phandle(dev->of_node,
  710. "aspeed,ahbc");
  711. if (IS_ERR(pcie->ahbc))
  712. return dev_err_probe(dev, PTR_ERR(pcie->ahbc),
  713. "failed to map ahbc base\n");
  714. aspeed_host_reset(pcie);
  715. regmap_write(pcie->ahbc, ASPEED_AHBC_KEY, ASPEED_AHBC_UNLOCK_KEY);
  716. regmap_update_bits(pcie->ahbc, ASPEED_AHBC_ADDR_MAPPING,
  717. ASPEED_PCIE_RC_MEMORY_EN, ASPEED_PCIE_RC_MEMORY_EN);
  718. regmap_write(pcie->ahbc, ASPEED_AHBC_KEY, ASPEED_AHBC_UNLOCK);
  719. writel(ASPEED_H2X_BRIDGE_EN, pcie->reg + ASPEED_H2X_CTRL);
  720. writel(ASPEED_PCIE_RX_DMA_EN | ASPEED_PCIE_RX_LINEAR |
  721. ASPEED_PCIE_RX_MSI_SEL | ASPEED_PCIE_RX_MSI_EN |
  722. ASPEED_PCIE_WAIT_RX_TLP_CLR | ASPEED_PCIE_RC_RX_ENABLE |
  723. ASPEED_PCIE_RC_ENABLE,
  724. pcie->reg + ASPEED_H2X_DEV_CTRL);
  725. writel(ASPEED_RC_TLP_TX_TAG_NUM, pcie->reg + ASPEED_H2X_DEV_TX_TAG);
  726. pcie->host->ops = &aspeed_ast2600_pcie_ops;
  727. pcie->host->child_ops = &aspeed_ast2600_pcie_child_ops;
  728. return 0;
  729. }
  730. static void aspeed_ast2700_pcie_map_ranges(struct aspeed_pcie *pcie,
  731. u64 pci_addr)
  732. {
  733. writel(ASPEED_REMAP_PCI_ADDR_31_12(pci_addr),
  734. pcie->reg + ASPEED_H2X_REMAP_PCI_ADDR_LO);
  735. writel(ASPEED_REMAP_PCI_ADDR_63_32(pci_addr),
  736. pcie->reg + ASPEED_H2X_REMAP_PCI_ADDR_HI);
  737. }
  738. static int aspeed_ast2700_setup(struct platform_device *pdev)
  739. {
  740. struct aspeed_pcie *pcie = platform_get_drvdata(pdev);
  741. struct device *dev = pcie->dev;
  742. pcie->cfg = syscon_regmap_lookup_by_phandle(dev->of_node,
  743. "aspeed,pciecfg");
  744. if (IS_ERR(pcie->cfg))
  745. return dev_err_probe(dev, PTR_ERR(pcie->cfg),
  746. "failed to map pciecfg base\n");
  747. regmap_update_bits(pcie->cfg, ASPEED_SCU_60,
  748. ASPEED_RC_E2M_PATH_EN | ASPEED_RC_H2XS_PATH_EN |
  749. ASPEED_RC_H2XD_PATH_EN | ASPEED_RC_H2XX_PATH_EN |
  750. ASPEED_RC_UPSTREAM_MEM_EN,
  751. ASPEED_RC_E2M_PATH_EN | ASPEED_RC_H2XS_PATH_EN |
  752. ASPEED_RC_H2XD_PATH_EN | ASPEED_RC_H2XX_PATH_EN |
  753. ASPEED_RC_UPSTREAM_MEM_EN);
  754. regmap_write(pcie->cfg, ASPEED_SCU_64,
  755. ASPEED_RC0_DECODE_DMA_BASE(0) |
  756. ASPEED_RC0_DECODE_DMA_LIMIT(0xff) |
  757. ASPEED_RC1_DECODE_DMA_BASE(0) |
  758. ASPEED_RC1_DECODE_DMA_LIMIT(0xff));
  759. regmap_write(pcie->cfg, ASPEED_SCU_70, ASPEED_DISABLE_EP_FUNC);
  760. aspeed_host_reset(pcie);
  761. writel(0, pcie->reg + ASPEED_H2X_CTRL);
  762. writel(ASPEED_H2X_BRIDGE_EN | ASPEED_H2X_BRIDGE_DIRECT_EN,
  763. pcie->reg + ASPEED_H2X_CTRL);
  764. /* Prepare for 64-bit BAR pref */
  765. writel(ASPEED_REMAP_PREF_ADDR_63_32(0x3),
  766. pcie->reg + ASPEED_H2X_REMAP_PREF_ADDR);
  767. pcie->host->ops = &aspeed_ast2700_pcie_ops;
  768. pcie->host->child_ops = &aspeed_ast2700_pcie_child_ops;
  769. pcie->clear_msi_twice = true;
  770. return 0;
  771. }
  772. static void aspeed_pcie_reset_release(void *d)
  773. {
  774. struct reset_control *perst = d;
  775. if (!perst)
  776. return;
  777. reset_control_put(perst);
  778. }
  779. static int aspeed_pcie_parse_port(struct aspeed_pcie *pcie,
  780. struct device_node *node,
  781. int slot)
  782. {
  783. struct aspeed_pcie_port *port;
  784. struct device *dev = pcie->dev;
  785. int ret;
  786. port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
  787. if (!port)
  788. return -ENOMEM;
  789. port->clk = devm_get_clk_from_child(dev, node, NULL);
  790. if (IS_ERR(port->clk))
  791. return dev_err_probe(dev, PTR_ERR(port->clk),
  792. "failed to get pcie%d clock\n", slot);
  793. port->phy = devm_of_phy_get(dev, node, NULL);
  794. if (IS_ERR(port->phy))
  795. return dev_err_probe(dev, PTR_ERR(port->phy),
  796. "failed to get phy pcie%d\n", slot);
  797. port->perst = of_reset_control_get_exclusive(node, "perst");
  798. if (IS_ERR(port->perst))
  799. return dev_err_probe(dev, PTR_ERR(port->perst),
  800. "failed to get pcie%d reset control\n",
  801. slot);
  802. ret = devm_add_action_or_reset(dev, aspeed_pcie_reset_release,
  803. port->perst);
  804. if (ret)
  805. return ret;
  806. reset_control_assert(port->perst);
  807. port->slot = slot;
  808. port->pcie = pcie;
  809. INIT_LIST_HEAD(&port->list);
  810. list_add_tail(&port->list, &pcie->ports);
  811. ret = aspeed_pcie_port_init(port);
  812. if (ret)
  813. return ret;
  814. return 0;
  815. }
  816. static int aspeed_pcie_parse_dt(struct aspeed_pcie *pcie)
  817. {
  818. struct device *dev = pcie->dev;
  819. struct device_node *node = dev->of_node;
  820. int ret;
  821. for_each_available_child_of_node_scoped(node, child) {
  822. int slot;
  823. const char *type;
  824. ret = of_property_read_string(child, "device_type", &type);
  825. if (ret || strcmp(type, "pci"))
  826. continue;
  827. ret = of_pci_get_devfn(child);
  828. if (ret < 0)
  829. return dev_err_probe(dev, ret,
  830. "failed to parse devfn\n");
  831. slot = PCI_SLOT(ret);
  832. ret = aspeed_pcie_parse_port(pcie, child, slot);
  833. if (ret)
  834. return ret;
  835. }
  836. if (list_empty(&pcie->ports))
  837. return dev_err_probe(dev, -ENODEV,
  838. "No PCIe port found in DT\n");
  839. return 0;
  840. }
  841. static int aspeed_pcie_probe(struct platform_device *pdev)
  842. {
  843. struct device *dev = &pdev->dev;
  844. struct pci_host_bridge *host;
  845. struct aspeed_pcie *pcie;
  846. struct resource_entry *entry;
  847. const struct aspeed_pcie_rc_platform *md;
  848. int irq, ret;
  849. md = of_device_get_match_data(dev);
  850. if (!md)
  851. return -ENODEV;
  852. host = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
  853. if (!host)
  854. return -ENOMEM;
  855. pcie = pci_host_bridge_priv(host);
  856. pcie->dev = dev;
  857. pcie->tx_tag = 0;
  858. platform_set_drvdata(pdev, pcie);
  859. pcie->platform = md;
  860. pcie->host = host;
  861. INIT_LIST_HEAD(&pcie->ports);
  862. /* Get root bus num for cfg command to decide tlp type 0 or type 1 */
  863. entry = resource_list_first_type(&host->windows, IORESOURCE_BUS);
  864. if (entry)
  865. pcie->root_bus_nr = entry->res->start;
  866. pcie->reg = devm_platform_ioremap_resource(pdev, 0);
  867. if (IS_ERR(pcie->reg))
  868. return PTR_ERR(pcie->reg);
  869. pcie->h2xrst = devm_reset_control_get_exclusive(dev, "h2x");
  870. if (IS_ERR(pcie->h2xrst))
  871. return dev_err_probe(dev, PTR_ERR(pcie->h2xrst),
  872. "failed to get h2x reset\n");
  873. ret = devm_mutex_init(dev, &pcie->lock);
  874. if (ret)
  875. return dev_err_probe(dev, ret, "failed to init mutex\n");
  876. ret = pcie->platform->setup(pdev);
  877. if (ret)
  878. return dev_err_probe(dev, ret, "failed to setup PCIe RC\n");
  879. aspeed_pcie_map_ranges(pcie);
  880. ret = aspeed_pcie_parse_dt(pcie);
  881. if (ret)
  882. return ret;
  883. host->sysdata = pcie;
  884. ret = aspeed_pcie_init_irq_domain(pcie);
  885. if (ret)
  886. return ret;
  887. irq = platform_get_irq(pdev, 0);
  888. if (irq < 0)
  889. return irq;
  890. ret = devm_add_action_or_reset(dev, aspeed_pcie_irq_domain_free, pcie);
  891. if (ret)
  892. return ret;
  893. ret = devm_request_irq(dev, irq, aspeed_pcie_intr_handler, IRQF_SHARED,
  894. dev_name(dev), pcie);
  895. if (ret)
  896. return ret;
  897. return pci_host_probe(host);
  898. }
  899. static const struct aspeed_pcie_rc_platform pcie_rc_ast2600 = {
  900. .setup = aspeed_ast2600_setup,
  901. .pcie_map_ranges = aspeed_ast2600_pcie_map_ranges,
  902. .reg_intx_en = 0xc4,
  903. .reg_intx_sts = 0xc8,
  904. .reg_msi_en = 0xe0,
  905. .reg_msi_sts = 0xe8,
  906. .msi_address = 0x1e77005c,
  907. };
  908. static const struct aspeed_pcie_rc_platform pcie_rc_ast2700 = {
  909. .setup = aspeed_ast2700_setup,
  910. .pcie_map_ranges = aspeed_ast2700_pcie_map_ranges,
  911. .reg_intx_en = 0x40,
  912. .reg_intx_sts = 0x48,
  913. .reg_msi_en = 0x50,
  914. .reg_msi_sts = 0x58,
  915. .msi_address = 0x000000f0,
  916. };
  917. static const struct of_device_id aspeed_pcie_of_match[] = {
  918. { .compatible = "aspeed,ast2600-pcie", .data = &pcie_rc_ast2600 },
  919. { .compatible = "aspeed,ast2700-pcie", .data = &pcie_rc_ast2700 },
  920. {}
  921. };
  922. static struct platform_driver aspeed_pcie_driver = {
  923. .driver = {
  924. .name = "aspeed-pcie",
  925. .of_match_table = aspeed_pcie_of_match,
  926. .suppress_bind_attrs = true,
  927. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  928. },
  929. .probe = aspeed_pcie_probe,
  930. };
  931. builtin_platform_driver(aspeed_pcie_driver);
  932. MODULE_AUTHOR("Jacky Chou <jacky_chou@aspeedtech.com>");
  933. MODULE_DESCRIPTION("ASPEED PCIe Root Complex");
  934. MODULE_LICENSE("GPL");