pcie-microchip-host.c 25 KB

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