pci-keystone.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for Texas Instruments Keystone SoCs
  4. *
  5. * Copyright (C) 2013-2014 Texas Instruments., Ltd.
  6. * https://www.ti.com
  7. *
  8. * Author: Murali Karicheri <m-karicheri2@ti.com>
  9. * Implementation based on pci-exynos.c and pcie-designware.c
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irqchip/chained_irq.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/module.h>
  20. #include <linux/msi.h>
  21. #include <linux/of.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of_pci.h>
  24. #include <linux/phy/phy.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/regmap.h>
  27. #include <linux/resource.h>
  28. #include <linux/signal.h>
  29. #include "../../pci.h"
  30. #include "pcie-designware.h"
  31. #define PCIE_VENDORID_MASK 0xffff
  32. #define PCIE_DEVICEID_SHIFT 16
  33. /* Application registers */
  34. #define PID 0x000
  35. #define RTL GENMASK(15, 11)
  36. #define RTL_SHIFT 11
  37. #define AM6_PCI_PG1_RTL_VER 0x15
  38. #define CMD_STATUS 0x004
  39. #define LTSSM_EN_VAL BIT(0)
  40. #define OB_XLAT_EN_VAL BIT(1)
  41. #define DBI_CS2 BIT(5)
  42. #define CFG_SETUP 0x008
  43. #define CFG_BUS(x) (((x) & 0xff) << 16)
  44. #define CFG_DEVICE(x) (((x) & 0x1f) << 8)
  45. #define CFG_FUNC(x) ((x) & 0x7)
  46. #define CFG_TYPE1 BIT(24)
  47. #define OB_SIZE 0x030
  48. #define OB_OFFSET_INDEX(n) (0x200 + (8 * (n)))
  49. #define OB_OFFSET_HI(n) (0x204 + (8 * (n)))
  50. #define OB_ENABLEN BIT(0)
  51. #define OB_WIN_SIZE 8 /* 8MB */
  52. #define PCIE_LEGACY_IRQ_ENABLE_SET(n) (0x188 + (0x10 * ((n) - 1)))
  53. #define PCIE_LEGACY_IRQ_ENABLE_CLR(n) (0x18c + (0x10 * ((n) - 1)))
  54. #define PCIE_EP_IRQ_SET 0x64
  55. #define PCIE_EP_IRQ_CLR 0x68
  56. #define INT_ENABLE BIT(0)
  57. /* IRQ register defines */
  58. #define IRQ_EOI 0x050
  59. #define MSI_IRQ 0x054
  60. #define MSI_IRQ_STATUS(n) (0x104 + ((n) << 4))
  61. #define MSI_IRQ_ENABLE_SET(n) (0x108 + ((n) << 4))
  62. #define MSI_IRQ_ENABLE_CLR(n) (0x10c + ((n) << 4))
  63. #define MSI_IRQ_OFFSET 4
  64. #define IRQ_STATUS(n) (0x184 + ((n) << 4))
  65. #define IRQ_ENABLE_SET(n) (0x188 + ((n) << 4))
  66. #define INTx_EN BIT(0)
  67. #define ERR_IRQ_STATUS 0x1c4
  68. #define ERR_IRQ_ENABLE_SET 0x1c8
  69. #define ERR_AER BIT(5) /* ECRC error */
  70. #define AM6_ERR_AER BIT(4) /* AM6 ECRC error */
  71. #define ERR_AXI BIT(4) /* AXI tag lookup fatal error */
  72. #define ERR_CORR BIT(3) /* Correctable error */
  73. #define ERR_NONFATAL BIT(2) /* Non-fatal error */
  74. #define ERR_FATAL BIT(1) /* Fatal error */
  75. #define ERR_SYS BIT(0) /* System error */
  76. #define ERR_IRQ_ALL (ERR_AER | ERR_AXI | ERR_CORR | \
  77. ERR_NONFATAL | ERR_FATAL | ERR_SYS)
  78. /* PCIE controller device IDs */
  79. #define PCIE_RC_K2HK 0xb008
  80. #define PCIE_RC_K2E 0xb009
  81. #define PCIE_RC_K2L 0xb00a
  82. #define PCIE_RC_K2G 0xb00b
  83. #define KS_PCIE_DEV_TYPE_MASK (0x3 << 1)
  84. #define KS_PCIE_DEV_TYPE(mode) ((mode) << 1)
  85. #define EP 0x0
  86. #define LEG_EP 0x1
  87. #define RC 0x2
  88. #define KS_PCIE_SYSCLOCKOUTEN BIT(0)
  89. #define AM654_PCIE_DEV_TYPE_MASK 0x3
  90. #define AM654_WIN_SIZE SZ_64K
  91. #define APP_ADDR_SPACE_0 (16 * SZ_1K)
  92. #define to_keystone_pcie(x) dev_get_drvdata((x)->dev)
  93. #define PCI_DEVICE_ID_TI_AM654X 0xb00c
  94. struct ks_pcie_of_data {
  95. enum dw_pcie_device_mode mode;
  96. const struct dw_pcie_host_ops *host_ops;
  97. const struct dw_pcie_ep_ops *ep_ops;
  98. u32 version;
  99. };
  100. struct keystone_pcie {
  101. struct dw_pcie *pci;
  102. /* PCI Device ID */
  103. u32 device_id;
  104. int intx_host_irqs[PCI_NUM_INTX];
  105. int msi_host_irq;
  106. int num_lanes;
  107. u32 num_viewport;
  108. struct phy **phy;
  109. struct device_link **link;
  110. struct device_node *msi_intc_np;
  111. struct irq_domain *intx_irq_domain;
  112. struct device_node *np;
  113. /* Application register space */
  114. void __iomem *va_app_base; /* DT 1st resource */
  115. struct resource app;
  116. bool is_am6;
  117. };
  118. static u32 ks_pcie_app_readl(struct keystone_pcie *ks_pcie, u32 offset)
  119. {
  120. return readl(ks_pcie->va_app_base + offset);
  121. }
  122. static void ks_pcie_app_writel(struct keystone_pcie *ks_pcie, u32 offset,
  123. u32 val)
  124. {
  125. writel(val, ks_pcie->va_app_base + offset);
  126. }
  127. static void ks_pcie_msi_irq_ack(struct irq_data *data)
  128. {
  129. struct dw_pcie_rp *pp = irq_data_get_irq_chip_data(data);
  130. struct keystone_pcie *ks_pcie;
  131. u32 irq = data->hwirq;
  132. struct dw_pcie *pci;
  133. u32 reg_offset;
  134. u32 bit_pos;
  135. pci = to_dw_pcie_from_pp(pp);
  136. ks_pcie = to_keystone_pcie(pci);
  137. reg_offset = irq % 8;
  138. bit_pos = irq >> 3;
  139. ks_pcie_app_writel(ks_pcie, MSI_IRQ_STATUS(reg_offset),
  140. BIT(bit_pos));
  141. ks_pcie_app_writel(ks_pcie, IRQ_EOI, reg_offset + MSI_IRQ_OFFSET);
  142. }
  143. static void ks_pcie_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  144. {
  145. struct dw_pcie_rp *pp = irq_data_get_irq_chip_data(data);
  146. struct keystone_pcie *ks_pcie;
  147. struct dw_pcie *pci;
  148. u64 msi_target;
  149. pci = to_dw_pcie_from_pp(pp);
  150. ks_pcie = to_keystone_pcie(pci);
  151. msi_target = ks_pcie->app.start + MSI_IRQ;
  152. msg->address_lo = lower_32_bits(msi_target);
  153. msg->address_hi = upper_32_bits(msi_target);
  154. msg->data = data->hwirq;
  155. dev_dbg(pci->dev, "msi#%d address_hi %#x address_lo %#x\n",
  156. (int)data->hwirq, msg->address_hi, msg->address_lo);
  157. }
  158. static void ks_pcie_msi_mask(struct irq_data *data)
  159. {
  160. struct dw_pcie_rp *pp = irq_data_get_irq_chip_data(data);
  161. struct keystone_pcie *ks_pcie;
  162. u32 irq = data->hwirq;
  163. struct dw_pcie *pci;
  164. unsigned long flags;
  165. u32 reg_offset;
  166. u32 bit_pos;
  167. raw_spin_lock_irqsave(&pp->lock, flags);
  168. pci = to_dw_pcie_from_pp(pp);
  169. ks_pcie = to_keystone_pcie(pci);
  170. reg_offset = irq % 8;
  171. bit_pos = irq >> 3;
  172. ks_pcie_app_writel(ks_pcie, MSI_IRQ_ENABLE_CLR(reg_offset),
  173. BIT(bit_pos));
  174. raw_spin_unlock_irqrestore(&pp->lock, flags);
  175. }
  176. static void ks_pcie_msi_unmask(struct irq_data *data)
  177. {
  178. struct dw_pcie_rp *pp = irq_data_get_irq_chip_data(data);
  179. struct keystone_pcie *ks_pcie;
  180. u32 irq = data->hwirq;
  181. struct dw_pcie *pci;
  182. unsigned long flags;
  183. u32 reg_offset;
  184. u32 bit_pos;
  185. raw_spin_lock_irqsave(&pp->lock, flags);
  186. pci = to_dw_pcie_from_pp(pp);
  187. ks_pcie = to_keystone_pcie(pci);
  188. reg_offset = irq % 8;
  189. bit_pos = irq >> 3;
  190. ks_pcie_app_writel(ks_pcie, MSI_IRQ_ENABLE_SET(reg_offset),
  191. BIT(bit_pos));
  192. raw_spin_unlock_irqrestore(&pp->lock, flags);
  193. }
  194. static struct irq_chip ks_pcie_msi_irq_chip = {
  195. .name = "KEYSTONE-PCI-MSI",
  196. .irq_ack = ks_pcie_msi_irq_ack,
  197. .irq_compose_msi_msg = ks_pcie_compose_msi_msg,
  198. .irq_mask = ks_pcie_msi_mask,
  199. .irq_unmask = ks_pcie_msi_unmask,
  200. };
  201. /**
  202. * ks_pcie_set_dbi_mode() - Set DBI mode to access overlaid BAR mask registers
  203. * @ks_pcie: A pointer to the keystone_pcie structure which holds the KeyStone
  204. * PCIe host controller driver information.
  205. *
  206. * Since modification of dbi_cs2 involves different clock domain, read the
  207. * status back to ensure the transition is complete.
  208. */
  209. static void ks_pcie_set_dbi_mode(struct keystone_pcie *ks_pcie)
  210. {
  211. u32 val;
  212. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  213. val |= DBI_CS2;
  214. ks_pcie_app_writel(ks_pcie, CMD_STATUS, val);
  215. do {
  216. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  217. } while (!(val & DBI_CS2));
  218. }
  219. /**
  220. * ks_pcie_clear_dbi_mode() - Disable DBI mode
  221. * @ks_pcie: A pointer to the keystone_pcie structure which holds the KeyStone
  222. * PCIe host controller driver information.
  223. *
  224. * Since modification of dbi_cs2 involves different clock domain, read the
  225. * status back to ensure the transition is complete.
  226. */
  227. static void ks_pcie_clear_dbi_mode(struct keystone_pcie *ks_pcie)
  228. {
  229. u32 val;
  230. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  231. val &= ~DBI_CS2;
  232. ks_pcie_app_writel(ks_pcie, CMD_STATUS, val);
  233. do {
  234. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  235. } while (val & DBI_CS2);
  236. }
  237. static int ks_pcie_msi_host_init(struct dw_pcie_rp *pp)
  238. {
  239. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  240. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  241. /* Configure and set up BAR0 */
  242. ks_pcie_set_dbi_mode(ks_pcie);
  243. /* Enable BAR0 */
  244. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 1);
  245. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, SZ_4K - 1);
  246. ks_pcie_clear_dbi_mode(ks_pcie);
  247. /*
  248. * For BAR0, just setting bus address for inbound writes (MSI) should
  249. * be sufficient. Use physical address to avoid any conflicts.
  250. */
  251. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, ks_pcie->app.start);
  252. pp->msi_irq_chip = &ks_pcie_msi_irq_chip;
  253. return dw_pcie_allocate_domains(pp);
  254. }
  255. static void ks_pcie_handle_intx_irq(struct keystone_pcie *ks_pcie,
  256. int offset)
  257. {
  258. struct dw_pcie *pci = ks_pcie->pci;
  259. struct device *dev = pci->dev;
  260. u32 pending;
  261. pending = ks_pcie_app_readl(ks_pcie, IRQ_STATUS(offset));
  262. if (BIT(0) & pending) {
  263. dev_dbg(dev, ": irq: irq_offset %d", offset);
  264. generic_handle_domain_irq(ks_pcie->intx_irq_domain, offset);
  265. }
  266. /* EOI the INTx interrupt */
  267. ks_pcie_app_writel(ks_pcie, IRQ_EOI, offset);
  268. }
  269. static void ks_pcie_enable_error_irq(struct keystone_pcie *ks_pcie)
  270. {
  271. ks_pcie_app_writel(ks_pcie, ERR_IRQ_ENABLE_SET, ERR_IRQ_ALL);
  272. }
  273. static irqreturn_t ks_pcie_handle_error_irq(struct keystone_pcie *ks_pcie)
  274. {
  275. u32 reg;
  276. struct device *dev = ks_pcie->pci->dev;
  277. reg = ks_pcie_app_readl(ks_pcie, ERR_IRQ_STATUS);
  278. if (!reg)
  279. return IRQ_NONE;
  280. if (reg & ERR_SYS)
  281. dev_err(dev, "System Error\n");
  282. if (reg & ERR_FATAL)
  283. dev_err(dev, "Fatal Error\n");
  284. if (reg & ERR_NONFATAL)
  285. dev_dbg(dev, "Non Fatal Error\n");
  286. if (reg & ERR_CORR)
  287. dev_dbg(dev, "Correctable Error\n");
  288. if (!ks_pcie->is_am6 && (reg & ERR_AXI))
  289. dev_err(dev, "AXI tag lookup fatal Error\n");
  290. if (reg & ERR_AER || (ks_pcie->is_am6 && (reg & AM6_ERR_AER)))
  291. dev_err(dev, "ECRC Error\n");
  292. ks_pcie_app_writel(ks_pcie, ERR_IRQ_STATUS, reg);
  293. return IRQ_HANDLED;
  294. }
  295. static void ks_pcie_ack_intx_irq(struct irq_data *d)
  296. {
  297. }
  298. static void ks_pcie_mask_intx_irq(struct irq_data *d)
  299. {
  300. }
  301. static void ks_pcie_unmask_intx_irq(struct irq_data *d)
  302. {
  303. }
  304. static struct irq_chip ks_pcie_intx_irq_chip = {
  305. .name = "Keystone-PCI-INTX-IRQ",
  306. .irq_ack = ks_pcie_ack_intx_irq,
  307. .irq_mask = ks_pcie_mask_intx_irq,
  308. .irq_unmask = ks_pcie_unmask_intx_irq,
  309. };
  310. static int ks_pcie_init_intx_irq_map(struct irq_domain *d,
  311. unsigned int irq, irq_hw_number_t hw_irq)
  312. {
  313. irq_set_chip_and_handler(irq, &ks_pcie_intx_irq_chip,
  314. handle_level_irq);
  315. irq_set_chip_data(irq, d->host_data);
  316. return 0;
  317. }
  318. static const struct irq_domain_ops ks_pcie_intx_irq_domain_ops = {
  319. .map = ks_pcie_init_intx_irq_map,
  320. .xlate = irq_domain_xlate_onetwocell,
  321. };
  322. static int ks_pcie_setup_rc_app_regs(struct keystone_pcie *ks_pcie)
  323. {
  324. u32 val;
  325. u32 num_viewport = ks_pcie->num_viewport;
  326. struct dw_pcie *pci = ks_pcie->pci;
  327. struct dw_pcie_rp *pp = &pci->pp;
  328. struct resource_entry *entry;
  329. struct resource *mem;
  330. u64 start, end;
  331. int i;
  332. entry = resource_list_first_type(&pp->bridge->windows, IORESOURCE_MEM);
  333. if (!entry)
  334. return -ENODEV;
  335. mem = entry->res;
  336. start = mem->start;
  337. end = mem->end;
  338. /* Disable BARs for inbound access */
  339. ks_pcie_set_dbi_mode(ks_pcie);
  340. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0);
  341. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0);
  342. ks_pcie_clear_dbi_mode(ks_pcie);
  343. if (ks_pcie->is_am6)
  344. return 0;
  345. val = ilog2(OB_WIN_SIZE);
  346. ks_pcie_app_writel(ks_pcie, OB_SIZE, val);
  347. /* Using Direct 1:1 mapping of RC <-> PCI memory space */
  348. for (i = 0; i < num_viewport && (start < end); i++) {
  349. ks_pcie_app_writel(ks_pcie, OB_OFFSET_INDEX(i),
  350. lower_32_bits(start) | OB_ENABLEN);
  351. ks_pcie_app_writel(ks_pcie, OB_OFFSET_HI(i),
  352. upper_32_bits(start));
  353. start += OB_WIN_SIZE * SZ_1M;
  354. }
  355. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  356. val |= OB_XLAT_EN_VAL;
  357. ks_pcie_app_writel(ks_pcie, CMD_STATUS, val);
  358. return 0;
  359. }
  360. static void __iomem *ks_pcie_other_map_bus(struct pci_bus *bus,
  361. unsigned int devfn, int where)
  362. {
  363. struct dw_pcie_rp *pp = bus->sysdata;
  364. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  365. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  366. u32 reg;
  367. /*
  368. * Checking whether the link is up here is a last line of defense
  369. * against platforms that forward errors on the system bus as
  370. * SError upon PCI configuration transactions issued when the link
  371. * is down. This check is racy by definition and does not stop
  372. * the system from triggering an SError if the link goes down
  373. * after this check is performed.
  374. */
  375. if (!dw_pcie_link_up(pci))
  376. return NULL;
  377. reg = CFG_BUS(bus->number) | CFG_DEVICE(PCI_SLOT(devfn)) |
  378. CFG_FUNC(PCI_FUNC(devfn));
  379. if (!pci_is_root_bus(bus->parent))
  380. reg |= CFG_TYPE1;
  381. ks_pcie_app_writel(ks_pcie, CFG_SETUP, reg);
  382. return pp->va_cfg0_base + where;
  383. }
  384. static struct pci_ops ks_child_pcie_ops = {
  385. .map_bus = ks_pcie_other_map_bus,
  386. .read = pci_generic_config_read,
  387. .write = pci_generic_config_write,
  388. };
  389. static struct pci_ops ks_pcie_ops = {
  390. .map_bus = dw_pcie_own_conf_map_bus,
  391. .read = pci_generic_config_read,
  392. .write = pci_generic_config_write,
  393. };
  394. /**
  395. * ks_pcie_link_up() - Check if link up
  396. * @pci: A pointer to the dw_pcie structure which holds the DesignWare PCIe host
  397. * controller driver information.
  398. */
  399. static bool ks_pcie_link_up(struct dw_pcie *pci)
  400. {
  401. u32 val;
  402. val = dw_pcie_readl_dbi(pci, PCIE_PORT_DEBUG0);
  403. return (val & PORT_LOGIC_LTSSM_STATE_MASK) == PORT_LOGIC_LTSSM_STATE_L0;
  404. }
  405. static void ks_pcie_stop_link(struct dw_pcie *pci)
  406. {
  407. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  408. u32 val;
  409. /* Disable Link training */
  410. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  411. val &= ~LTSSM_EN_VAL;
  412. ks_pcie_app_writel(ks_pcie, CMD_STATUS, val);
  413. }
  414. static int ks_pcie_start_link(struct dw_pcie *pci)
  415. {
  416. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  417. u32 val;
  418. /* Initiate Link Training */
  419. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  420. ks_pcie_app_writel(ks_pcie, CMD_STATUS, LTSSM_EN_VAL | val);
  421. return 0;
  422. }
  423. static void ks_pcie_quirk(struct pci_dev *dev)
  424. {
  425. struct pci_bus *bus = dev->bus;
  426. struct keystone_pcie *ks_pcie;
  427. struct device *bridge_dev;
  428. struct pci_dev *bridge;
  429. u32 val;
  430. static const struct pci_device_id rc_pci_devids[] = {
  431. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2HK),
  432. .class = PCI_CLASS_BRIDGE_PCI_NORMAL, .class_mask = ~0, },
  433. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2E),
  434. .class = PCI_CLASS_BRIDGE_PCI_NORMAL, .class_mask = ~0, },
  435. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2L),
  436. .class = PCI_CLASS_BRIDGE_PCI_NORMAL, .class_mask = ~0, },
  437. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2G),
  438. .class = PCI_CLASS_BRIDGE_PCI_NORMAL, .class_mask = ~0, },
  439. { 0, },
  440. };
  441. static const struct pci_device_id am6_pci_devids[] = {
  442. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_AM654X),
  443. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  444. { 0, },
  445. };
  446. if (pci_is_root_bus(bus))
  447. bridge = dev;
  448. /* look for the host bridge */
  449. while (!pci_is_root_bus(bus)) {
  450. bridge = bus->self;
  451. bus = bus->parent;
  452. }
  453. if (!bridge)
  454. return;
  455. /*
  456. * Keystone PCI controller has a h/w limitation of
  457. * 256 bytes maximum read request size. It can't handle
  458. * anything higher than this. So force this limit on
  459. * all downstream devices.
  460. */
  461. if (pci_match_id(rc_pci_devids, bridge)) {
  462. if (pcie_get_readrq(dev) > 256) {
  463. dev_info(&dev->dev, "limiting MRRS to 256 bytes\n");
  464. pcie_set_readrq(dev, 256);
  465. }
  466. }
  467. /*
  468. * Memory transactions fail with PCI controller in AM654 PG1.0
  469. * when MRRS is set to more than 128 bytes. Force the MRRS to
  470. * 128 bytes in all downstream devices.
  471. */
  472. if (pci_match_id(am6_pci_devids, bridge)) {
  473. bridge_dev = pci_get_host_bridge_device(dev);
  474. if (!bridge_dev || !bridge_dev->parent)
  475. return;
  476. ks_pcie = dev_get_drvdata(bridge_dev->parent);
  477. if (!ks_pcie)
  478. return;
  479. val = ks_pcie_app_readl(ks_pcie, PID);
  480. val &= RTL;
  481. val >>= RTL_SHIFT;
  482. if (val != AM6_PCI_PG1_RTL_VER)
  483. return;
  484. if (pcie_get_readrq(dev) > 128) {
  485. dev_info(&dev->dev, "limiting MRRS to 128 bytes\n");
  486. pcie_set_readrq(dev, 128);
  487. }
  488. }
  489. }
  490. DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, ks_pcie_quirk);
  491. static void ks_pcie_msi_irq_handler(struct irq_desc *desc)
  492. {
  493. unsigned int irq = desc->irq_data.hwirq;
  494. struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
  495. u32 offset = irq - ks_pcie->msi_host_irq;
  496. struct dw_pcie *pci = ks_pcie->pci;
  497. struct dw_pcie_rp *pp = &pci->pp;
  498. struct device *dev = pci->dev;
  499. struct irq_chip *chip = irq_desc_get_chip(desc);
  500. u32 vector, reg, pos;
  501. dev_dbg(dev, "%s, irq %d\n", __func__, irq);
  502. /*
  503. * The chained irq handler installation would have replaced normal
  504. * interrupt driver handler so we need to take care of mask/unmask and
  505. * ack operation.
  506. */
  507. chained_irq_enter(chip, desc);
  508. reg = ks_pcie_app_readl(ks_pcie, MSI_IRQ_STATUS(offset));
  509. /*
  510. * MSI0 status bit 0-3 shows vectors 0, 8, 16, 24, MSI1 status bit
  511. * shows 1, 9, 17, 25 and so forth
  512. */
  513. for (pos = 0; pos < 4; pos++) {
  514. if (!(reg & BIT(pos)))
  515. continue;
  516. vector = offset + (pos << 3);
  517. dev_dbg(dev, "irq: bit %d, vector %d\n", pos, vector);
  518. generic_handle_domain_irq(pp->irq_domain, vector);
  519. }
  520. chained_irq_exit(chip, desc);
  521. }
  522. /**
  523. * ks_pcie_intx_irq_handler() - Handle INTX interrupt
  524. * @desc: Pointer to irq descriptor
  525. *
  526. * Traverse through pending INTX interrupts and invoke handler for each. Also
  527. * takes care of interrupt controller level mask/ack operation.
  528. */
  529. static void ks_pcie_intx_irq_handler(struct irq_desc *desc)
  530. {
  531. unsigned int irq = irq_desc_get_irq(desc);
  532. struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
  533. struct dw_pcie *pci = ks_pcie->pci;
  534. struct device *dev = pci->dev;
  535. u32 irq_offset = irq - ks_pcie->intx_host_irqs[0];
  536. struct irq_chip *chip = irq_desc_get_chip(desc);
  537. dev_dbg(dev, ": Handling INTX irq %d\n", irq);
  538. /*
  539. * The chained irq handler installation would have replaced normal
  540. * interrupt driver handler so we need to take care of mask/unmask and
  541. * ack operation.
  542. */
  543. chained_irq_enter(chip, desc);
  544. ks_pcie_handle_intx_irq(ks_pcie, irq_offset);
  545. chained_irq_exit(chip, desc);
  546. }
  547. static int ks_pcie_config_msi_irq(struct keystone_pcie *ks_pcie)
  548. {
  549. struct device *dev = ks_pcie->pci->dev;
  550. struct device_node *np = ks_pcie->np;
  551. struct device_node *intc_np;
  552. struct irq_data *irq_data;
  553. int irq_count, irq, ret, i;
  554. if (!IS_ENABLED(CONFIG_PCI_MSI))
  555. return 0;
  556. intc_np = of_get_child_by_name(np, "msi-interrupt-controller");
  557. if (!intc_np) {
  558. if (ks_pcie->is_am6)
  559. return 0;
  560. dev_warn(dev, "msi-interrupt-controller node is absent\n");
  561. return -EINVAL;
  562. }
  563. irq_count = of_irq_count(intc_np);
  564. if (!irq_count) {
  565. dev_err(dev, "No IRQ entries in msi-interrupt-controller\n");
  566. ret = -EINVAL;
  567. goto err;
  568. }
  569. for (i = 0; i < irq_count; i++) {
  570. irq = irq_of_parse_and_map(intc_np, i);
  571. if (!irq) {
  572. ret = -EINVAL;
  573. goto err;
  574. }
  575. if (!ks_pcie->msi_host_irq) {
  576. irq_data = irq_get_irq_data(irq);
  577. if (!irq_data) {
  578. ret = -EINVAL;
  579. goto err;
  580. }
  581. ks_pcie->msi_host_irq = irq_data->hwirq;
  582. }
  583. irq_set_chained_handler_and_data(irq, ks_pcie_msi_irq_handler,
  584. ks_pcie);
  585. }
  586. of_node_put(intc_np);
  587. return 0;
  588. err:
  589. of_node_put(intc_np);
  590. return ret;
  591. }
  592. static int ks_pcie_config_intx_irq(struct keystone_pcie *ks_pcie)
  593. {
  594. struct device *dev = ks_pcie->pci->dev;
  595. struct irq_domain *intx_irq_domain;
  596. struct device_node *np = ks_pcie->np;
  597. struct device_node *intc_np;
  598. int irq_count, irq, ret = 0, i;
  599. intc_np = of_get_child_by_name(np, "legacy-interrupt-controller");
  600. if (!intc_np) {
  601. /*
  602. * Since INTX interrupts are modeled as edge-interrupts in
  603. * AM6, keep it disabled for now.
  604. */
  605. if (ks_pcie->is_am6)
  606. return 0;
  607. dev_warn(dev, "legacy-interrupt-controller node is absent\n");
  608. return -EINVAL;
  609. }
  610. irq_count = of_irq_count(intc_np);
  611. if (!irq_count) {
  612. dev_err(dev, "No IRQ entries in legacy-interrupt-controller\n");
  613. ret = -EINVAL;
  614. goto err;
  615. }
  616. for (i = 0; i < irq_count; i++) {
  617. irq = irq_of_parse_and_map(intc_np, i);
  618. if (!irq) {
  619. ret = -EINVAL;
  620. goto err;
  621. }
  622. ks_pcie->intx_host_irqs[i] = irq;
  623. irq_set_chained_handler_and_data(irq,
  624. ks_pcie_intx_irq_handler,
  625. ks_pcie);
  626. }
  627. intx_irq_domain = irq_domain_create_linear(of_fwnode_handle(intc_np), PCI_NUM_INTX,
  628. &ks_pcie_intx_irq_domain_ops, NULL);
  629. if (!intx_irq_domain) {
  630. dev_err(dev, "Failed to add irq domain for INTX irqs\n");
  631. ret = -EINVAL;
  632. goto err;
  633. }
  634. ks_pcie->intx_irq_domain = intx_irq_domain;
  635. for (i = 0; i < PCI_NUM_INTX; i++)
  636. ks_pcie_app_writel(ks_pcie, IRQ_ENABLE_SET(i), INTx_EN);
  637. err:
  638. of_node_put(intc_np);
  639. return ret;
  640. }
  641. static int ks_pcie_init_id(struct keystone_pcie *ks_pcie)
  642. {
  643. int ret;
  644. unsigned int id;
  645. struct regmap *devctrl_regs;
  646. struct dw_pcie *pci = ks_pcie->pci;
  647. struct device *dev = pci->dev;
  648. struct device_node *np = dev->of_node;
  649. struct of_phandle_args args;
  650. unsigned int offset = 0;
  651. devctrl_regs = syscon_regmap_lookup_by_phandle(np, "ti,syscon-pcie-id");
  652. if (IS_ERR(devctrl_regs))
  653. return PTR_ERR(devctrl_regs);
  654. /* Do not error out to maintain old DT compatibility */
  655. ret = of_parse_phandle_with_fixed_args(np, "ti,syscon-pcie-id", 1, 0, &args);
  656. if (!ret)
  657. offset = args.args[0];
  658. ret = regmap_read(devctrl_regs, offset, &id);
  659. if (ret)
  660. return ret;
  661. dw_pcie_dbi_ro_wr_en(pci);
  662. dw_pcie_writew_dbi(pci, PCI_VENDOR_ID, id & PCIE_VENDORID_MASK);
  663. dw_pcie_writew_dbi(pci, PCI_DEVICE_ID, id >> PCIE_DEVICEID_SHIFT);
  664. dw_pcie_dbi_ro_wr_dis(pci);
  665. return 0;
  666. }
  667. static int ks_pcie_host_init(struct dw_pcie_rp *pp)
  668. {
  669. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  670. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  671. int ret;
  672. pp->bridge->ops = &ks_pcie_ops;
  673. if (!ks_pcie->is_am6)
  674. pp->bridge->child_ops = &ks_child_pcie_ops;
  675. ret = ks_pcie_config_intx_irq(ks_pcie);
  676. if (ret)
  677. return ret;
  678. ret = ks_pcie_config_msi_irq(ks_pcie);
  679. if (ret)
  680. return ret;
  681. ks_pcie_stop_link(pci);
  682. ret = ks_pcie_setup_rc_app_regs(ks_pcie);
  683. if (ret)
  684. return ret;
  685. writew(PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8),
  686. pci->dbi_base + PCI_IO_BASE);
  687. ret = ks_pcie_init_id(ks_pcie);
  688. if (ret < 0)
  689. return ret;
  690. return 0;
  691. }
  692. static const struct dw_pcie_host_ops ks_pcie_host_ops = {
  693. .init = ks_pcie_host_init,
  694. .msi_init = ks_pcie_msi_host_init,
  695. };
  696. static const struct dw_pcie_host_ops ks_pcie_am654_host_ops = {
  697. .init = ks_pcie_host_init,
  698. };
  699. static irqreturn_t ks_pcie_err_irq_handler(int irq, void *priv)
  700. {
  701. struct keystone_pcie *ks_pcie = priv;
  702. return ks_pcie_handle_error_irq(ks_pcie);
  703. }
  704. static void ks_pcie_am654_write_dbi2(struct dw_pcie *pci, void __iomem *base,
  705. u32 reg, size_t size, u32 val)
  706. {
  707. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  708. ks_pcie_set_dbi_mode(ks_pcie);
  709. dw_pcie_write(base + reg, size, val);
  710. ks_pcie_clear_dbi_mode(ks_pcie);
  711. }
  712. static const struct dw_pcie_ops ks_pcie_dw_pcie_ops = {
  713. .start_link = ks_pcie_start_link,
  714. .stop_link = ks_pcie_stop_link,
  715. .link_up = ks_pcie_link_up,
  716. .write_dbi2 = ks_pcie_am654_write_dbi2,
  717. };
  718. static void ks_pcie_am654_ep_init(struct dw_pcie_ep *ep)
  719. {
  720. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  721. int flags;
  722. ep->page_size = AM654_WIN_SIZE;
  723. flags = PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_32;
  724. dw_pcie_writel_dbi2(pci, PCI_BASE_ADDRESS_0, APP_ADDR_SPACE_0 - 1);
  725. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, flags);
  726. }
  727. static void ks_pcie_am654_raise_intx_irq(struct keystone_pcie *ks_pcie)
  728. {
  729. struct dw_pcie *pci = ks_pcie->pci;
  730. u8 int_pin;
  731. int_pin = dw_pcie_readb_dbi(pci, PCI_INTERRUPT_PIN);
  732. if (int_pin == 0 || int_pin > 4)
  733. return;
  734. ks_pcie_app_writel(ks_pcie, PCIE_LEGACY_IRQ_ENABLE_SET(int_pin),
  735. INT_ENABLE);
  736. ks_pcie_app_writel(ks_pcie, PCIE_EP_IRQ_SET, INT_ENABLE);
  737. mdelay(1);
  738. ks_pcie_app_writel(ks_pcie, PCIE_EP_IRQ_CLR, INT_ENABLE);
  739. ks_pcie_app_writel(ks_pcie, PCIE_LEGACY_IRQ_ENABLE_CLR(int_pin),
  740. INT_ENABLE);
  741. }
  742. static int ks_pcie_am654_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
  743. unsigned int type, u16 interrupt_num)
  744. {
  745. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  746. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  747. switch (type) {
  748. case PCI_IRQ_INTX:
  749. ks_pcie_am654_raise_intx_irq(ks_pcie);
  750. break;
  751. case PCI_IRQ_MSI:
  752. dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num);
  753. break;
  754. case PCI_IRQ_MSIX:
  755. dw_pcie_ep_raise_msix_irq(ep, func_no, interrupt_num);
  756. break;
  757. default:
  758. dev_err(pci->dev, "UNKNOWN IRQ type\n");
  759. return -EINVAL;
  760. }
  761. return 0;
  762. }
  763. static const struct pci_epc_features ks_pcie_am654_epc_features = {
  764. DWC_EPC_COMMON_FEATURES,
  765. .msi_capable = true,
  766. .msix_capable = true,
  767. .bar[BAR_0] = { .type = BAR_RESERVED, },
  768. .bar[BAR_1] = { .type = BAR_RESERVED, },
  769. .bar[BAR_2] = { .type = BAR_RESIZABLE, },
  770. .bar[BAR_3] = { .type = BAR_FIXED, .fixed_size = SZ_64K, },
  771. .bar[BAR_4] = { .type = BAR_FIXED, .fixed_size = 256, },
  772. .bar[BAR_5] = { .type = BAR_RESIZABLE, },
  773. .align = SZ_64K,
  774. };
  775. static const struct pci_epc_features*
  776. ks_pcie_am654_get_features(struct dw_pcie_ep *ep)
  777. {
  778. return &ks_pcie_am654_epc_features;
  779. }
  780. static const struct dw_pcie_ep_ops ks_pcie_am654_ep_ops = {
  781. .init = ks_pcie_am654_ep_init,
  782. .raise_irq = ks_pcie_am654_raise_irq,
  783. .get_features = &ks_pcie_am654_get_features,
  784. };
  785. static void ks_pcie_disable_phy(struct keystone_pcie *ks_pcie)
  786. {
  787. int num_lanes = ks_pcie->num_lanes;
  788. while (num_lanes--) {
  789. phy_power_off(ks_pcie->phy[num_lanes]);
  790. phy_exit(ks_pcie->phy[num_lanes]);
  791. }
  792. }
  793. static int ks_pcie_enable_phy(struct keystone_pcie *ks_pcie)
  794. {
  795. int i;
  796. int ret;
  797. int num_lanes = ks_pcie->num_lanes;
  798. for (i = 0; i < num_lanes; i++) {
  799. ret = phy_reset(ks_pcie->phy[i]);
  800. if (ret < 0)
  801. goto err_phy;
  802. ret = phy_init(ks_pcie->phy[i]);
  803. if (ret < 0)
  804. goto err_phy;
  805. ret = phy_power_on(ks_pcie->phy[i]);
  806. if (ret < 0) {
  807. phy_exit(ks_pcie->phy[i]);
  808. goto err_phy;
  809. }
  810. }
  811. return 0;
  812. err_phy:
  813. while (--i >= 0) {
  814. phy_power_off(ks_pcie->phy[i]);
  815. phy_exit(ks_pcie->phy[i]);
  816. }
  817. return ret;
  818. }
  819. static int ks_pcie_set_mode(struct device *dev)
  820. {
  821. struct device_node *np = dev->of_node;
  822. struct of_phandle_args args;
  823. unsigned int offset = 0;
  824. struct regmap *syscon;
  825. u32 val;
  826. u32 mask;
  827. int ret = 0;
  828. syscon = syscon_regmap_lookup_by_phandle(np, "ti,syscon-pcie-mode");
  829. if (IS_ERR(syscon))
  830. return 0;
  831. /* Do not error out to maintain old DT compatibility */
  832. ret = of_parse_phandle_with_fixed_args(np, "ti,syscon-pcie-mode", 1, 0, &args);
  833. if (!ret)
  834. offset = args.args[0];
  835. mask = KS_PCIE_DEV_TYPE_MASK | KS_PCIE_SYSCLOCKOUTEN;
  836. val = KS_PCIE_DEV_TYPE(RC) | KS_PCIE_SYSCLOCKOUTEN;
  837. ret = regmap_update_bits(syscon, offset, mask, val);
  838. if (ret) {
  839. dev_err(dev, "failed to set pcie mode\n");
  840. return ret;
  841. }
  842. return 0;
  843. }
  844. static int ks_pcie_am654_set_mode(struct device *dev,
  845. enum dw_pcie_device_mode mode)
  846. {
  847. struct device_node *np = dev->of_node;
  848. struct of_phandle_args args;
  849. unsigned int offset = 0;
  850. struct regmap *syscon;
  851. u32 val;
  852. u32 mask;
  853. int ret = 0;
  854. syscon = syscon_regmap_lookup_by_phandle(np, "ti,syscon-pcie-mode");
  855. if (IS_ERR(syscon))
  856. return 0;
  857. /* Do not error out to maintain old DT compatibility */
  858. ret = of_parse_phandle_with_fixed_args(np, "ti,syscon-pcie-mode", 1, 0, &args);
  859. if (!ret)
  860. offset = args.args[0];
  861. mask = AM654_PCIE_DEV_TYPE_MASK;
  862. switch (mode) {
  863. case DW_PCIE_RC_TYPE:
  864. val = RC;
  865. break;
  866. case DW_PCIE_EP_TYPE:
  867. val = EP;
  868. break;
  869. default:
  870. dev_err(dev, "INVALID device type %d\n", mode);
  871. return -EINVAL;
  872. }
  873. ret = regmap_update_bits(syscon, offset, mask, val);
  874. if (ret) {
  875. dev_err(dev, "failed to set pcie mode\n");
  876. return ret;
  877. }
  878. return 0;
  879. }
  880. static const struct ks_pcie_of_data ks_pcie_rc_of_data = {
  881. .host_ops = &ks_pcie_host_ops,
  882. .mode = DW_PCIE_RC_TYPE,
  883. .version = DW_PCIE_VER_365A,
  884. };
  885. static const struct ks_pcie_of_data ks_pcie_am654_rc_of_data = {
  886. .host_ops = &ks_pcie_am654_host_ops,
  887. .mode = DW_PCIE_RC_TYPE,
  888. .version = DW_PCIE_VER_490A,
  889. };
  890. static const struct ks_pcie_of_data ks_pcie_am654_ep_of_data = {
  891. .ep_ops = &ks_pcie_am654_ep_ops,
  892. .mode = DW_PCIE_EP_TYPE,
  893. .version = DW_PCIE_VER_490A,
  894. };
  895. static const struct of_device_id ks_pcie_of_match[] = {
  896. {
  897. .type = "pci",
  898. .data = &ks_pcie_rc_of_data,
  899. .compatible = "ti,keystone-pcie",
  900. },
  901. {
  902. .data = &ks_pcie_am654_rc_of_data,
  903. .compatible = "ti,am654-pcie-rc",
  904. },
  905. {
  906. .data = &ks_pcie_am654_ep_of_data,
  907. .compatible = "ti,am654-pcie-ep",
  908. },
  909. { },
  910. };
  911. MODULE_DEVICE_TABLE(of, ks_pcie_of_match);
  912. static int ks_pcie_probe(struct platform_device *pdev)
  913. {
  914. const struct dw_pcie_host_ops *host_ops;
  915. const struct dw_pcie_ep_ops *ep_ops;
  916. struct device *dev = &pdev->dev;
  917. struct device_node *np = dev->of_node;
  918. const struct ks_pcie_of_data *data;
  919. enum dw_pcie_device_mode mode;
  920. struct dw_pcie *pci;
  921. struct keystone_pcie *ks_pcie;
  922. struct device_link **link;
  923. struct gpio_desc *gpiod;
  924. struct resource *res;
  925. void __iomem *base;
  926. u32 num_viewport;
  927. struct phy **phy;
  928. u32 num_lanes;
  929. char name[10];
  930. u32 version;
  931. int ret;
  932. int irq;
  933. int i;
  934. data = of_device_get_match_data(dev);
  935. if (!data)
  936. return -EINVAL;
  937. version = data->version;
  938. host_ops = data->host_ops;
  939. ep_ops = data->ep_ops;
  940. mode = data->mode;
  941. ks_pcie = devm_kzalloc(dev, sizeof(*ks_pcie), GFP_KERNEL);
  942. if (!ks_pcie)
  943. return -ENOMEM;
  944. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  945. if (!pci)
  946. return -ENOMEM;
  947. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "app");
  948. ks_pcie->va_app_base = devm_ioremap_resource(dev, res);
  949. if (IS_ERR(ks_pcie->va_app_base))
  950. return PTR_ERR(ks_pcie->va_app_base);
  951. ks_pcie->app = *res;
  952. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbics");
  953. base = devm_pci_remap_cfg_resource(dev, res);
  954. if (IS_ERR(base))
  955. return PTR_ERR(base);
  956. if (of_device_is_compatible(np, "ti,am654-pcie-rc"))
  957. ks_pcie->is_am6 = true;
  958. pci->dbi_base = base;
  959. pci->dbi_base2 = base;
  960. pci->dev = dev;
  961. pci->ops = &ks_pcie_dw_pcie_ops;
  962. pci->version = version;
  963. irq = platform_get_irq(pdev, 0);
  964. if (irq < 0)
  965. return irq;
  966. ret = devm_request_irq(dev, irq, ks_pcie_err_irq_handler, IRQF_SHARED,
  967. "ks-pcie-error-irq", ks_pcie);
  968. if (ret < 0) {
  969. dev_err(dev, "failed to request error IRQ %d\n",
  970. irq);
  971. return ret;
  972. }
  973. ret = of_property_read_u32(np, "num-lanes", &num_lanes);
  974. if (ret)
  975. num_lanes = 1;
  976. phy = devm_kcalloc(dev, num_lanes, sizeof(*phy), GFP_KERNEL);
  977. if (!phy)
  978. return -ENOMEM;
  979. link = devm_kcalloc(dev, num_lanes, sizeof(*link), GFP_KERNEL);
  980. if (!link)
  981. return -ENOMEM;
  982. for (i = 0; i < num_lanes; i++) {
  983. snprintf(name, sizeof(name), "pcie-phy%d", i);
  984. phy[i] = devm_phy_optional_get(dev, name);
  985. if (IS_ERR(phy[i])) {
  986. ret = PTR_ERR(phy[i]);
  987. goto err_link;
  988. }
  989. if (!phy[i])
  990. continue;
  991. link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS);
  992. if (!link[i]) {
  993. ret = -EINVAL;
  994. goto err_link;
  995. }
  996. }
  997. ks_pcie->np = np;
  998. ks_pcie->pci = pci;
  999. ks_pcie->link = link;
  1000. ks_pcie->num_lanes = num_lanes;
  1001. ks_pcie->phy = phy;
  1002. gpiod = devm_gpiod_get_optional(dev, "reset",
  1003. GPIOD_OUT_LOW);
  1004. if (IS_ERR(gpiod)) {
  1005. ret = PTR_ERR(gpiod);
  1006. if (ret != -EPROBE_DEFER)
  1007. dev_err(dev, "Failed to get reset GPIO\n");
  1008. goto err_link;
  1009. }
  1010. /* Obtain references to the PHYs */
  1011. for (i = 0; i < num_lanes; i++)
  1012. phy_pm_runtime_get_sync(ks_pcie->phy[i]);
  1013. ret = ks_pcie_enable_phy(ks_pcie);
  1014. /* Release references to the PHYs */
  1015. for (i = 0; i < num_lanes; i++)
  1016. phy_pm_runtime_put_sync(ks_pcie->phy[i]);
  1017. if (ret) {
  1018. dev_err(dev, "failed to enable phy\n");
  1019. goto err_link;
  1020. }
  1021. platform_set_drvdata(pdev, ks_pcie);
  1022. pm_runtime_enable(dev);
  1023. ret = pm_runtime_get_sync(dev);
  1024. if (ret < 0) {
  1025. dev_err(dev, "pm_runtime_get_sync failed\n");
  1026. goto err_get_sync;
  1027. }
  1028. if (dw_pcie_ver_is_ge(pci, 480A))
  1029. ret = ks_pcie_am654_set_mode(dev, mode);
  1030. else
  1031. ret = ks_pcie_set_mode(dev);
  1032. if (ret < 0)
  1033. goto err_get_sync;
  1034. switch (mode) {
  1035. case DW_PCIE_RC_TYPE:
  1036. if (!IS_ENABLED(CONFIG_PCI_KEYSTONE_HOST)) {
  1037. ret = -ENODEV;
  1038. goto err_get_sync;
  1039. }
  1040. ret = of_property_read_u32(np, "num-viewport", &num_viewport);
  1041. if (ret < 0) {
  1042. dev_err(dev, "unable to read *num-viewport* property\n");
  1043. goto err_get_sync;
  1044. }
  1045. /*
  1046. * "Power Sequencing and Reset Signal Timings" table in
  1047. * PCI EXPRESS CARD ELECTROMECHANICAL SPECIFICATION, REV. 2.0
  1048. * indicates PERST# should be deasserted after minimum of 100us
  1049. * once REFCLK is stable. The REFCLK to the connector in RC
  1050. * mode is selected while enabling the PHY. So deassert PERST#
  1051. * after 100 us.
  1052. */
  1053. if (gpiod) {
  1054. usleep_range(100, 200);
  1055. gpiod_set_value_cansleep(gpiod, 1);
  1056. }
  1057. ks_pcie->num_viewport = num_viewport;
  1058. pci->pp.ops = host_ops;
  1059. ret = dw_pcie_host_init(&pci->pp);
  1060. if (ret < 0)
  1061. goto err_get_sync;
  1062. break;
  1063. case DW_PCIE_EP_TYPE:
  1064. if (!IS_ENABLED(CONFIG_PCI_KEYSTONE_EP)) {
  1065. ret = -ENODEV;
  1066. goto err_get_sync;
  1067. }
  1068. pci->ep.ops = ep_ops;
  1069. ret = dw_pcie_ep_init(&pci->ep);
  1070. if (ret < 0)
  1071. goto err_get_sync;
  1072. ret = dw_pcie_ep_init_registers(&pci->ep);
  1073. if (ret) {
  1074. dev_err(dev, "Failed to initialize DWC endpoint registers\n");
  1075. goto err_ep_init;
  1076. }
  1077. pci_epc_init_notify(pci->ep.epc);
  1078. break;
  1079. default:
  1080. dev_err(dev, "INVALID device type %d\n", mode);
  1081. ret = -EINVAL;
  1082. goto err_get_sync;
  1083. }
  1084. ks_pcie_enable_error_irq(ks_pcie);
  1085. return 0;
  1086. err_ep_init:
  1087. dw_pcie_ep_deinit(&pci->ep);
  1088. err_get_sync:
  1089. pm_runtime_put(dev);
  1090. pm_runtime_disable(dev);
  1091. ks_pcie_disable_phy(ks_pcie);
  1092. err_link:
  1093. while (--i >= 0 && link[i])
  1094. device_link_del(link[i]);
  1095. return ret;
  1096. }
  1097. static void ks_pcie_remove(struct platform_device *pdev)
  1098. {
  1099. struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev);
  1100. struct device_link **link = ks_pcie->link;
  1101. int num_lanes = ks_pcie->num_lanes;
  1102. struct device *dev = &pdev->dev;
  1103. pm_runtime_put(dev);
  1104. pm_runtime_disable(dev);
  1105. ks_pcie_disable_phy(ks_pcie);
  1106. while (num_lanes--)
  1107. device_link_del(link[num_lanes]);
  1108. }
  1109. static struct platform_driver ks_pcie_driver = {
  1110. .probe = ks_pcie_probe,
  1111. .remove = ks_pcie_remove,
  1112. .driver = {
  1113. .name = "keystone-pcie",
  1114. .of_match_table = ks_pcie_of_match,
  1115. },
  1116. };
  1117. #ifdef CONFIG_ARM
  1118. /*
  1119. * When a PCI device does not exist during config cycles, keystone host
  1120. * gets a bus error instead of returning 0xffffffff (PCI_ERROR_RESPONSE).
  1121. * This handler always returns 0 for this kind of fault.
  1122. */
  1123. static int ks_pcie_fault(unsigned long addr, unsigned int fsr,
  1124. struct pt_regs *regs)
  1125. {
  1126. unsigned long instr = *(unsigned long *)instruction_pointer(regs);
  1127. if ((instr & 0x0e100090) == 0x00100090) {
  1128. int reg = (instr >> 12) & 15;
  1129. regs->uregs[reg] = -1;
  1130. regs->ARM_pc += 4;
  1131. }
  1132. return 0;
  1133. }
  1134. static int __init ks_pcie_init(void)
  1135. {
  1136. /*
  1137. * PCIe access errors that result into OCP errors are caught by ARM as
  1138. * "External aborts"
  1139. */
  1140. if (of_find_matching_node(NULL, ks_pcie_of_match))
  1141. hook_fault_code(17, ks_pcie_fault, SIGBUS, 0,
  1142. "Asynchronous external abort");
  1143. return platform_driver_register(&ks_pcie_driver);
  1144. }
  1145. device_initcall(ks_pcie_init);
  1146. #else
  1147. builtin_platform_driver(ks_pcie_driver);
  1148. #endif
  1149. MODULE_LICENSE("GPL");
  1150. MODULE_DESCRIPTION("PCIe controller driver for Texas Instruments Keystone SoCs");
  1151. MODULE_AUTHOR("Murali Karicheri <m-karicheri2@ti.com>");