pcie-iproc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (C) 2014 Hauke Mehrtens <hauke@hauke-m.de>
  3. * Copyright (C) 2015 Broadcom Corporatcommon ion
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation version 2.
  8. *
  9. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  10. * kind, whether express or implied; without even the implied warranty
  11. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/pci.h>
  16. #include <linux/msi.h>
  17. #include <linux/clk.h>
  18. #include <linux/module.h>
  19. #include <linux/mbus.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_pci.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/phy/phy.h>
  29. #include "pcie-iproc.h"
  30. #define CLK_CONTROL_OFFSET 0x000
  31. #define EP_MODE_SURVIVE_PERST_SHIFT 1
  32. #define EP_MODE_SURVIVE_PERST BIT(EP_MODE_SURVIVE_PERST_SHIFT)
  33. #define RC_PCIE_RST_OUTPUT_SHIFT 0
  34. #define RC_PCIE_RST_OUTPUT BIT(RC_PCIE_RST_OUTPUT_SHIFT)
  35. #define CFG_IND_ADDR_OFFSET 0x120
  36. #define CFG_IND_ADDR_MASK 0x00001ffc
  37. #define CFG_IND_DATA_OFFSET 0x124
  38. #define CFG_ADDR_OFFSET 0x1f8
  39. #define CFG_ADDR_BUS_NUM_SHIFT 20
  40. #define CFG_ADDR_BUS_NUM_MASK 0x0ff00000
  41. #define CFG_ADDR_DEV_NUM_SHIFT 15
  42. #define CFG_ADDR_DEV_NUM_MASK 0x000f8000
  43. #define CFG_ADDR_FUNC_NUM_SHIFT 12
  44. #define CFG_ADDR_FUNC_NUM_MASK 0x00007000
  45. #define CFG_ADDR_REG_NUM_SHIFT 2
  46. #define CFG_ADDR_REG_NUM_MASK 0x00000ffc
  47. #define CFG_ADDR_CFG_TYPE_SHIFT 0
  48. #define CFG_ADDR_CFG_TYPE_MASK 0x00000003
  49. #define CFG_DATA_OFFSET 0x1fc
  50. #define SYS_RC_INTX_EN 0x330
  51. #define SYS_RC_INTX_MASK 0xf
  52. static inline struct iproc_pcie *iproc_data(struct pci_bus *bus)
  53. {
  54. struct iproc_pcie *pcie;
  55. #ifdef CONFIG_ARM
  56. struct pci_sys_data *sys = bus->sysdata;
  57. pcie = sys->private_data;
  58. #else
  59. pcie = bus->sysdata;
  60. #endif
  61. return pcie;
  62. }
  63. /**
  64. * Note access to the configuration registers are protected at the higher layer
  65. * by 'pci_lock' in drivers/pci/access.c
  66. */
  67. static void __iomem *iproc_pcie_map_cfg_bus(struct pci_bus *bus,
  68. unsigned int devfn,
  69. int where)
  70. {
  71. struct iproc_pcie *pcie = iproc_data(bus);
  72. unsigned slot = PCI_SLOT(devfn);
  73. unsigned fn = PCI_FUNC(devfn);
  74. unsigned busno = bus->number;
  75. u32 val;
  76. /* root complex access */
  77. if (busno == 0) {
  78. if (slot >= 1)
  79. return NULL;
  80. writel(where & CFG_IND_ADDR_MASK,
  81. pcie->base + CFG_IND_ADDR_OFFSET);
  82. return (pcie->base + CFG_IND_DATA_OFFSET);
  83. }
  84. if (fn > 1)
  85. return NULL;
  86. /* EP device access */
  87. val = (busno << CFG_ADDR_BUS_NUM_SHIFT) |
  88. (slot << CFG_ADDR_DEV_NUM_SHIFT) |
  89. (fn << CFG_ADDR_FUNC_NUM_SHIFT) |
  90. (where & CFG_ADDR_REG_NUM_MASK) |
  91. (1 & CFG_ADDR_CFG_TYPE_MASK);
  92. writel(val, pcie->base + CFG_ADDR_OFFSET);
  93. return (pcie->base + CFG_DATA_OFFSET);
  94. }
  95. static struct pci_ops iproc_pcie_ops = {
  96. .map_bus = iproc_pcie_map_cfg_bus,
  97. .read = pci_generic_config_read32,
  98. .write = pci_generic_config_write32,
  99. };
  100. static void iproc_pcie_reset(struct iproc_pcie *pcie)
  101. {
  102. u32 val;
  103. /*
  104. * Configure the PCIe controller as root complex and send a downstream
  105. * reset
  106. */
  107. val = EP_MODE_SURVIVE_PERST | RC_PCIE_RST_OUTPUT;
  108. writel(val, pcie->base + CLK_CONTROL_OFFSET);
  109. udelay(250);
  110. val &= ~EP_MODE_SURVIVE_PERST;
  111. writel(val, pcie->base + CLK_CONTROL_OFFSET);
  112. msleep(250);
  113. }
  114. static int iproc_pcie_check_link(struct iproc_pcie *pcie, struct pci_bus *bus)
  115. {
  116. u8 hdr_type;
  117. u32 link_ctrl;
  118. u16 pos, link_status;
  119. int link_is_active = 0;
  120. /* make sure we are not in EP mode */
  121. pci_bus_read_config_byte(bus, 0, PCI_HEADER_TYPE, &hdr_type);
  122. if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE) {
  123. dev_err(pcie->dev, "in EP mode, hdr=%#02x\n", hdr_type);
  124. return -EFAULT;
  125. }
  126. /* force class to PCI_CLASS_BRIDGE_PCI (0x0604) */
  127. pci_bus_write_config_word(bus, 0, PCI_CLASS_DEVICE,
  128. PCI_CLASS_BRIDGE_PCI);
  129. /* check link status to see if link is active */
  130. pos = pci_bus_find_capability(bus, 0, PCI_CAP_ID_EXP);
  131. pci_bus_read_config_word(bus, 0, pos + PCI_EXP_LNKSTA, &link_status);
  132. if (link_status & PCI_EXP_LNKSTA_NLW)
  133. link_is_active = 1;
  134. if (!link_is_active) {
  135. /* try GEN 1 link speed */
  136. #define PCI_LINK_STATUS_CTRL_2_OFFSET 0x0dc
  137. #define PCI_TARGET_LINK_SPEED_MASK 0xf
  138. #define PCI_TARGET_LINK_SPEED_GEN2 0x2
  139. #define PCI_TARGET_LINK_SPEED_GEN1 0x1
  140. pci_bus_read_config_dword(bus, 0,
  141. PCI_LINK_STATUS_CTRL_2_OFFSET,
  142. &link_ctrl);
  143. if ((link_ctrl & PCI_TARGET_LINK_SPEED_MASK) ==
  144. PCI_TARGET_LINK_SPEED_GEN2) {
  145. link_ctrl &= ~PCI_TARGET_LINK_SPEED_MASK;
  146. link_ctrl |= PCI_TARGET_LINK_SPEED_GEN1;
  147. pci_bus_write_config_dword(bus, 0,
  148. PCI_LINK_STATUS_CTRL_2_OFFSET,
  149. link_ctrl);
  150. msleep(100);
  151. pos = pci_bus_find_capability(bus, 0, PCI_CAP_ID_EXP);
  152. pci_bus_read_config_word(bus, 0, pos + PCI_EXP_LNKSTA,
  153. &link_status);
  154. if (link_status & PCI_EXP_LNKSTA_NLW)
  155. link_is_active = 1;
  156. }
  157. }
  158. dev_info(pcie->dev, "link: %s\n", link_is_active ? "UP" : "DOWN");
  159. return link_is_active ? 0 : -ENODEV;
  160. }
  161. static void iproc_pcie_enable(struct iproc_pcie *pcie)
  162. {
  163. writel(SYS_RC_INTX_MASK, pcie->base + SYS_RC_INTX_EN);
  164. }
  165. int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res)
  166. {
  167. int ret;
  168. void *sysdata;
  169. struct pci_bus *bus;
  170. if (!pcie || !pcie->dev || !pcie->base)
  171. return -EINVAL;
  172. ret = phy_init(pcie->phy);
  173. if (ret) {
  174. dev_err(pcie->dev, "unable to initialize PCIe PHY\n");
  175. return ret;
  176. }
  177. ret = phy_power_on(pcie->phy);
  178. if (ret) {
  179. dev_err(pcie->dev, "unable to power on PCIe PHY\n");
  180. goto err_exit_phy;
  181. }
  182. iproc_pcie_reset(pcie);
  183. #ifdef CONFIG_ARM
  184. pcie->sysdata.private_data = pcie;
  185. sysdata = &pcie->sysdata;
  186. #else
  187. sysdata = pcie;
  188. #endif
  189. bus = pci_create_root_bus(pcie->dev, 0, &iproc_pcie_ops, sysdata, res);
  190. if (!bus) {
  191. dev_err(pcie->dev, "unable to create PCI root bus\n");
  192. ret = -ENOMEM;
  193. goto err_power_off_phy;
  194. }
  195. pcie->root_bus = bus;
  196. ret = iproc_pcie_check_link(pcie, bus);
  197. if (ret) {
  198. dev_err(pcie->dev, "no PCIe EP device detected\n");
  199. goto err_rm_root_bus;
  200. }
  201. iproc_pcie_enable(pcie);
  202. pci_scan_child_bus(bus);
  203. pci_assign_unassigned_bus_resources(bus);
  204. #ifdef CONFIG_ARM
  205. pci_fixup_irqs(pci_common_swizzle, pcie->map_irq);
  206. #endif
  207. pci_bus_add_devices(bus);
  208. return 0;
  209. err_rm_root_bus:
  210. pci_stop_root_bus(bus);
  211. pci_remove_root_bus(bus);
  212. err_power_off_phy:
  213. phy_power_off(pcie->phy);
  214. err_exit_phy:
  215. phy_exit(pcie->phy);
  216. return ret;
  217. }
  218. EXPORT_SYMBOL(iproc_pcie_setup);
  219. int iproc_pcie_remove(struct iproc_pcie *pcie)
  220. {
  221. pci_stop_root_bus(pcie->root_bus);
  222. pci_remove_root_bus(pcie->root_bus);
  223. phy_power_off(pcie->phy);
  224. phy_exit(pcie->phy);
  225. return 0;
  226. }
  227. EXPORT_SYMBOL(iproc_pcie_remove);
  228. MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
  229. MODULE_DESCRIPTION("Broadcom iPROC PCIe common driver");
  230. MODULE_LICENSE("GPL v2");