pcie-mt7621.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * BRIEF MODULE DESCRIPTION
  4. * PCI init for Ralink RT2880 solution
  5. *
  6. * Copyright 2007 Ralink Inc. ([email protected])
  7. *
  8. * May 2007 Bruce Chang
  9. * Initial Release
  10. *
  11. * May 2009 Bruce Chang
  12. * support RT2880/RT3883 PCIe
  13. *
  14. * May 2011 Bruce Chang
  15. * support RT6855/MT7620 PCIe
  16. */
  17. #include <linux/bitops.h>
  18. #include <linux/clk.h>
  19. #include <linux/delay.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_pci.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/pci.h>
  27. #include <linux/phy/phy.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/reset.h>
  30. #include <linux/sys_soc.h>
  31. #include "../pci.h"
  32. /* MediaTek-specific configuration registers */
  33. #define PCIE_FTS_NUM 0x70c
  34. #define PCIE_FTS_NUM_MASK GENMASK(15, 8)
  35. #define PCIE_FTS_NUM_L0(x) (((x) & 0xff) << 8)
  36. /* Host-PCI bridge registers */
  37. #define RALINK_PCI_PCICFG_ADDR 0x0000
  38. #define RALINK_PCI_PCIMSK_ADDR 0x000c
  39. #define RALINK_PCI_CONFIG_ADDR 0x0020
  40. #define RALINK_PCI_CONFIG_DATA 0x0024
  41. #define RALINK_PCI_MEMBASE 0x0028
  42. #define RALINK_PCI_IOBASE 0x002c
  43. /* PCIe RC control registers */
  44. #define RALINK_PCI_ID 0x0030
  45. #define RALINK_PCI_CLASS 0x0034
  46. #define RALINK_PCI_SUBID 0x0038
  47. #define RALINK_PCI_STATUS 0x0050
  48. /* Some definition values */
  49. #define PCIE_REVISION_ID BIT(0)
  50. #define PCIE_CLASS_CODE (0x60400 << 8)
  51. #define PCIE_BAR_MAP_MAX GENMASK(30, 16)
  52. #define PCIE_BAR_ENABLE BIT(0)
  53. #define PCIE_PORT_INT_EN(x) BIT(20 + (x))
  54. #define PCIE_PORT_LINKUP BIT(0)
  55. #define PCIE_PORT_CNT 3
  56. #define INIT_PORTS_DELAY_MS 100
  57. #define PERST_DELAY_MS 100
  58. /**
  59. * struct mt7621_pcie_port - PCIe port information
  60. * @base: I/O mapped register base
  61. * @list: port list
  62. * @pcie: pointer to PCIe host info
  63. * @clk: pointer to the port clock gate
  64. * @phy: pointer to PHY control block
  65. * @pcie_rst: pointer to port reset control
  66. * @gpio_rst: gpio reset
  67. * @slot: port slot
  68. * @enabled: indicates if port is enabled
  69. */
  70. struct mt7621_pcie_port {
  71. void __iomem *base;
  72. struct list_head list;
  73. struct mt7621_pcie *pcie;
  74. struct clk *clk;
  75. struct phy *phy;
  76. struct reset_control *pcie_rst;
  77. struct gpio_desc *gpio_rst;
  78. u32 slot;
  79. bool enabled;
  80. };
  81. /**
  82. * struct mt7621_pcie - PCIe host information
  83. * @base: IO Mapped Register Base
  84. * @dev: Pointer to PCIe device
  85. * @ports: pointer to PCIe port information
  86. * @resets_inverted: depends on chip revision
  87. * reset lines are inverted.
  88. */
  89. struct mt7621_pcie {
  90. struct device *dev;
  91. void __iomem *base;
  92. struct list_head ports;
  93. bool resets_inverted;
  94. };
  95. static inline u32 pcie_read(struct mt7621_pcie *pcie, u32 reg)
  96. {
  97. return readl_relaxed(pcie->base + reg);
  98. }
  99. static inline void pcie_write(struct mt7621_pcie *pcie, u32 val, u32 reg)
  100. {
  101. writel_relaxed(val, pcie->base + reg);
  102. }
  103. static inline u32 pcie_port_read(struct mt7621_pcie_port *port, u32 reg)
  104. {
  105. return readl_relaxed(port->base + reg);
  106. }
  107. static inline void pcie_port_write(struct mt7621_pcie_port *port,
  108. u32 val, u32 reg)
  109. {
  110. writel_relaxed(val, port->base + reg);
  111. }
  112. static void __iomem *mt7621_pcie_map_bus(struct pci_bus *bus,
  113. unsigned int devfn, int where)
  114. {
  115. struct mt7621_pcie *pcie = bus->sysdata;
  116. u32 address = PCI_CONF1_EXT_ADDRESS(bus->number, PCI_SLOT(devfn),
  117. PCI_FUNC(devfn), where);
  118. writel_relaxed(address, pcie->base + RALINK_PCI_CONFIG_ADDR);
  119. return pcie->base + RALINK_PCI_CONFIG_DATA + (where & 3);
  120. }
  121. static struct pci_ops mt7621_pcie_ops = {
  122. .map_bus = mt7621_pcie_map_bus,
  123. .read = pci_generic_config_read,
  124. .write = pci_generic_config_write,
  125. };
  126. static u32 read_config(struct mt7621_pcie *pcie, unsigned int dev, u32 reg)
  127. {
  128. u32 address = PCI_CONF1_EXT_ADDRESS(0, dev, 0, reg);
  129. pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
  130. return pcie_read(pcie, RALINK_PCI_CONFIG_DATA);
  131. }
  132. static void write_config(struct mt7621_pcie *pcie, unsigned int dev,
  133. u32 reg, u32 val)
  134. {
  135. u32 address = PCI_CONF1_EXT_ADDRESS(0, dev, 0, reg);
  136. pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
  137. pcie_write(pcie, val, RALINK_PCI_CONFIG_DATA);
  138. }
  139. static inline void mt7621_rst_gpio_pcie_assert(struct mt7621_pcie_port *port)
  140. {
  141. if (port->gpio_rst)
  142. gpiod_set_value(port->gpio_rst, 1);
  143. }
  144. static inline void mt7621_rst_gpio_pcie_deassert(struct mt7621_pcie_port *port)
  145. {
  146. if (port->gpio_rst)
  147. gpiod_set_value(port->gpio_rst, 0);
  148. }
  149. static inline bool mt7621_pcie_port_is_linkup(struct mt7621_pcie_port *port)
  150. {
  151. return (pcie_port_read(port, RALINK_PCI_STATUS) & PCIE_PORT_LINKUP) != 0;
  152. }
  153. static inline void mt7621_control_assert(struct mt7621_pcie_port *port)
  154. {
  155. struct mt7621_pcie *pcie = port->pcie;
  156. if (pcie->resets_inverted)
  157. reset_control_assert(port->pcie_rst);
  158. else
  159. reset_control_deassert(port->pcie_rst);
  160. }
  161. static inline void mt7621_control_deassert(struct mt7621_pcie_port *port)
  162. {
  163. struct mt7621_pcie *pcie = port->pcie;
  164. if (pcie->resets_inverted)
  165. reset_control_deassert(port->pcie_rst);
  166. else
  167. reset_control_assert(port->pcie_rst);
  168. }
  169. static int mt7621_pcie_parse_port(struct mt7621_pcie *pcie,
  170. struct device_node *node,
  171. int slot)
  172. {
  173. struct mt7621_pcie_port *port;
  174. struct device *dev = pcie->dev;
  175. struct platform_device *pdev = to_platform_device(dev);
  176. char name[10];
  177. int err;
  178. port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
  179. if (!port)
  180. return -ENOMEM;
  181. port->base = devm_platform_ioremap_resource(pdev, slot + 1);
  182. if (IS_ERR(port->base))
  183. return PTR_ERR(port->base);
  184. port->clk = devm_get_clk_from_child(dev, node, NULL);
  185. if (IS_ERR(port->clk)) {
  186. dev_err(dev, "failed to get pcie%d clock\n", slot);
  187. return PTR_ERR(port->clk);
  188. }
  189. port->pcie_rst = of_reset_control_get_exclusive(node, NULL);
  190. if (PTR_ERR(port->pcie_rst) == -EPROBE_DEFER) {
  191. dev_err(dev, "failed to get pcie%d reset control\n", slot);
  192. return PTR_ERR(port->pcie_rst);
  193. }
  194. snprintf(name, sizeof(name), "pcie-phy%d", slot);
  195. port->phy = devm_of_phy_get(dev, node, name);
  196. if (IS_ERR(port->phy)) {
  197. dev_err(dev, "failed to get pcie-phy%d\n", slot);
  198. err = PTR_ERR(port->phy);
  199. goto remove_reset;
  200. }
  201. port->gpio_rst = devm_gpiod_get_index_optional(dev, "reset", slot,
  202. GPIOD_OUT_LOW);
  203. if (IS_ERR(port->gpio_rst)) {
  204. dev_err(dev, "failed to get GPIO for PCIe%d\n", slot);
  205. err = PTR_ERR(port->gpio_rst);
  206. goto remove_reset;
  207. }
  208. port->slot = slot;
  209. port->pcie = pcie;
  210. INIT_LIST_HEAD(&port->list);
  211. list_add_tail(&port->list, &pcie->ports);
  212. return 0;
  213. remove_reset:
  214. reset_control_put(port->pcie_rst);
  215. return err;
  216. }
  217. static int mt7621_pcie_parse_dt(struct mt7621_pcie *pcie)
  218. {
  219. struct device *dev = pcie->dev;
  220. struct platform_device *pdev = to_platform_device(dev);
  221. struct device_node *node = dev->of_node, *child;
  222. int err;
  223. pcie->base = devm_platform_ioremap_resource(pdev, 0);
  224. if (IS_ERR(pcie->base))
  225. return PTR_ERR(pcie->base);
  226. for_each_available_child_of_node(node, child) {
  227. int slot;
  228. err = of_pci_get_devfn(child);
  229. if (err < 0) {
  230. of_node_put(child);
  231. dev_err(dev, "failed to parse devfn: %d\n", err);
  232. return err;
  233. }
  234. slot = PCI_SLOT(err);
  235. err = mt7621_pcie_parse_port(pcie, child, slot);
  236. if (err) {
  237. of_node_put(child);
  238. return err;
  239. }
  240. }
  241. return 0;
  242. }
  243. static int mt7621_pcie_init_port(struct mt7621_pcie_port *port)
  244. {
  245. struct mt7621_pcie *pcie = port->pcie;
  246. struct device *dev = pcie->dev;
  247. u32 slot = port->slot;
  248. int err;
  249. err = phy_init(port->phy);
  250. if (err) {
  251. dev_err(dev, "failed to initialize port%d phy\n", slot);
  252. return err;
  253. }
  254. err = phy_power_on(port->phy);
  255. if (err) {
  256. dev_err(dev, "failed to power on port%d phy\n", slot);
  257. phy_exit(port->phy);
  258. return err;
  259. }
  260. port->enabled = true;
  261. return 0;
  262. }
  263. static void mt7621_pcie_reset_assert(struct mt7621_pcie *pcie)
  264. {
  265. struct mt7621_pcie_port *port;
  266. list_for_each_entry(port, &pcie->ports, list) {
  267. /* PCIe RC reset assert */
  268. mt7621_control_assert(port);
  269. /* PCIe EP reset assert */
  270. mt7621_rst_gpio_pcie_assert(port);
  271. }
  272. msleep(PERST_DELAY_MS);
  273. }
  274. static void mt7621_pcie_reset_rc_deassert(struct mt7621_pcie *pcie)
  275. {
  276. struct mt7621_pcie_port *port;
  277. list_for_each_entry(port, &pcie->ports, list)
  278. mt7621_control_deassert(port);
  279. }
  280. static void mt7621_pcie_reset_ep_deassert(struct mt7621_pcie *pcie)
  281. {
  282. struct mt7621_pcie_port *port;
  283. list_for_each_entry(port, &pcie->ports, list)
  284. mt7621_rst_gpio_pcie_deassert(port);
  285. msleep(PERST_DELAY_MS);
  286. }
  287. static int mt7621_pcie_init_ports(struct mt7621_pcie *pcie)
  288. {
  289. struct device *dev = pcie->dev;
  290. struct mt7621_pcie_port *port, *tmp;
  291. u8 num_disabled = 0;
  292. int err;
  293. mt7621_pcie_reset_assert(pcie);
  294. mt7621_pcie_reset_rc_deassert(pcie);
  295. list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
  296. u32 slot = port->slot;
  297. if (slot == 1) {
  298. port->enabled = true;
  299. continue;
  300. }
  301. err = mt7621_pcie_init_port(port);
  302. if (err) {
  303. dev_err(dev, "initializing port %d failed\n", slot);
  304. list_del(&port->list);
  305. }
  306. }
  307. msleep(INIT_PORTS_DELAY_MS);
  308. mt7621_pcie_reset_ep_deassert(pcie);
  309. tmp = NULL;
  310. list_for_each_entry(port, &pcie->ports, list) {
  311. u32 slot = port->slot;
  312. if (!mt7621_pcie_port_is_linkup(port)) {
  313. dev_err(dev, "pcie%d no card, disable it (RST & CLK)\n",
  314. slot);
  315. mt7621_control_assert(port);
  316. port->enabled = false;
  317. num_disabled++;
  318. if (slot == 0) {
  319. tmp = port;
  320. continue;
  321. }
  322. if (slot == 1 && tmp && !tmp->enabled)
  323. phy_power_off(tmp->phy);
  324. }
  325. }
  326. return (num_disabled != PCIE_PORT_CNT) ? 0 : -ENODEV;
  327. }
  328. static void mt7621_pcie_enable_port(struct mt7621_pcie_port *port)
  329. {
  330. struct mt7621_pcie *pcie = port->pcie;
  331. u32 slot = port->slot;
  332. u32 val;
  333. /* enable pcie interrupt */
  334. val = pcie_read(pcie, RALINK_PCI_PCIMSK_ADDR);
  335. val |= PCIE_PORT_INT_EN(slot);
  336. pcie_write(pcie, val, RALINK_PCI_PCIMSK_ADDR);
  337. /* map 2G DDR region */
  338. pcie_port_write(port, PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
  339. PCI_BASE_ADDRESS_0);
  340. /* configure class code and revision ID */
  341. pcie_port_write(port, PCIE_CLASS_CODE | PCIE_REVISION_ID,
  342. RALINK_PCI_CLASS);
  343. /* configure RC FTS number to 250 when it leaves L0s */
  344. val = read_config(pcie, slot, PCIE_FTS_NUM);
  345. val &= ~PCIE_FTS_NUM_MASK;
  346. val |= PCIE_FTS_NUM_L0(0x50);
  347. write_config(pcie, slot, PCIE_FTS_NUM, val);
  348. }
  349. static int mt7621_pcie_enable_ports(struct pci_host_bridge *host)
  350. {
  351. struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
  352. struct device *dev = pcie->dev;
  353. struct mt7621_pcie_port *port;
  354. struct resource_entry *entry;
  355. int err;
  356. entry = resource_list_first_type(&host->windows, IORESOURCE_IO);
  357. if (!entry) {
  358. dev_err(dev, "cannot get io resource\n");
  359. return -EINVAL;
  360. }
  361. /* Setup MEMWIN and IOWIN */
  362. pcie_write(pcie, 0xffffffff, RALINK_PCI_MEMBASE);
  363. pcie_write(pcie, entry->res->start - entry->offset, RALINK_PCI_IOBASE);
  364. list_for_each_entry(port, &pcie->ports, list) {
  365. if (port->enabled) {
  366. err = clk_prepare_enable(port->clk);
  367. if (err) {
  368. dev_err(dev, "enabling clk pcie%d\n",
  369. port->slot);
  370. return err;
  371. }
  372. mt7621_pcie_enable_port(port);
  373. dev_info(dev, "PCIE%d enabled\n", port->slot);
  374. }
  375. }
  376. return 0;
  377. }
  378. static int mt7621_pcie_register_host(struct pci_host_bridge *host)
  379. {
  380. struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
  381. host->ops = &mt7621_pcie_ops;
  382. host->sysdata = pcie;
  383. return pci_host_probe(host);
  384. }
  385. static const struct soc_device_attribute mt7621_pcie_quirks_match[] = {
  386. { .soc_id = "mt7621", .revision = "E2" },
  387. { /* sentinel */ }
  388. };
  389. static int mt7621_pcie_probe(struct platform_device *pdev)
  390. {
  391. struct device *dev = &pdev->dev;
  392. const struct soc_device_attribute *attr;
  393. struct mt7621_pcie_port *port;
  394. struct mt7621_pcie *pcie;
  395. struct pci_host_bridge *bridge;
  396. int err;
  397. if (!dev->of_node)
  398. return -ENODEV;
  399. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
  400. if (!bridge)
  401. return -ENOMEM;
  402. pcie = pci_host_bridge_priv(bridge);
  403. pcie->dev = dev;
  404. platform_set_drvdata(pdev, pcie);
  405. INIT_LIST_HEAD(&pcie->ports);
  406. attr = soc_device_match(mt7621_pcie_quirks_match);
  407. if (attr)
  408. pcie->resets_inverted = true;
  409. err = mt7621_pcie_parse_dt(pcie);
  410. if (err) {
  411. dev_err(dev, "parsing DT failed\n");
  412. return err;
  413. }
  414. err = mt7621_pcie_init_ports(pcie);
  415. if (err) {
  416. dev_err(dev, "nothing connected in virtual bridges\n");
  417. return 0;
  418. }
  419. err = mt7621_pcie_enable_ports(bridge);
  420. if (err) {
  421. dev_err(dev, "error enabling pcie ports\n");
  422. goto remove_resets;
  423. }
  424. return mt7621_pcie_register_host(bridge);
  425. remove_resets:
  426. list_for_each_entry(port, &pcie->ports, list)
  427. reset_control_put(port->pcie_rst);
  428. return err;
  429. }
  430. static int mt7621_pcie_remove(struct platform_device *pdev)
  431. {
  432. struct mt7621_pcie *pcie = platform_get_drvdata(pdev);
  433. struct mt7621_pcie_port *port;
  434. list_for_each_entry(port, &pcie->ports, list)
  435. reset_control_put(port->pcie_rst);
  436. return 0;
  437. }
  438. static const struct of_device_id mt7621_pcie_ids[] = {
  439. { .compatible = "mediatek,mt7621-pci" },
  440. {},
  441. };
  442. MODULE_DEVICE_TABLE(of, mt7621_pcie_ids);
  443. static struct platform_driver mt7621_pcie_driver = {
  444. .probe = mt7621_pcie_probe,
  445. .remove = mt7621_pcie_remove,
  446. .driver = {
  447. .name = "mt7621-pci",
  448. .of_match_table = mt7621_pcie_ids,
  449. },
  450. };
  451. builtin_platform_driver(mt7621_pcie_driver);
  452. MODULE_LICENSE("GPL v2");