pcie-iproc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (C) 2014 Hauke Mehrtens <hauke@hauke-m.de>
  3. * Copyright (C) 2015 Broadcom Corporation
  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_PERST_SOURCE_SELECT_SHIFT 2
  32. #define EP_PERST_SOURCE_SELECT BIT(EP_PERST_SOURCE_SELECT_SHIFT)
  33. #define EP_MODE_SURVIVE_PERST_SHIFT 1
  34. #define EP_MODE_SURVIVE_PERST BIT(EP_MODE_SURVIVE_PERST_SHIFT)
  35. #define RC_PCIE_RST_OUTPUT_SHIFT 0
  36. #define RC_PCIE_RST_OUTPUT BIT(RC_PCIE_RST_OUTPUT_SHIFT)
  37. #define CFG_IND_ADDR_OFFSET 0x120
  38. #define CFG_IND_ADDR_MASK 0x00001ffc
  39. #define CFG_IND_DATA_OFFSET 0x124
  40. #define CFG_ADDR_OFFSET 0x1f8
  41. #define CFG_ADDR_BUS_NUM_SHIFT 20
  42. #define CFG_ADDR_BUS_NUM_MASK 0x0ff00000
  43. #define CFG_ADDR_DEV_NUM_SHIFT 15
  44. #define CFG_ADDR_DEV_NUM_MASK 0x000f8000
  45. #define CFG_ADDR_FUNC_NUM_SHIFT 12
  46. #define CFG_ADDR_FUNC_NUM_MASK 0x00007000
  47. #define CFG_ADDR_REG_NUM_SHIFT 2
  48. #define CFG_ADDR_REG_NUM_MASK 0x00000ffc
  49. #define CFG_ADDR_CFG_TYPE_SHIFT 0
  50. #define CFG_ADDR_CFG_TYPE_MASK 0x00000003
  51. #define CFG_DATA_OFFSET 0x1fc
  52. #define SYS_RC_INTX_EN 0x330
  53. #define SYS_RC_INTX_MASK 0xf
  54. #define PCIE_LINK_STATUS_OFFSET 0xf0c
  55. #define PCIE_PHYLINKUP_SHIFT 3
  56. #define PCIE_PHYLINKUP BIT(PCIE_PHYLINKUP_SHIFT)
  57. #define PCIE_DL_ACTIVE_SHIFT 2
  58. #define PCIE_DL_ACTIVE BIT(PCIE_DL_ACTIVE_SHIFT)
  59. #define OARR_VALID_SHIFT 0
  60. #define OARR_VALID BIT(OARR_VALID_SHIFT)
  61. #define OARR_SIZE_CFG_SHIFT 1
  62. #define OARR_SIZE_CFG BIT(OARR_SIZE_CFG_SHIFT)
  63. #define OARR_LO(window) (0xd20 + (window) * 8)
  64. #define OARR_HI(window) (0xd24 + (window) * 8)
  65. #define OMAP_LO(window) (0xd40 + (window) * 8)
  66. #define OMAP_HI(window) (0xd44 + (window) * 8)
  67. #define MAX_NUM_OB_WINDOWS 2
  68. static inline struct iproc_pcie *iproc_data(struct pci_bus *bus)
  69. {
  70. struct iproc_pcie *pcie;
  71. #ifdef CONFIG_ARM
  72. struct pci_sys_data *sys = bus->sysdata;
  73. pcie = sys->private_data;
  74. #else
  75. pcie = bus->sysdata;
  76. #endif
  77. return pcie;
  78. }
  79. /**
  80. * Note access to the configuration registers are protected at the higher layer
  81. * by 'pci_lock' in drivers/pci/access.c
  82. */
  83. static void __iomem *iproc_pcie_map_cfg_bus(struct pci_bus *bus,
  84. unsigned int devfn,
  85. int where)
  86. {
  87. struct iproc_pcie *pcie = iproc_data(bus);
  88. unsigned slot = PCI_SLOT(devfn);
  89. unsigned fn = PCI_FUNC(devfn);
  90. unsigned busno = bus->number;
  91. u32 val;
  92. /* root complex access */
  93. if (busno == 0) {
  94. if (slot >= 1)
  95. return NULL;
  96. writel(where & CFG_IND_ADDR_MASK,
  97. pcie->base + CFG_IND_ADDR_OFFSET);
  98. return (pcie->base + CFG_IND_DATA_OFFSET);
  99. }
  100. if (fn > 1)
  101. return NULL;
  102. /* EP device access */
  103. val = (busno << CFG_ADDR_BUS_NUM_SHIFT) |
  104. (slot << CFG_ADDR_DEV_NUM_SHIFT) |
  105. (fn << CFG_ADDR_FUNC_NUM_SHIFT) |
  106. (where & CFG_ADDR_REG_NUM_MASK) |
  107. (1 & CFG_ADDR_CFG_TYPE_MASK);
  108. writel(val, pcie->base + CFG_ADDR_OFFSET);
  109. return (pcie->base + CFG_DATA_OFFSET);
  110. }
  111. static struct pci_ops iproc_pcie_ops = {
  112. .map_bus = iproc_pcie_map_cfg_bus,
  113. .read = pci_generic_config_read32,
  114. .write = pci_generic_config_write32,
  115. };
  116. static void iproc_pcie_reset(struct iproc_pcie *pcie)
  117. {
  118. u32 val;
  119. /*
  120. * Select perst_b signal as reset source. Put the device into reset,
  121. * and then bring it out of reset
  122. */
  123. val = readl(pcie->base + CLK_CONTROL_OFFSET);
  124. val &= ~EP_PERST_SOURCE_SELECT & ~EP_MODE_SURVIVE_PERST &
  125. ~RC_PCIE_RST_OUTPUT;
  126. writel(val, pcie->base + CLK_CONTROL_OFFSET);
  127. udelay(250);
  128. val |= RC_PCIE_RST_OUTPUT;
  129. writel(val, pcie->base + CLK_CONTROL_OFFSET);
  130. msleep(100);
  131. }
  132. static int iproc_pcie_check_link(struct iproc_pcie *pcie, struct pci_bus *bus)
  133. {
  134. u8 hdr_type;
  135. u32 link_ctrl, class, val;
  136. u16 pos, link_status;
  137. bool link_is_active = false;
  138. val = readl(pcie->base + PCIE_LINK_STATUS_OFFSET);
  139. if (!(val & PCIE_PHYLINKUP) || !(val & PCIE_DL_ACTIVE)) {
  140. dev_err(pcie->dev, "PHY or data link is INACTIVE!\n");
  141. return -ENODEV;
  142. }
  143. /* make sure we are not in EP mode */
  144. pci_bus_read_config_byte(bus, 0, PCI_HEADER_TYPE, &hdr_type);
  145. if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE) {
  146. dev_err(pcie->dev, "in EP mode, hdr=%#02x\n", hdr_type);
  147. return -EFAULT;
  148. }
  149. /* force class to PCI_CLASS_BRIDGE_PCI (0x0604) */
  150. #define PCI_BRIDGE_CTRL_REG_OFFSET 0x43c
  151. #define PCI_CLASS_BRIDGE_MASK 0xffff00
  152. #define PCI_CLASS_BRIDGE_SHIFT 8
  153. pci_bus_read_config_dword(bus, 0, PCI_BRIDGE_CTRL_REG_OFFSET, &class);
  154. class &= ~PCI_CLASS_BRIDGE_MASK;
  155. class |= (PCI_CLASS_BRIDGE_PCI << PCI_CLASS_BRIDGE_SHIFT);
  156. pci_bus_write_config_dword(bus, 0, PCI_BRIDGE_CTRL_REG_OFFSET, class);
  157. /* check link status to see if link is active */
  158. pos = pci_bus_find_capability(bus, 0, PCI_CAP_ID_EXP);
  159. pci_bus_read_config_word(bus, 0, pos + PCI_EXP_LNKSTA, &link_status);
  160. if (link_status & PCI_EXP_LNKSTA_NLW)
  161. link_is_active = true;
  162. if (!link_is_active) {
  163. /* try GEN 1 link speed */
  164. #define PCI_LINK_STATUS_CTRL_2_OFFSET 0x0dc
  165. #define PCI_TARGET_LINK_SPEED_MASK 0xf
  166. #define PCI_TARGET_LINK_SPEED_GEN2 0x2
  167. #define PCI_TARGET_LINK_SPEED_GEN1 0x1
  168. pci_bus_read_config_dword(bus, 0,
  169. PCI_LINK_STATUS_CTRL_2_OFFSET,
  170. &link_ctrl);
  171. if ((link_ctrl & PCI_TARGET_LINK_SPEED_MASK) ==
  172. PCI_TARGET_LINK_SPEED_GEN2) {
  173. link_ctrl &= ~PCI_TARGET_LINK_SPEED_MASK;
  174. link_ctrl |= PCI_TARGET_LINK_SPEED_GEN1;
  175. pci_bus_write_config_dword(bus, 0,
  176. PCI_LINK_STATUS_CTRL_2_OFFSET,
  177. link_ctrl);
  178. msleep(100);
  179. pos = pci_bus_find_capability(bus, 0, PCI_CAP_ID_EXP);
  180. pci_bus_read_config_word(bus, 0, pos + PCI_EXP_LNKSTA,
  181. &link_status);
  182. if (link_status & PCI_EXP_LNKSTA_NLW)
  183. link_is_active = true;
  184. }
  185. }
  186. dev_info(pcie->dev, "link: %s\n", link_is_active ? "UP" : "DOWN");
  187. return link_is_active ? 0 : -ENODEV;
  188. }
  189. static void iproc_pcie_enable(struct iproc_pcie *pcie)
  190. {
  191. writel(SYS_RC_INTX_MASK, pcie->base + SYS_RC_INTX_EN);
  192. }
  193. /**
  194. * Some iProc SoCs require the SW to configure the outbound address mapping
  195. *
  196. * Outbound address translation:
  197. *
  198. * iproc_pcie_address = axi_address - axi_offset
  199. * OARR = iproc_pcie_address
  200. * OMAP = pci_addr
  201. *
  202. * axi_addr -> iproc_pcie_address -> OARR -> OMAP -> pci_address
  203. */
  204. static int iproc_pcie_setup_ob(struct iproc_pcie *pcie, u64 axi_addr,
  205. u64 pci_addr, resource_size_t size)
  206. {
  207. struct iproc_pcie_ob *ob = &pcie->ob;
  208. unsigned i;
  209. u64 max_size = (u64)ob->window_size * MAX_NUM_OB_WINDOWS;
  210. u64 remainder;
  211. if (size > max_size) {
  212. dev_err(pcie->dev,
  213. "res size 0x%pap exceeds max supported size 0x%llx\n",
  214. &size, max_size);
  215. return -EINVAL;
  216. }
  217. div64_u64_rem(size, ob->window_size, &remainder);
  218. if (remainder) {
  219. dev_err(pcie->dev,
  220. "res size %pap needs to be multiple of window size %pap\n",
  221. &size, &ob->window_size);
  222. return -EINVAL;
  223. }
  224. if (axi_addr < ob->axi_offset) {
  225. dev_err(pcie->dev,
  226. "axi address %pap less than offset %pap\n",
  227. &axi_addr, &ob->axi_offset);
  228. return -EINVAL;
  229. }
  230. /*
  231. * Translate the AXI address to the internal address used by the iProc
  232. * PCIe core before programming the OARR
  233. */
  234. axi_addr -= ob->axi_offset;
  235. for (i = 0; i < MAX_NUM_OB_WINDOWS; i++) {
  236. writel(lower_32_bits(axi_addr) | OARR_VALID |
  237. (ob->set_oarr_size ? 1 : 0), pcie->base + OARR_LO(i));
  238. writel(upper_32_bits(axi_addr), pcie->base + OARR_HI(i));
  239. writel(lower_32_bits(pci_addr), pcie->base + OMAP_LO(i));
  240. writel(upper_32_bits(pci_addr), pcie->base + OMAP_HI(i));
  241. size -= ob->window_size;
  242. if (size == 0)
  243. break;
  244. axi_addr += ob->window_size;
  245. pci_addr += ob->window_size;
  246. }
  247. return 0;
  248. }
  249. static int iproc_pcie_map_ranges(struct iproc_pcie *pcie,
  250. struct list_head *resources)
  251. {
  252. struct resource_entry *window;
  253. int ret;
  254. resource_list_for_each_entry(window, resources) {
  255. struct resource *res = window->res;
  256. u64 res_type = resource_type(res);
  257. switch (res_type) {
  258. case IORESOURCE_IO:
  259. case IORESOURCE_BUS:
  260. break;
  261. case IORESOURCE_MEM:
  262. ret = iproc_pcie_setup_ob(pcie, res->start,
  263. res->start - window->offset,
  264. resource_size(res));
  265. if (ret)
  266. return ret;
  267. break;
  268. default:
  269. dev_err(pcie->dev, "invalid resource %pR\n", res);
  270. return -EINVAL;
  271. }
  272. }
  273. return 0;
  274. }
  275. int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res)
  276. {
  277. int ret;
  278. void *sysdata;
  279. struct pci_bus *bus;
  280. if (!pcie || !pcie->dev || !pcie->base)
  281. return -EINVAL;
  282. ret = phy_init(pcie->phy);
  283. if (ret) {
  284. dev_err(pcie->dev, "unable to initialize PCIe PHY\n");
  285. return ret;
  286. }
  287. ret = phy_power_on(pcie->phy);
  288. if (ret) {
  289. dev_err(pcie->dev, "unable to power on PCIe PHY\n");
  290. goto err_exit_phy;
  291. }
  292. iproc_pcie_reset(pcie);
  293. if (pcie->need_ob_cfg) {
  294. ret = iproc_pcie_map_ranges(pcie, res);
  295. if (ret) {
  296. dev_err(pcie->dev, "map failed\n");
  297. goto err_power_off_phy;
  298. }
  299. }
  300. #ifdef CONFIG_ARM
  301. pcie->sysdata.private_data = pcie;
  302. sysdata = &pcie->sysdata;
  303. #else
  304. sysdata = pcie;
  305. #endif
  306. bus = pci_create_root_bus(pcie->dev, 0, &iproc_pcie_ops, sysdata, res);
  307. if (!bus) {
  308. dev_err(pcie->dev, "unable to create PCI root bus\n");
  309. ret = -ENOMEM;
  310. goto err_power_off_phy;
  311. }
  312. pcie->root_bus = bus;
  313. ret = iproc_pcie_check_link(pcie, bus);
  314. if (ret) {
  315. dev_err(pcie->dev, "no PCIe EP device detected\n");
  316. goto err_rm_root_bus;
  317. }
  318. iproc_pcie_enable(pcie);
  319. pci_scan_child_bus(bus);
  320. pci_assign_unassigned_bus_resources(bus);
  321. pci_fixup_irqs(pci_common_swizzle, pcie->map_irq);
  322. pci_bus_add_devices(bus);
  323. return 0;
  324. err_rm_root_bus:
  325. pci_stop_root_bus(bus);
  326. pci_remove_root_bus(bus);
  327. err_power_off_phy:
  328. phy_power_off(pcie->phy);
  329. err_exit_phy:
  330. phy_exit(pcie->phy);
  331. return ret;
  332. }
  333. EXPORT_SYMBOL(iproc_pcie_setup);
  334. int iproc_pcie_remove(struct iproc_pcie *pcie)
  335. {
  336. pci_stop_root_bus(pcie->root_bus);
  337. pci_remove_root_bus(pcie->root_bus);
  338. phy_power_off(pcie->phy);
  339. phy_exit(pcie->phy);
  340. return 0;
  341. }
  342. EXPORT_SYMBOL(iproc_pcie_remove);
  343. MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
  344. MODULE_DESCRIPTION("Broadcom iPROC PCIe common driver");
  345. MODULE_LICENSE("GPL v2");