pcie-microchip-host.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Microchip AXI PCIe Bridge host controller driver
  4. *
  5. * Copyright (c) 2018 - 2020 Microchip Corporation. All rights reserved.
  6. *
  7. * Author: Daire McNamara <daire.mcnamara@microchip.com>
  8. */
  9. #include <linux/align.h>
  10. #include <linux/bits.h>
  11. #include <linux/bitfield.h>
  12. #include <linux/clk.h>
  13. #include <linux/irqchip/chained_irq.h>
  14. #include <linux/irqdomain.h>
  15. #include <linux/log2.h>
  16. #include <linux/module.h>
  17. #include <linux/msi.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_pci.h>
  20. #include <linux/pci-ecam.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/wordpart.h>
  23. #include "../../pci.h"
  24. #include "pcie-plda.h"
  25. #define MC_MAX_NUM_INBOUND_WINDOWS 8
  26. #define MPFS_NC_BOUNCE_ADDR 0x80000000
  27. /* PCIe Bridge Phy and Controller Phy offsets */
  28. #define MC_PCIE1_BRIDGE_ADDR 0x00008000u
  29. #define MC_PCIE1_CTRL_ADDR 0x0000a000u
  30. /* PCIe Controller Phy Regs */
  31. #define SEC_ERROR_EVENT_CNT 0x20
  32. #define DED_ERROR_EVENT_CNT 0x24
  33. #define SEC_ERROR_INT 0x28
  34. #define SEC_ERROR_INT_TX_RAM_SEC_ERR_INT GENMASK(3, 0)
  35. #define SEC_ERROR_INT_RX_RAM_SEC_ERR_INT GENMASK(7, 4)
  36. #define SEC_ERROR_INT_PCIE2AXI_RAM_SEC_ERR_INT GENMASK(11, 8)
  37. #define SEC_ERROR_INT_AXI2PCIE_RAM_SEC_ERR_INT GENMASK(15, 12)
  38. #define SEC_ERROR_INT_ALL_RAM_SEC_ERR_INT GENMASK(15, 0)
  39. #define NUM_SEC_ERROR_INTS (4)
  40. #define SEC_ERROR_INT_MASK 0x2c
  41. #define DED_ERROR_INT 0x30
  42. #define DED_ERROR_INT_TX_RAM_DED_ERR_INT GENMASK(3, 0)
  43. #define DED_ERROR_INT_RX_RAM_DED_ERR_INT GENMASK(7, 4)
  44. #define DED_ERROR_INT_PCIE2AXI_RAM_DED_ERR_INT GENMASK(11, 8)
  45. #define DED_ERROR_INT_AXI2PCIE_RAM_DED_ERR_INT GENMASK(15, 12)
  46. #define DED_ERROR_INT_ALL_RAM_DED_ERR_INT GENMASK(15, 0)
  47. #define NUM_DED_ERROR_INTS (4)
  48. #define DED_ERROR_INT_MASK 0x34
  49. #define ECC_CONTROL 0x38
  50. #define ECC_CONTROL_TX_RAM_INJ_ERROR_0 BIT(0)
  51. #define ECC_CONTROL_TX_RAM_INJ_ERROR_1 BIT(1)
  52. #define ECC_CONTROL_TX_RAM_INJ_ERROR_2 BIT(2)
  53. #define ECC_CONTROL_TX_RAM_INJ_ERROR_3 BIT(3)
  54. #define ECC_CONTROL_RX_RAM_INJ_ERROR_0 BIT(4)
  55. #define ECC_CONTROL_RX_RAM_INJ_ERROR_1 BIT(5)
  56. #define ECC_CONTROL_RX_RAM_INJ_ERROR_2 BIT(6)
  57. #define ECC_CONTROL_RX_RAM_INJ_ERROR_3 BIT(7)
  58. #define ECC_CONTROL_PCIE2AXI_RAM_INJ_ERROR_0 BIT(8)
  59. #define ECC_CONTROL_PCIE2AXI_RAM_INJ_ERROR_1 BIT(9)
  60. #define ECC_CONTROL_PCIE2AXI_RAM_INJ_ERROR_2 BIT(10)
  61. #define ECC_CONTROL_PCIE2AXI_RAM_INJ_ERROR_3 BIT(11)
  62. #define ECC_CONTROL_AXI2PCIE_RAM_INJ_ERROR_0 BIT(12)
  63. #define ECC_CONTROL_AXI2PCIE_RAM_INJ_ERROR_1 BIT(13)
  64. #define ECC_CONTROL_AXI2PCIE_RAM_INJ_ERROR_2 BIT(14)
  65. #define ECC_CONTROL_AXI2PCIE_RAM_INJ_ERROR_3 BIT(15)
  66. #define ECC_CONTROL_TX_RAM_ECC_BYPASS BIT(24)
  67. #define ECC_CONTROL_RX_RAM_ECC_BYPASS BIT(25)
  68. #define ECC_CONTROL_PCIE2AXI_RAM_ECC_BYPASS BIT(26)
  69. #define ECC_CONTROL_AXI2PCIE_RAM_ECC_BYPASS BIT(27)
  70. #define PCIE_EVENT_INT 0x14c
  71. #define PCIE_EVENT_INT_L2_EXIT_INT BIT(0)
  72. #define PCIE_EVENT_INT_HOTRST_EXIT_INT BIT(1)
  73. #define PCIE_EVENT_INT_DLUP_EXIT_INT BIT(2)
  74. #define PCIE_EVENT_INT_MASK GENMASK(2, 0)
  75. #define PCIE_EVENT_INT_L2_EXIT_INT_MASK BIT(16)
  76. #define PCIE_EVENT_INT_HOTRST_EXIT_INT_MASK BIT(17)
  77. #define PCIE_EVENT_INT_DLUP_EXIT_INT_MASK BIT(18)
  78. #define PCIE_EVENT_INT_ENB_MASK GENMASK(18, 16)
  79. #define PCIE_EVENT_INT_ENB_SHIFT 16
  80. #define NUM_PCIE_EVENTS (3)
  81. /* PCIe Config space MSI capability structure */
  82. #define MC_MSI_CAP_CTRL_OFFSET 0xe0u
  83. /* Events */
  84. #define EVENT_PCIE_L2_EXIT 0
  85. #define EVENT_PCIE_HOTRST_EXIT 1
  86. #define EVENT_PCIE_DLUP_EXIT 2
  87. #define EVENT_SEC_TX_RAM_SEC_ERR 3
  88. #define EVENT_SEC_RX_RAM_SEC_ERR 4
  89. #define EVENT_SEC_PCIE2AXI_RAM_SEC_ERR 5
  90. #define EVENT_SEC_AXI2PCIE_RAM_SEC_ERR 6
  91. #define EVENT_DED_TX_RAM_DED_ERR 7
  92. #define EVENT_DED_RX_RAM_DED_ERR 8
  93. #define EVENT_DED_PCIE2AXI_RAM_DED_ERR 9
  94. #define EVENT_DED_AXI2PCIE_RAM_DED_ERR 10
  95. #define EVENT_LOCAL_DMA_END_ENGINE_0 11
  96. #define EVENT_LOCAL_DMA_END_ENGINE_1 12
  97. #define EVENT_LOCAL_DMA_ERROR_ENGINE_0 13
  98. #define EVENT_LOCAL_DMA_ERROR_ENGINE_1 14
  99. #define NUM_MC_EVENTS 15
  100. #define EVENT_LOCAL_A_ATR_EVT_POST_ERR (NUM_MC_EVENTS + PLDA_AXI_POST_ERR)
  101. #define EVENT_LOCAL_A_ATR_EVT_FETCH_ERR (NUM_MC_EVENTS + PLDA_AXI_FETCH_ERR)
  102. #define EVENT_LOCAL_A_ATR_EVT_DISCARD_ERR (NUM_MC_EVENTS + PLDA_AXI_DISCARD_ERR)
  103. #define EVENT_LOCAL_A_ATR_EVT_DOORBELL (NUM_MC_EVENTS + PLDA_AXI_DOORBELL)
  104. #define EVENT_LOCAL_P_ATR_EVT_POST_ERR (NUM_MC_EVENTS + PLDA_PCIE_POST_ERR)
  105. #define EVENT_LOCAL_P_ATR_EVT_FETCH_ERR (NUM_MC_EVENTS + PLDA_PCIE_FETCH_ERR)
  106. #define EVENT_LOCAL_P_ATR_EVT_DISCARD_ERR (NUM_MC_EVENTS + PLDA_PCIE_DISCARD_ERR)
  107. #define EVENT_LOCAL_P_ATR_EVT_DOORBELL (NUM_MC_EVENTS + PLDA_PCIE_DOORBELL)
  108. #define EVENT_LOCAL_PM_MSI_INT_INTX (NUM_MC_EVENTS + PLDA_INTX)
  109. #define EVENT_LOCAL_PM_MSI_INT_MSI (NUM_MC_EVENTS + PLDA_MSI)
  110. #define EVENT_LOCAL_PM_MSI_INT_AER_EVT (NUM_MC_EVENTS + PLDA_AER_EVENT)
  111. #define EVENT_LOCAL_PM_MSI_INT_EVENTS (NUM_MC_EVENTS + PLDA_MISC_EVENTS)
  112. #define EVENT_LOCAL_PM_MSI_INT_SYS_ERR (NUM_MC_EVENTS + PLDA_SYS_ERR)
  113. #define NUM_EVENTS (NUM_MC_EVENTS + PLDA_INT_EVENT_NUM)
  114. #define PCIE_EVENT_CAUSE(x, s) \
  115. [EVENT_PCIE_ ## x] = { __stringify(x), s }
  116. #define SEC_ERROR_CAUSE(x, s) \
  117. [EVENT_SEC_ ## x] = { __stringify(x), s }
  118. #define DED_ERROR_CAUSE(x, s) \
  119. [EVENT_DED_ ## x] = { __stringify(x), s }
  120. #define LOCAL_EVENT_CAUSE(x, s) \
  121. [EVENT_LOCAL_ ## x] = { __stringify(x), s }
  122. #define PCIE_EVENT(x) \
  123. .offset = PCIE_EVENT_INT, \
  124. .mask_offset = PCIE_EVENT_INT, \
  125. .mask_high = 1, \
  126. .mask = PCIE_EVENT_INT_ ## x ## _INT, \
  127. .enb_mask = PCIE_EVENT_INT_ENB_MASK
  128. #define SEC_EVENT(x) \
  129. .offset = SEC_ERROR_INT, \
  130. .mask_offset = SEC_ERROR_INT_MASK, \
  131. .mask = SEC_ERROR_INT_ ## x ## _INT, \
  132. .mask_high = 1, \
  133. .enb_mask = 0
  134. #define DED_EVENT(x) \
  135. .offset = DED_ERROR_INT, \
  136. .mask_offset = DED_ERROR_INT_MASK, \
  137. .mask_high = 1, \
  138. .mask = DED_ERROR_INT_ ## x ## _INT, \
  139. .enb_mask = 0
  140. #define LOCAL_EVENT(x) \
  141. .offset = ISTATUS_LOCAL, \
  142. .mask_offset = IMASK_LOCAL, \
  143. .mask_high = 0, \
  144. .mask = x ## _MASK, \
  145. .enb_mask = 0
  146. #define PCIE_EVENT_TO_EVENT_MAP(x) \
  147. { PCIE_EVENT_INT_ ## x ## _INT, EVENT_PCIE_ ## x }
  148. #define SEC_ERROR_TO_EVENT_MAP(x) \
  149. { SEC_ERROR_INT_ ## x ## _INT, EVENT_SEC_ ## x }
  150. #define DED_ERROR_TO_EVENT_MAP(x) \
  151. { DED_ERROR_INT_ ## x ## _INT, EVENT_DED_ ## x }
  152. #define LOCAL_STATUS_TO_EVENT_MAP(x) \
  153. { x ## _MASK, EVENT_LOCAL_ ## x }
  154. struct event_map {
  155. u32 reg_mask;
  156. u32 event_bit;
  157. };
  158. struct mc_pcie {
  159. struct plda_pcie_rp plda;
  160. void __iomem *bridge_base_addr;
  161. void __iomem *ctrl_base_addr;
  162. };
  163. struct cause {
  164. const char *sym;
  165. const char *str;
  166. };
  167. static const struct cause event_cause[NUM_EVENTS] = {
  168. PCIE_EVENT_CAUSE(L2_EXIT, "L2 exit event"),
  169. PCIE_EVENT_CAUSE(HOTRST_EXIT, "Hot reset exit event"),
  170. PCIE_EVENT_CAUSE(DLUP_EXIT, "DLUP exit event"),
  171. SEC_ERROR_CAUSE(TX_RAM_SEC_ERR, "sec error in tx buffer"),
  172. SEC_ERROR_CAUSE(RX_RAM_SEC_ERR, "sec error in rx buffer"),
  173. SEC_ERROR_CAUSE(PCIE2AXI_RAM_SEC_ERR, "sec error in pcie2axi buffer"),
  174. SEC_ERROR_CAUSE(AXI2PCIE_RAM_SEC_ERR, "sec error in axi2pcie buffer"),
  175. DED_ERROR_CAUSE(TX_RAM_DED_ERR, "ded error in tx buffer"),
  176. DED_ERROR_CAUSE(RX_RAM_DED_ERR, "ded error in rx buffer"),
  177. DED_ERROR_CAUSE(PCIE2AXI_RAM_DED_ERR, "ded error in pcie2axi buffer"),
  178. DED_ERROR_CAUSE(AXI2PCIE_RAM_DED_ERR, "ded error in axi2pcie buffer"),
  179. LOCAL_EVENT_CAUSE(DMA_ERROR_ENGINE_0, "dma engine 0 error"),
  180. LOCAL_EVENT_CAUSE(DMA_ERROR_ENGINE_1, "dma engine 1 error"),
  181. LOCAL_EVENT_CAUSE(A_ATR_EVT_POST_ERR, "axi write request error"),
  182. LOCAL_EVENT_CAUSE(A_ATR_EVT_FETCH_ERR, "axi read request error"),
  183. LOCAL_EVENT_CAUSE(A_ATR_EVT_DISCARD_ERR, "axi read timeout"),
  184. LOCAL_EVENT_CAUSE(P_ATR_EVT_POST_ERR, "pcie write request error"),
  185. LOCAL_EVENT_CAUSE(P_ATR_EVT_FETCH_ERR, "pcie read request error"),
  186. LOCAL_EVENT_CAUSE(P_ATR_EVT_DISCARD_ERR, "pcie read timeout"),
  187. LOCAL_EVENT_CAUSE(PM_MSI_INT_AER_EVT, "aer event"),
  188. LOCAL_EVENT_CAUSE(PM_MSI_INT_EVENTS, "pm/ltr/hotplug event"),
  189. LOCAL_EVENT_CAUSE(PM_MSI_INT_SYS_ERR, "system error"),
  190. };
  191. static struct event_map pcie_event_to_event[] = {
  192. PCIE_EVENT_TO_EVENT_MAP(L2_EXIT),
  193. PCIE_EVENT_TO_EVENT_MAP(HOTRST_EXIT),
  194. PCIE_EVENT_TO_EVENT_MAP(DLUP_EXIT),
  195. };
  196. static struct event_map sec_error_to_event[] = {
  197. SEC_ERROR_TO_EVENT_MAP(TX_RAM_SEC_ERR),
  198. SEC_ERROR_TO_EVENT_MAP(RX_RAM_SEC_ERR),
  199. SEC_ERROR_TO_EVENT_MAP(PCIE2AXI_RAM_SEC_ERR),
  200. SEC_ERROR_TO_EVENT_MAP(AXI2PCIE_RAM_SEC_ERR),
  201. };
  202. static struct event_map ded_error_to_event[] = {
  203. DED_ERROR_TO_EVENT_MAP(TX_RAM_DED_ERR),
  204. DED_ERROR_TO_EVENT_MAP(RX_RAM_DED_ERR),
  205. DED_ERROR_TO_EVENT_MAP(PCIE2AXI_RAM_DED_ERR),
  206. DED_ERROR_TO_EVENT_MAP(AXI2PCIE_RAM_DED_ERR),
  207. };
  208. static struct event_map local_status_to_event[] = {
  209. LOCAL_STATUS_TO_EVENT_MAP(DMA_END_ENGINE_0),
  210. LOCAL_STATUS_TO_EVENT_MAP(DMA_END_ENGINE_1),
  211. LOCAL_STATUS_TO_EVENT_MAP(DMA_ERROR_ENGINE_0),
  212. LOCAL_STATUS_TO_EVENT_MAP(DMA_ERROR_ENGINE_1),
  213. LOCAL_STATUS_TO_EVENT_MAP(A_ATR_EVT_POST_ERR),
  214. LOCAL_STATUS_TO_EVENT_MAP(A_ATR_EVT_FETCH_ERR),
  215. LOCAL_STATUS_TO_EVENT_MAP(A_ATR_EVT_DISCARD_ERR),
  216. LOCAL_STATUS_TO_EVENT_MAP(A_ATR_EVT_DOORBELL),
  217. LOCAL_STATUS_TO_EVENT_MAP(P_ATR_EVT_POST_ERR),
  218. LOCAL_STATUS_TO_EVENT_MAP(P_ATR_EVT_FETCH_ERR),
  219. LOCAL_STATUS_TO_EVENT_MAP(P_ATR_EVT_DISCARD_ERR),
  220. LOCAL_STATUS_TO_EVENT_MAP(P_ATR_EVT_DOORBELL),
  221. LOCAL_STATUS_TO_EVENT_MAP(PM_MSI_INT_INTX),
  222. LOCAL_STATUS_TO_EVENT_MAP(PM_MSI_INT_MSI),
  223. LOCAL_STATUS_TO_EVENT_MAP(PM_MSI_INT_AER_EVT),
  224. LOCAL_STATUS_TO_EVENT_MAP(PM_MSI_INT_EVENTS),
  225. LOCAL_STATUS_TO_EVENT_MAP(PM_MSI_INT_SYS_ERR),
  226. };
  227. static struct {
  228. u32 offset;
  229. u32 mask;
  230. u32 shift;
  231. u32 enb_mask;
  232. u32 mask_high;
  233. u32 mask_offset;
  234. } event_descs[] = {
  235. { PCIE_EVENT(L2_EXIT) },
  236. { PCIE_EVENT(HOTRST_EXIT) },
  237. { PCIE_EVENT(DLUP_EXIT) },
  238. { SEC_EVENT(TX_RAM_SEC_ERR) },
  239. { SEC_EVENT(RX_RAM_SEC_ERR) },
  240. { SEC_EVENT(PCIE2AXI_RAM_SEC_ERR) },
  241. { SEC_EVENT(AXI2PCIE_RAM_SEC_ERR) },
  242. { DED_EVENT(TX_RAM_DED_ERR) },
  243. { DED_EVENT(RX_RAM_DED_ERR) },
  244. { DED_EVENT(PCIE2AXI_RAM_DED_ERR) },
  245. { DED_EVENT(AXI2PCIE_RAM_DED_ERR) },
  246. { LOCAL_EVENT(DMA_END_ENGINE_0) },
  247. { LOCAL_EVENT(DMA_END_ENGINE_1) },
  248. { LOCAL_EVENT(DMA_ERROR_ENGINE_0) },
  249. { LOCAL_EVENT(DMA_ERROR_ENGINE_1) },
  250. { LOCAL_EVENT(A_ATR_EVT_POST_ERR) },
  251. { LOCAL_EVENT(A_ATR_EVT_FETCH_ERR) },
  252. { LOCAL_EVENT(A_ATR_EVT_DISCARD_ERR) },
  253. { LOCAL_EVENT(A_ATR_EVT_DOORBELL) },
  254. { LOCAL_EVENT(P_ATR_EVT_POST_ERR) },
  255. { LOCAL_EVENT(P_ATR_EVT_FETCH_ERR) },
  256. { LOCAL_EVENT(P_ATR_EVT_DISCARD_ERR) },
  257. { LOCAL_EVENT(P_ATR_EVT_DOORBELL) },
  258. { LOCAL_EVENT(PM_MSI_INT_INTX) },
  259. { LOCAL_EVENT(PM_MSI_INT_MSI) },
  260. { LOCAL_EVENT(PM_MSI_INT_AER_EVT) },
  261. { LOCAL_EVENT(PM_MSI_INT_EVENTS) },
  262. { LOCAL_EVENT(PM_MSI_INT_SYS_ERR) },
  263. };
  264. static char poss_clks[][5] = { "fic0", "fic1", "fic2", "fic3" };
  265. static struct mc_pcie *port;
  266. static void mc_pcie_enable_msi(struct mc_pcie *port, void __iomem *ecam)
  267. {
  268. struct plda_msi *msi = &port->plda.msi;
  269. u16 reg;
  270. u8 queue_size;
  271. /* Fixup MSI enable flag */
  272. reg = readw_relaxed(ecam + MC_MSI_CAP_CTRL_OFFSET + PCI_MSI_FLAGS);
  273. reg |= PCI_MSI_FLAGS_ENABLE;
  274. writew_relaxed(reg, ecam + MC_MSI_CAP_CTRL_OFFSET + PCI_MSI_FLAGS);
  275. /* Fixup PCI MSI queue flags */
  276. queue_size = FIELD_GET(PCI_MSI_FLAGS_QMASK, reg);
  277. reg |= FIELD_PREP(PCI_MSI_FLAGS_QSIZE, queue_size);
  278. writew_relaxed(reg, ecam + MC_MSI_CAP_CTRL_OFFSET + PCI_MSI_FLAGS);
  279. /* Fixup MSI addr fields */
  280. writel_relaxed(lower_32_bits(msi->vector_phy),
  281. ecam + MC_MSI_CAP_CTRL_OFFSET + PCI_MSI_ADDRESS_LO);
  282. writel_relaxed(upper_32_bits(msi->vector_phy),
  283. ecam + MC_MSI_CAP_CTRL_OFFSET + PCI_MSI_ADDRESS_HI);
  284. }
  285. static inline u32 reg_to_event(u32 reg, struct event_map field)
  286. {
  287. return (reg & field.reg_mask) ? BIT(field.event_bit) : 0;
  288. }
  289. static u32 pcie_events(struct mc_pcie *port)
  290. {
  291. u32 reg = readl_relaxed(port->ctrl_base_addr + PCIE_EVENT_INT);
  292. u32 val = 0;
  293. int i;
  294. for (i = 0; i < ARRAY_SIZE(pcie_event_to_event); i++)
  295. val |= reg_to_event(reg, pcie_event_to_event[i]);
  296. return val;
  297. }
  298. static u32 sec_errors(struct mc_pcie *port)
  299. {
  300. u32 reg = readl_relaxed(port->ctrl_base_addr + SEC_ERROR_INT);
  301. u32 val = 0;
  302. int i;
  303. for (i = 0; i < ARRAY_SIZE(sec_error_to_event); i++)
  304. val |= reg_to_event(reg, sec_error_to_event[i]);
  305. return val;
  306. }
  307. static u32 ded_errors(struct mc_pcie *port)
  308. {
  309. u32 reg = readl_relaxed(port->ctrl_base_addr + DED_ERROR_INT);
  310. u32 val = 0;
  311. int i;
  312. for (i = 0; i < ARRAY_SIZE(ded_error_to_event); i++)
  313. val |= reg_to_event(reg, ded_error_to_event[i]);
  314. return val;
  315. }
  316. static u32 local_events(struct mc_pcie *port)
  317. {
  318. u32 reg = readl_relaxed(port->bridge_base_addr + ISTATUS_LOCAL);
  319. u32 val = 0;
  320. int i;
  321. for (i = 0; i < ARRAY_SIZE(local_status_to_event); i++)
  322. val |= reg_to_event(reg, local_status_to_event[i]);
  323. return val;
  324. }
  325. static u32 mc_get_events(struct plda_pcie_rp *port)
  326. {
  327. struct mc_pcie *mc_port = container_of(port, struct mc_pcie, plda);
  328. u32 events = 0;
  329. events |= pcie_events(mc_port);
  330. events |= sec_errors(mc_port);
  331. events |= ded_errors(mc_port);
  332. events |= local_events(mc_port);
  333. return events;
  334. }
  335. static irqreturn_t mc_event_handler(int irq, void *dev_id)
  336. {
  337. struct plda_pcie_rp *port = dev_id;
  338. struct device *dev = port->dev;
  339. struct irq_data *data;
  340. data = irq_domain_get_irq_data(port->event_domain, irq);
  341. if (event_cause[data->hwirq].str)
  342. dev_err_ratelimited(dev, "%s\n", event_cause[data->hwirq].str);
  343. else
  344. dev_err_ratelimited(dev, "bad event IRQ %ld\n", data->hwirq);
  345. return IRQ_HANDLED;
  346. }
  347. static void mc_ack_event_irq(struct irq_data *data)
  348. {
  349. struct plda_pcie_rp *port = irq_data_get_irq_chip_data(data);
  350. struct mc_pcie *mc_port = container_of(port, struct mc_pcie, plda);
  351. u32 event = data->hwirq;
  352. void __iomem *addr;
  353. u32 mask;
  354. if (event_descs[event].offset == ISTATUS_LOCAL)
  355. addr = mc_port->bridge_base_addr;
  356. else
  357. addr = mc_port->ctrl_base_addr;
  358. addr += event_descs[event].offset;
  359. mask = event_descs[event].mask;
  360. mask |= event_descs[event].enb_mask;
  361. writel_relaxed(mask, addr);
  362. }
  363. static void mc_mask_event_irq(struct irq_data *data)
  364. {
  365. struct plda_pcie_rp *port = irq_data_get_irq_chip_data(data);
  366. struct mc_pcie *mc_port = container_of(port, struct mc_pcie, plda);
  367. u32 event = data->hwirq;
  368. void __iomem *addr;
  369. u32 mask;
  370. u32 val;
  371. if (event_descs[event].offset == ISTATUS_LOCAL)
  372. addr = mc_port->bridge_base_addr;
  373. else
  374. addr = mc_port->ctrl_base_addr;
  375. addr += event_descs[event].mask_offset;
  376. mask = event_descs[event].mask;
  377. if (event_descs[event].enb_mask) {
  378. mask <<= PCIE_EVENT_INT_ENB_SHIFT;
  379. mask &= PCIE_EVENT_INT_ENB_MASK;
  380. }
  381. if (!event_descs[event].mask_high)
  382. mask = ~mask;
  383. raw_spin_lock(&port->lock);
  384. val = readl_relaxed(addr);
  385. if (event_descs[event].mask_high)
  386. val |= mask;
  387. else
  388. val &= mask;
  389. writel_relaxed(val, addr);
  390. raw_spin_unlock(&port->lock);
  391. }
  392. static void mc_unmask_event_irq(struct irq_data *data)
  393. {
  394. struct plda_pcie_rp *port = irq_data_get_irq_chip_data(data);
  395. struct mc_pcie *mc_port = container_of(port, struct mc_pcie, plda);
  396. u32 event = data->hwirq;
  397. void __iomem *addr;
  398. u32 mask;
  399. u32 val;
  400. if (event_descs[event].offset == ISTATUS_LOCAL)
  401. addr = mc_port->bridge_base_addr;
  402. else
  403. addr = mc_port->ctrl_base_addr;
  404. addr += event_descs[event].mask_offset;
  405. mask = event_descs[event].mask;
  406. if (event_descs[event].enb_mask)
  407. mask <<= PCIE_EVENT_INT_ENB_SHIFT;
  408. if (event_descs[event].mask_high)
  409. mask = ~mask;
  410. if (event_descs[event].enb_mask)
  411. mask &= PCIE_EVENT_INT_ENB_MASK;
  412. raw_spin_lock(&port->lock);
  413. val = readl_relaxed(addr);
  414. if (event_descs[event].mask_high)
  415. val &= mask;
  416. else
  417. val |= mask;
  418. writel_relaxed(val, addr);
  419. raw_spin_unlock(&port->lock);
  420. }
  421. static struct irq_chip mc_event_irq_chip = {
  422. .name = "Microchip PCIe EVENT",
  423. .irq_ack = mc_ack_event_irq,
  424. .irq_mask = mc_mask_event_irq,
  425. .irq_unmask = mc_unmask_event_irq,
  426. };
  427. static inline void mc_pcie_deinit_clk(void *data)
  428. {
  429. struct clk *clk = data;
  430. clk_disable_unprepare(clk);
  431. }
  432. static inline struct clk *mc_pcie_init_clk(struct device *dev, const char *id)
  433. {
  434. struct clk *clk;
  435. int ret;
  436. clk = devm_clk_get_optional(dev, id);
  437. if (IS_ERR(clk))
  438. return clk;
  439. if (!clk)
  440. return clk;
  441. ret = clk_prepare_enable(clk);
  442. if (ret)
  443. return ERR_PTR(ret);
  444. devm_add_action_or_reset(dev, mc_pcie_deinit_clk, clk);
  445. return clk;
  446. }
  447. static int mc_pcie_init_clks(struct device *dev)
  448. {
  449. int i;
  450. struct clk *fic;
  451. /*
  452. * PCIe may be clocked via Fabric Interface using between 1 and 4
  453. * clocks. Scan DT for clocks and enable them if present
  454. */
  455. for (i = 0; i < ARRAY_SIZE(poss_clks); i++) {
  456. fic = mc_pcie_init_clk(dev, poss_clks[i]);
  457. if (IS_ERR(fic))
  458. return PTR_ERR(fic);
  459. }
  460. return 0;
  461. }
  462. static int mc_request_event_irq(struct plda_pcie_rp *plda, int event_irq,
  463. int event)
  464. {
  465. return devm_request_irq(plda->dev, event_irq, mc_event_handler,
  466. 0, event_cause[event].sym, plda);
  467. }
  468. static const struct plda_event_ops mc_event_ops = {
  469. .get_events = mc_get_events,
  470. };
  471. static const struct plda_event mc_event = {
  472. .request_event_irq = mc_request_event_irq,
  473. .intx_event = EVENT_LOCAL_PM_MSI_INT_INTX,
  474. .msi_event = EVENT_LOCAL_PM_MSI_INT_MSI,
  475. };
  476. static inline void mc_clear_secs(struct mc_pcie *port)
  477. {
  478. writel_relaxed(SEC_ERROR_INT_ALL_RAM_SEC_ERR_INT,
  479. port->ctrl_base_addr + SEC_ERROR_INT);
  480. writel_relaxed(0, port->ctrl_base_addr + SEC_ERROR_EVENT_CNT);
  481. }
  482. static inline void mc_clear_deds(struct mc_pcie *port)
  483. {
  484. writel_relaxed(DED_ERROR_INT_ALL_RAM_DED_ERR_INT,
  485. port->ctrl_base_addr + DED_ERROR_INT);
  486. writel_relaxed(0, port->ctrl_base_addr + DED_ERROR_EVENT_CNT);
  487. }
  488. static void mc_disable_interrupts(struct mc_pcie *port)
  489. {
  490. u32 val;
  491. /* Ensure ECC bypass is enabled */
  492. val = ECC_CONTROL_TX_RAM_ECC_BYPASS |
  493. ECC_CONTROL_RX_RAM_ECC_BYPASS |
  494. ECC_CONTROL_PCIE2AXI_RAM_ECC_BYPASS |
  495. ECC_CONTROL_AXI2PCIE_RAM_ECC_BYPASS;
  496. writel_relaxed(val, port->ctrl_base_addr + ECC_CONTROL);
  497. /* Disable SEC errors and clear any outstanding */
  498. writel_relaxed(SEC_ERROR_INT_ALL_RAM_SEC_ERR_INT,
  499. port->ctrl_base_addr + SEC_ERROR_INT_MASK);
  500. mc_clear_secs(port);
  501. /* Disable DED errors and clear any outstanding */
  502. writel_relaxed(DED_ERROR_INT_ALL_RAM_DED_ERR_INT,
  503. port->ctrl_base_addr + DED_ERROR_INT_MASK);
  504. mc_clear_deds(port);
  505. /* Disable local interrupts and clear any outstanding */
  506. writel_relaxed(0, port->bridge_base_addr + IMASK_LOCAL);
  507. writel_relaxed(GENMASK(31, 0), port->bridge_base_addr + ISTATUS_LOCAL);
  508. writel_relaxed(GENMASK(31, 0), port->bridge_base_addr + ISTATUS_MSI);
  509. /* Disable PCIe events and clear any outstanding */
  510. val = PCIE_EVENT_INT_L2_EXIT_INT |
  511. PCIE_EVENT_INT_HOTRST_EXIT_INT |
  512. PCIE_EVENT_INT_DLUP_EXIT_INT |
  513. PCIE_EVENT_INT_L2_EXIT_INT_MASK |
  514. PCIE_EVENT_INT_HOTRST_EXIT_INT_MASK |
  515. PCIE_EVENT_INT_DLUP_EXIT_INT_MASK;
  516. writel_relaxed(val, port->ctrl_base_addr + PCIE_EVENT_INT);
  517. /* Disable host interrupts and clear any outstanding */
  518. writel_relaxed(0, port->bridge_base_addr + IMASK_HOST);
  519. writel_relaxed(GENMASK(31, 0), port->bridge_base_addr + ISTATUS_HOST);
  520. }
  521. static void mc_pcie_setup_inbound_atr(struct mc_pcie *port, int window_index,
  522. u64 axi_addr, u64 pcie_addr, u64 size)
  523. {
  524. u32 table_offset = window_index * ATR_ENTRY_SIZE;
  525. void __iomem *table_addr = port->bridge_base_addr + table_offset;
  526. u32 atr_sz;
  527. u32 val;
  528. atr_sz = ilog2(size) - 1;
  529. val = ALIGN_DOWN(lower_32_bits(pcie_addr), SZ_4K);
  530. val |= FIELD_PREP(ATR_SIZE_MASK, atr_sz);
  531. val |= ATR_IMPL_ENABLE;
  532. writel(val, table_addr + ATR0_PCIE_WIN0_SRCADDR_PARAM);
  533. writel(upper_32_bits(pcie_addr), table_addr + ATR0_PCIE_WIN0_SRC_ADDR);
  534. writel(lower_32_bits(axi_addr), table_addr + ATR0_PCIE_WIN0_TRSL_ADDR_LSB);
  535. writel(upper_32_bits(axi_addr), table_addr + ATR0_PCIE_WIN0_TRSL_ADDR_UDW);
  536. writel(TRSL_ID_AXI4_MASTER_0, table_addr + ATR0_PCIE_WIN0_TRSL_PARAM);
  537. }
  538. static int mc_pcie_setup_inbound_ranges(struct platform_device *pdev,
  539. struct mc_pcie *port)
  540. {
  541. struct device *dev = &pdev->dev;
  542. struct device_node *dn = dev->of_node;
  543. struct of_range_parser parser;
  544. struct of_range range;
  545. int atr_index = 0;
  546. /*
  547. * MPFS PCIe Root Port is 32-bit only, behind a Fabric Interface
  548. * Controller FPGA logic block which contains the AXI-S interface.
  549. *
  550. * From the point of view of the PCIe Root Port, there are only two
  551. * supported Root Port configurations:
  552. *
  553. * Configuration 1: for use with fully coherent designs; supports a
  554. * window from 0x0 (CPU space) to specified PCIe space.
  555. *
  556. * Configuration 2: for use with non-coherent designs; supports two
  557. * 1 GB windows to CPU space; one mapping CPU space 0 to PCIe space
  558. * 0x80000000 and a second mapping CPU space 0x40000000 to PCIe
  559. * space 0xc0000000. This cfg needs two windows because of how the
  560. * MSI space is allocated in the AXI-S range on MPFS.
  561. *
  562. * The FIC interface outside the PCIe block *must* complete the
  563. * inbound address translation as per MCHP MPFS FPGA design
  564. * guidelines.
  565. */
  566. if (device_property_read_bool(dev, "dma-noncoherent")) {
  567. /*
  568. * Always need same two tables in this case. Need two tables
  569. * due to hardware interactions between address and size.
  570. */
  571. mc_pcie_setup_inbound_atr(port, 0, 0,
  572. MPFS_NC_BOUNCE_ADDR, SZ_1G);
  573. mc_pcie_setup_inbound_atr(port, 1, SZ_1G,
  574. MPFS_NC_BOUNCE_ADDR + SZ_1G, SZ_1G);
  575. } else {
  576. /* Find any DMA ranges */
  577. if (of_pci_dma_range_parser_init(&parser, dn)) {
  578. /* No DMA range property - setup default */
  579. mc_pcie_setup_inbound_atr(port, 0, 0, 0, SZ_4G);
  580. return 0;
  581. }
  582. for_each_of_range(&parser, &range) {
  583. if (atr_index >= MC_MAX_NUM_INBOUND_WINDOWS) {
  584. dev_err(dev, "too many inbound ranges; %d available tables\n",
  585. MC_MAX_NUM_INBOUND_WINDOWS);
  586. return -EINVAL;
  587. }
  588. mc_pcie_setup_inbound_atr(port, atr_index, 0,
  589. range.pci_addr, range.size);
  590. atr_index++;
  591. }
  592. }
  593. return 0;
  594. }
  595. static int mc_platform_init(struct pci_config_window *cfg)
  596. {
  597. struct device *dev = cfg->parent;
  598. struct platform_device *pdev = to_platform_device(dev);
  599. struct pci_host_bridge *bridge = platform_get_drvdata(pdev);
  600. int ret;
  601. /* Configure address translation table 0 for PCIe config space */
  602. plda_pcie_setup_window(port->bridge_base_addr, 0, cfg->res.start,
  603. cfg->res.start,
  604. resource_size(&cfg->res));
  605. /* Need some fixups in config space */
  606. mc_pcie_enable_msi(port, cfg->win);
  607. /* Configure non-config space outbound ranges */
  608. ret = plda_pcie_setup_iomems(bridge, &port->plda);
  609. if (ret)
  610. return ret;
  611. ret = mc_pcie_setup_inbound_ranges(pdev, port);
  612. if (ret)
  613. return ret;
  614. port->plda.event_ops = &mc_event_ops;
  615. port->plda.event_irq_chip = &mc_event_irq_chip;
  616. port->plda.events_bitmap = GENMASK(NUM_EVENTS - 1, 0);
  617. /* Address translation is up; safe to enable interrupts */
  618. ret = plda_init_interrupts(pdev, &port->plda, &mc_event);
  619. if (ret)
  620. return ret;
  621. return 0;
  622. }
  623. static int mc_host_probe(struct platform_device *pdev)
  624. {
  625. struct device *dev = &pdev->dev;
  626. void __iomem *apb_base_addr;
  627. struct plda_pcie_rp *plda;
  628. int ret;
  629. u32 val;
  630. port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
  631. if (!port)
  632. return -ENOMEM;
  633. plda = &port->plda;
  634. plda->dev = dev;
  635. port->bridge_base_addr = devm_platform_ioremap_resource_byname(pdev,
  636. "bridge");
  637. port->ctrl_base_addr = devm_platform_ioremap_resource_byname(pdev,
  638. "ctrl");
  639. if (!IS_ERR(port->bridge_base_addr) && !IS_ERR(port->ctrl_base_addr))
  640. goto addrs_set;
  641. /*
  642. * The original, incorrect, binding that lumped the control and
  643. * bridge addresses together still needs to be handled by the driver.
  644. */
  645. apb_base_addr = devm_platform_ioremap_resource_byname(pdev, "apb");
  646. if (IS_ERR(apb_base_addr))
  647. return dev_err_probe(dev, PTR_ERR(apb_base_addr),
  648. "both legacy apb register and ctrl/bridge regions missing");
  649. port->bridge_base_addr = apb_base_addr + MC_PCIE1_BRIDGE_ADDR;
  650. port->ctrl_base_addr = apb_base_addr + MC_PCIE1_CTRL_ADDR;
  651. addrs_set:
  652. mc_disable_interrupts(port);
  653. plda->bridge_addr = port->bridge_base_addr;
  654. plda->num_events = NUM_EVENTS;
  655. /* Allow enabling MSI by disabling MSI-X */
  656. val = readl(port->bridge_base_addr + PCIE_PCI_IRQ_DW0);
  657. val &= ~MSIX_CAP_MASK;
  658. writel(val, port->bridge_base_addr + PCIE_PCI_IRQ_DW0);
  659. /* Pick num vectors from bitfile programmed onto FPGA fabric */
  660. val = readl(port->bridge_base_addr + PCIE_PCI_IRQ_DW0);
  661. val &= NUM_MSI_MSGS_MASK;
  662. val >>= NUM_MSI_MSGS_SHIFT;
  663. plda->msi.num_vectors = 1 << val;
  664. /* Pick vector address from design */
  665. plda->msi.vector_phy = readl_relaxed(port->bridge_base_addr + IMSI_ADDR);
  666. ret = mc_pcie_init_clks(dev);
  667. if (ret) {
  668. dev_err(dev, "failed to get clock resources, error %d\n", ret);
  669. return -ENODEV;
  670. }
  671. return pci_host_common_probe(pdev);
  672. }
  673. static const struct pci_ecam_ops mc_ecam_ops = {
  674. .init = mc_platform_init,
  675. .pci_ops = {
  676. .map_bus = pci_ecam_map_bus,
  677. .read = pci_generic_config_read,
  678. .write = pci_generic_config_write,
  679. }
  680. };
  681. static const struct of_device_id mc_pcie_of_match[] = {
  682. {
  683. .compatible = "microchip,pcie-host-1.0",
  684. .data = &mc_ecam_ops,
  685. },
  686. {},
  687. };
  688. MODULE_DEVICE_TABLE(of, mc_pcie_of_match);
  689. static struct platform_driver mc_pcie_driver = {
  690. .probe = mc_host_probe,
  691. .driver = {
  692. .name = "microchip-pcie",
  693. .of_match_table = mc_pcie_of_match,
  694. .suppress_bind_attrs = true,
  695. },
  696. };
  697. builtin_platform_driver(mc_pcie_driver);
  698. MODULE_LICENSE("GPL");
  699. MODULE_DESCRIPTION("Microchip PCIe host controller driver");
  700. MODULE_AUTHOR("Daire McNamara <daire.mcnamara@microchip.com>");