pcie-mt7621.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. (bruce_chang@ralinktech.com.tw)
  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[11];
  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;
  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_scoped(node, child) {
  227. int slot;
  228. err = of_pci_get_devfn(child);
  229. if (err < 0)
  230. return dev_err_probe(dev, err, "failed to parse devfn\n");
  231. slot = PCI_SLOT(err);
  232. err = mt7621_pcie_parse_port(pcie, child, slot);
  233. if (err)
  234. return err;
  235. }
  236. return 0;
  237. }
  238. static int mt7621_pcie_init_port(struct mt7621_pcie_port *port)
  239. {
  240. struct mt7621_pcie *pcie = port->pcie;
  241. struct device *dev = pcie->dev;
  242. u32 slot = port->slot;
  243. int err;
  244. err = phy_init(port->phy);
  245. if (err) {
  246. dev_err(dev, "failed to initialize port%d phy\n", slot);
  247. return err;
  248. }
  249. err = phy_power_on(port->phy);
  250. if (err) {
  251. dev_err(dev, "failed to power on port%d phy\n", slot);
  252. phy_exit(port->phy);
  253. return err;
  254. }
  255. port->enabled = true;
  256. return 0;
  257. }
  258. static void mt7621_pcie_reset_assert(struct mt7621_pcie *pcie)
  259. {
  260. struct mt7621_pcie_port *port;
  261. list_for_each_entry(port, &pcie->ports, list) {
  262. /* PCIe RC reset assert */
  263. mt7621_control_assert(port);
  264. /* PCIe EP reset assert */
  265. mt7621_rst_gpio_pcie_assert(port);
  266. }
  267. msleep(PERST_DELAY_MS);
  268. }
  269. static void mt7621_pcie_reset_rc_deassert(struct mt7621_pcie *pcie)
  270. {
  271. struct mt7621_pcie_port *port;
  272. list_for_each_entry(port, &pcie->ports, list)
  273. mt7621_control_deassert(port);
  274. }
  275. static void mt7621_pcie_reset_ep_deassert(struct mt7621_pcie *pcie)
  276. {
  277. struct mt7621_pcie_port *port;
  278. list_for_each_entry(port, &pcie->ports, list)
  279. mt7621_rst_gpio_pcie_deassert(port);
  280. msleep(PERST_DELAY_MS);
  281. }
  282. static int mt7621_pcie_init_ports(struct mt7621_pcie *pcie)
  283. {
  284. struct device *dev = pcie->dev;
  285. struct mt7621_pcie_port *port, *tmp;
  286. u8 num_disabled = 0;
  287. int err;
  288. mt7621_pcie_reset_assert(pcie);
  289. mt7621_pcie_reset_rc_deassert(pcie);
  290. list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
  291. u32 slot = port->slot;
  292. if (slot == 1) {
  293. port->enabled = true;
  294. continue;
  295. }
  296. err = mt7621_pcie_init_port(port);
  297. if (err) {
  298. dev_err(dev, "initializing port %d failed\n", slot);
  299. list_del(&port->list);
  300. }
  301. }
  302. msleep(INIT_PORTS_DELAY_MS);
  303. mt7621_pcie_reset_ep_deassert(pcie);
  304. tmp = NULL;
  305. list_for_each_entry(port, &pcie->ports, list) {
  306. u32 slot = port->slot;
  307. if (!mt7621_pcie_port_is_linkup(port)) {
  308. dev_info(dev, "pcie%d no card, disable it (RST & CLK)\n",
  309. slot);
  310. mt7621_control_assert(port);
  311. port->enabled = false;
  312. num_disabled++;
  313. if (slot == 0) {
  314. tmp = port;
  315. continue;
  316. }
  317. if (slot == 1 && tmp && !tmp->enabled)
  318. phy_power_off(tmp->phy);
  319. }
  320. }
  321. return (num_disabled != PCIE_PORT_CNT) ? 0 : -ENODEV;
  322. }
  323. static void mt7621_pcie_enable_port(struct mt7621_pcie_port *port)
  324. {
  325. struct mt7621_pcie *pcie = port->pcie;
  326. u32 slot = port->slot;
  327. u32 val;
  328. /* enable pcie interrupt */
  329. val = pcie_read(pcie, RALINK_PCI_PCIMSK_ADDR);
  330. val |= PCIE_PORT_INT_EN(slot);
  331. pcie_write(pcie, val, RALINK_PCI_PCIMSK_ADDR);
  332. /* map 2G DDR region */
  333. pcie_port_write(port, PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
  334. PCI_BASE_ADDRESS_0);
  335. /* configure class code and revision ID */
  336. pcie_port_write(port, PCIE_CLASS_CODE | PCIE_REVISION_ID,
  337. RALINK_PCI_CLASS);
  338. /* configure RC FTS number to 250 when it leaves L0s */
  339. val = read_config(pcie, slot, PCIE_FTS_NUM);
  340. val &= ~PCIE_FTS_NUM_MASK;
  341. val |= PCIE_FTS_NUM_L0(0x50);
  342. write_config(pcie, slot, PCIE_FTS_NUM, val);
  343. }
  344. static int mt7621_pcie_enable_ports(struct pci_host_bridge *host)
  345. {
  346. struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
  347. struct device *dev = pcie->dev;
  348. struct mt7621_pcie_port *port;
  349. struct resource_entry *entry;
  350. int err;
  351. entry = resource_list_first_type(&host->windows, IORESOURCE_IO);
  352. if (!entry) {
  353. dev_err(dev, "cannot get io resource\n");
  354. return -EINVAL;
  355. }
  356. /* Setup MEMWIN and IOWIN */
  357. pcie_write(pcie, 0xffffffff, RALINK_PCI_MEMBASE);
  358. pcie_write(pcie, entry->res->start - entry->offset, RALINK_PCI_IOBASE);
  359. list_for_each_entry(port, &pcie->ports, list) {
  360. if (port->enabled) {
  361. err = clk_prepare_enable(port->clk);
  362. if (err) {
  363. dev_err(dev, "enabling clk pcie%d\n",
  364. port->slot);
  365. return err;
  366. }
  367. mt7621_pcie_enable_port(port);
  368. dev_info(dev, "PCIE%d enabled\n", port->slot);
  369. }
  370. }
  371. return 0;
  372. }
  373. static int mt7621_pcie_register_host(struct pci_host_bridge *host)
  374. {
  375. struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
  376. host->ops = &mt7621_pcie_ops;
  377. host->sysdata = pcie;
  378. return pci_host_probe(host);
  379. }
  380. static const struct soc_device_attribute mt7621_pcie_quirks_match[] = {
  381. { .soc_id = "mt7621", .revision = "E2" },
  382. { /* sentinel */ }
  383. };
  384. static int mt7621_pcie_probe(struct platform_device *pdev)
  385. {
  386. struct device *dev = &pdev->dev;
  387. const struct soc_device_attribute *attr;
  388. struct mt7621_pcie_port *port;
  389. struct mt7621_pcie *pcie;
  390. struct pci_host_bridge *bridge;
  391. int err;
  392. if (!dev->of_node)
  393. return -ENODEV;
  394. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
  395. if (!bridge)
  396. return -ENOMEM;
  397. pcie = pci_host_bridge_priv(bridge);
  398. pcie->dev = dev;
  399. platform_set_drvdata(pdev, pcie);
  400. INIT_LIST_HEAD(&pcie->ports);
  401. attr = soc_device_match(mt7621_pcie_quirks_match);
  402. if (attr)
  403. pcie->resets_inverted = true;
  404. err = mt7621_pcie_parse_dt(pcie);
  405. if (err) {
  406. dev_err(dev, "parsing DT failed\n");
  407. return err;
  408. }
  409. err = mt7621_pcie_init_ports(pcie);
  410. if (err) {
  411. dev_err(dev, "nothing connected in virtual bridges\n");
  412. return 0;
  413. }
  414. err = mt7621_pcie_enable_ports(bridge);
  415. if (err) {
  416. dev_err(dev, "error enabling pcie ports\n");
  417. goto remove_resets;
  418. }
  419. return mt7621_pcie_register_host(bridge);
  420. remove_resets:
  421. list_for_each_entry(port, &pcie->ports, list)
  422. reset_control_put(port->pcie_rst);
  423. return err;
  424. }
  425. static void mt7621_pcie_remove(struct platform_device *pdev)
  426. {
  427. struct mt7621_pcie *pcie = platform_get_drvdata(pdev);
  428. struct mt7621_pcie_port *port;
  429. list_for_each_entry(port, &pcie->ports, list)
  430. reset_control_put(port->pcie_rst);
  431. }
  432. static const struct of_device_id mt7621_pcie_ids[] = {
  433. { .compatible = "mediatek,mt7621-pci" },
  434. {},
  435. };
  436. MODULE_DEVICE_TABLE(of, mt7621_pcie_ids);
  437. static struct platform_driver mt7621_pcie_driver = {
  438. .probe = mt7621_pcie_probe,
  439. .remove = mt7621_pcie_remove,
  440. .driver = {
  441. .name = "mt7621-pci",
  442. .of_match_table = mt7621_pcie_ids,
  443. },
  444. };
  445. builtin_platform_driver(mt7621_pcie_driver);
  446. MODULE_DESCRIPTION("MediaTek MT7621 PCIe host controller driver");
  447. MODULE_LICENSE("GPL v2");