pci-mt7621.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /**************************************************************************
  3. *
  4. * BRIEF MODULE DESCRIPTION
  5. * PCI init for Ralink RT2880 solution
  6. *
  7. * Copyright 2007 Ralink Inc. (bruce_chang@ralinktech.com.tw)
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  15. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  17. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  20. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 675 Mass Ave, Cambridge, MA 02139, USA.
  28. *
  29. *
  30. **************************************************************************
  31. * May 2007 Bruce Chang
  32. * Initial Release
  33. *
  34. * May 2009 Bruce Chang
  35. * support RT2880/RT3883 PCIe
  36. *
  37. * May 2011 Bruce Chang
  38. * support RT6855/MT7620 PCIe
  39. *
  40. **************************************************************************
  41. */
  42. #include <linux/bitops.h>
  43. #include <linux/clk.h>
  44. #include <linux/delay.h>
  45. #include <linux/module.h>
  46. #include <linux/of.h>
  47. #include <linux/of_address.h>
  48. #include <linux/of_irq.h>
  49. #include <linux/of_pci.h>
  50. #include <linux/of_platform.h>
  51. #include <linux/pci.h>
  52. #include <linux/platform_device.h>
  53. #include <linux/reset.h>
  54. #include <mt7621.h>
  55. #include <ralink_regs.h>
  56. #include "../../pci/pci.h"
  57. /*
  58. * These functions and structures provide the BIOS scan and mapping of the PCI
  59. * devices.
  60. */
  61. #define RALINK_PCIE0_CLK_EN BIT(24)
  62. #define RALINK_PCIE1_CLK_EN BIT(25)
  63. #define RALINK_PCIE2_CLK_EN BIT(26)
  64. #define RALINK_PCI_CONFIG_ADDR 0x20
  65. #define RALINK_PCI_CONFIG_DATA 0x24
  66. #define RALINK_PCI_MEMBASE 0x28
  67. #define RALINK_PCI_IOBASE 0x2C
  68. #define RALINK_PCIE0_RST BIT(24)
  69. #define RALINK_PCIE1_RST BIT(25)
  70. #define RALINK_PCIE2_RST BIT(26)
  71. #define RALINK_PCI_PCICFG_ADDR 0x0000
  72. #define RALINK_PCI_PCIMSK_ADDR 0x000C
  73. #define RT6855_PCIE0_OFFSET 0x2000
  74. #define RT6855_PCIE1_OFFSET 0x3000
  75. #define RT6855_PCIE2_OFFSET 0x4000
  76. #define RALINK_PCI_BAR0SETUP_ADDR 0x0010
  77. #define RALINK_PCI_IMBASEBAR0_ADDR 0x0018
  78. #define RALINK_PCI_ID 0x0030
  79. #define RALINK_PCI_CLASS 0x0034
  80. #define RALINK_PCI_SUBID 0x0038
  81. #define RALINK_PCI_STATUS 0x0050
  82. #define RALINK_PCIEPHY_P0P1_CTL_OFFSET 0x9000
  83. #define RALINK_PCIEPHY_P2_CTL_OFFSET 0xA000
  84. #define RALINK_PCI_MM_MAP_BASE 0x60000000
  85. #define RALINK_PCI_IO_MAP_BASE 0x1e160000
  86. #define ASSERT_SYSRST_PCIE(val) \
  87. do { \
  88. if (rt_sysc_r32(SYSC_REG_CHIP_REV) == 0x00030101) \
  89. rt_sysc_m32(0, val, RALINK_RSTCTRL); \
  90. else \
  91. rt_sysc_m32(val, 0, RALINK_RSTCTRL); \
  92. } while (0)
  93. #define DEASSERT_SYSRST_PCIE(val) \
  94. do { \
  95. if (rt_sysc_r32(SYSC_REG_CHIP_REV) == 0x00030101) \
  96. rt_sysc_m32(val, 0, RALINK_RSTCTRL); \
  97. else \
  98. rt_sysc_m32(0, val, RALINK_RSTCTRL); \
  99. } while (0)
  100. #define RALINK_CLKCFG1 0x30
  101. #define RALINK_RSTCTRL 0x34
  102. #define RALINK_GPIOMODE 0x60
  103. #define RALINK_PCIE_CLK_GEN 0x7c
  104. #define RALINK_PCIE_CLK_GEN1 0x80
  105. //RALINK_RSTCTRL bit
  106. #define RALINK_PCIE_RST BIT(23)
  107. #define RALINK_PCI_RST BIT(24)
  108. //RALINK_CLKCFG1 bit
  109. #define RALINK_PCI_CLK_EN BIT(19)
  110. #define RALINK_PCIE_CLK_EN BIT(21)
  111. #define MEMORY_BASE 0x0
  112. static int pcie_link_status = 0;
  113. /**
  114. * struct mt7621_pcie_port - PCIe port information
  115. * @base: IO mapped register base
  116. * @list: port list
  117. * @pcie: pointer to PCIe host info
  118. * @reset: pointer to port reset control
  119. */
  120. struct mt7621_pcie_port {
  121. void __iomem *base;
  122. struct list_head list;
  123. struct mt7621_pcie *pcie;
  124. struct reset_control *reset;
  125. };
  126. /**
  127. * struct mt7621_pcie - PCIe host information
  128. * @base: IO Mapped Register Base
  129. * @io: IO resource
  130. * @mem: non-prefetchable memory resource
  131. * @busn: bus range
  132. * @offset: IO / Memory offset
  133. * @dev: Pointer to PCIe device
  134. * @ports: pointer to PCIe port information
  135. */
  136. struct mt7621_pcie {
  137. void __iomem *base;
  138. struct device *dev;
  139. struct resource io;
  140. struct resource mem;
  141. struct resource busn;
  142. struct {
  143. resource_size_t mem;
  144. resource_size_t io;
  145. } offset;
  146. struct list_head ports;
  147. };
  148. static inline u32 pcie_read(struct mt7621_pcie *pcie, u32 reg)
  149. {
  150. return readl(pcie->base + reg);
  151. }
  152. static inline void pcie_write(struct mt7621_pcie *pcie, u32 val, u32 reg)
  153. {
  154. writel(val, pcie->base + reg);
  155. }
  156. static inline u32 mt7621_pci_get_cfgaddr(unsigned int bus, unsigned int slot,
  157. unsigned int func, unsigned int where)
  158. {
  159. return (((where & 0xF00) >> 8) << 24) | (bus << 16) | (slot << 11) |
  160. (func << 8) | (where & 0xfc) | 0x80000000;
  161. }
  162. static void __iomem *mt7621_pcie_map_bus(struct pci_bus *bus,
  163. unsigned int devfn, int where)
  164. {
  165. struct mt7621_pcie *pcie = bus->sysdata;
  166. u32 address = mt7621_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
  167. PCI_FUNC(devfn), where);
  168. writel(address, pcie->base + RALINK_PCI_CONFIG_ADDR);
  169. return pcie->base + RALINK_PCI_CONFIG_DATA + (where & 3);
  170. }
  171. struct pci_ops mt7621_pci_ops = {
  172. .map_bus = mt7621_pcie_map_bus,
  173. .read = pci_generic_config_read,
  174. .write = pci_generic_config_write,
  175. };
  176. static u32
  177. read_config(struct mt7621_pcie *pcie, unsigned int dev, u32 reg)
  178. {
  179. u32 address = mt7621_pci_get_cfgaddr(0, dev, 0, reg);
  180. pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
  181. return pcie_read(pcie, RALINK_PCI_CONFIG_DATA);
  182. }
  183. static void
  184. write_config(struct mt7621_pcie *pcie, unsigned int dev, u32 reg, u32 val)
  185. {
  186. u32 address = mt7621_pci_get_cfgaddr(0, dev, 0, reg);
  187. pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
  188. pcie_write(pcie, val, RALINK_PCI_CONFIG_DATA);
  189. }
  190. void
  191. set_pcie_phy(struct mt7621_pcie *pcie, u32 offset,
  192. int start_b, int bits, int val)
  193. {
  194. u32 reg = pcie_read(pcie, offset);
  195. reg &= ~(((1 << bits) - 1) << start_b);
  196. reg |= val << start_b;
  197. pcie_write(pcie, reg, offset);
  198. }
  199. void
  200. bypass_pipe_rst(struct mt7621_pcie *pcie)
  201. {
  202. /* PCIe Port 0 */
  203. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x02c), 12, 1, 0x01); // rg_pe1_pipe_rst_b
  204. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x02c), 4, 1, 0x01); // rg_pe1_pipe_cmd_frc[4]
  205. /* PCIe Port 1 */
  206. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x12c), 12, 1, 0x01); // rg_pe1_pipe_rst_b
  207. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x12c), 4, 1, 0x01); // rg_pe1_pipe_cmd_frc[4]
  208. /* PCIe Port 2 */
  209. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x02c), 12, 1, 0x01); // rg_pe1_pipe_rst_b
  210. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x02c), 4, 1, 0x01); // rg_pe1_pipe_cmd_frc[4]
  211. }
  212. void
  213. set_phy_for_ssc(struct mt7621_pcie *pcie)
  214. {
  215. unsigned long reg = rt_sysc_r32(SYSC_REG_SYSTEM_CONFIG0);
  216. reg = (reg >> 6) & 0x7;
  217. /* Set PCIe Port0 & Port1 PHY to disable SSC */
  218. /* Debug Xtal Type */
  219. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x400), 8, 1, 0x01); // rg_pe1_frc_h_xtal_type
  220. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x400), 9, 2, 0x00); // rg_pe1_h_xtal_type
  221. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x000), 4, 1, 0x01); // rg_pe1_frc_phy_en //Force Port 0 enable control
  222. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x100), 4, 1, 0x01); // rg_pe1_frc_phy_en //Force Port 1 enable control
  223. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x000), 5, 1, 0x00); // rg_pe1_phy_en //Port 0 disable
  224. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x100), 5, 1, 0x00); // rg_pe1_phy_en //Port 1 disable
  225. if (reg <= 5 && reg >= 3) { // 40MHz Xtal
  226. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 6, 2, 0x01); // RG_PE1_H_PLL_PREDIV //Pre-divider ratio (for host mode)
  227. printk("***** Xtal 40MHz *****\n");
  228. } else { // 25MHz | 20MHz Xtal
  229. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 6, 2, 0x00); // RG_PE1_H_PLL_PREDIV //Pre-divider ratio (for host mode)
  230. if (reg >= 6) {
  231. printk("***** Xtal 25MHz *****\n");
  232. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4bc), 4, 2, 0x01); // RG_PE1_H_PLL_FBKSEL //Feedback clock select
  233. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x49c), 0, 31, 0x18000000); // RG_PE1_H_LCDDS_PCW_NCPO //DDS NCPO PCW (for host mode)
  234. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4a4), 0, 16, 0x18d); // RG_PE1_H_LCDDS_SSC_PRD //DDS SSC dither period control
  235. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4a8), 0, 12, 0x4a); // RG_PE1_H_LCDDS_SSC_DELTA //DDS SSC dither amplitude control
  236. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4a8), 16, 12, 0x4a); // RG_PE1_H_LCDDS_SSC_DELTA1 //DDS SSC dither amplitude control for initial
  237. } else {
  238. printk("***** Xtal 20MHz *****\n");
  239. }
  240. }
  241. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4a0), 5, 1, 0x01); // RG_PE1_LCDDS_CLK_PH_INV //DDS clock inversion
  242. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 22, 2, 0x02); // RG_PE1_H_PLL_BC
  243. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 18, 4, 0x06); // RG_PE1_H_PLL_BP
  244. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 12, 4, 0x02); // RG_PE1_H_PLL_IR
  245. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 8, 4, 0x01); // RG_PE1_H_PLL_IC
  246. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4ac), 16, 3, 0x00); // RG_PE1_H_PLL_BR
  247. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 1, 3, 0x02); // RG_PE1_PLL_DIVEN
  248. if (reg <= 5 && reg >= 3) { // 40MHz Xtal
  249. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x414), 6, 2, 0x01); // rg_pe1_mstckdiv //value of da_pe1_mstckdiv when force mode enable
  250. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x414), 5, 1, 0x01); // rg_pe1_frc_mstckdiv //force mode enable of da_pe1_mstckdiv
  251. }
  252. /* Enable PHY and disable force mode */
  253. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x000), 5, 1, 0x01); // rg_pe1_phy_en //Port 0 enable
  254. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x100), 5, 1, 0x01); // rg_pe1_phy_en //Port 1 enable
  255. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x000), 4, 1, 0x00); // rg_pe1_frc_phy_en //Force Port 0 disable control
  256. set_pcie_phy(pcie, (RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x100), 4, 1, 0x00); // rg_pe1_frc_phy_en //Force Port 1 disable control
  257. /* Set PCIe Port2 PHY to disable SSC */
  258. /* Debug Xtal Type */
  259. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x400), 8, 1, 0x01); // rg_pe1_frc_h_xtal_type
  260. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x400), 9, 2, 0x00); // rg_pe1_h_xtal_type
  261. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x000), 4, 1, 0x01); // rg_pe1_frc_phy_en //Force Port 0 enable control
  262. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x000), 5, 1, 0x00); // rg_pe1_phy_en //Port 0 disable
  263. if (reg <= 5 && reg >= 3) { // 40MHz Xtal
  264. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 6, 2, 0x01); // RG_PE1_H_PLL_PREDIV //Pre-divider ratio (for host mode)
  265. } else { // 25MHz | 20MHz Xtal
  266. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 6, 2, 0x00); // RG_PE1_H_PLL_PREDIV //Pre-divider ratio (for host mode)
  267. if (reg >= 6) { // 25MHz Xtal
  268. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4bc), 4, 2, 0x01); // RG_PE1_H_PLL_FBKSEL //Feedback clock select
  269. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x49c), 0, 31, 0x18000000); // RG_PE1_H_LCDDS_PCW_NCPO //DDS NCPO PCW (for host mode)
  270. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4a4), 0, 16, 0x18d); // RG_PE1_H_LCDDS_SSC_PRD //DDS SSC dither period control
  271. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4a8), 0, 12, 0x4a); // RG_PE1_H_LCDDS_SSC_DELTA //DDS SSC dither amplitude control
  272. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4a8), 16, 12, 0x4a); // RG_PE1_H_LCDDS_SSC_DELTA1 //DDS SSC dither amplitude control for initial
  273. }
  274. }
  275. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4a0), 5, 1, 0x01); // RG_PE1_LCDDS_CLK_PH_INV //DDS clock inversion
  276. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 22, 2, 0x02); // RG_PE1_H_PLL_BC
  277. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 18, 4, 0x06); // RG_PE1_H_PLL_BP
  278. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 12, 4, 0x02); // RG_PE1_H_PLL_IR
  279. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 8, 4, 0x01); // RG_PE1_H_PLL_IC
  280. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4ac), 16, 3, 0x00); // RG_PE1_H_PLL_BR
  281. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 1, 3, 0x02); // RG_PE1_PLL_DIVEN
  282. if (reg <= 5 && reg >= 3) { // 40MHz Xtal
  283. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x414), 6, 2, 0x01); // rg_pe1_mstckdiv //value of da_pe1_mstckdiv when force mode enable
  284. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x414), 5, 1, 0x01); // rg_pe1_frc_mstckdiv //force mode enable of da_pe1_mstckdiv
  285. }
  286. /* Enable PHY and disable force mode */
  287. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x000), 5, 1, 0x01); // rg_pe1_phy_en //Port 0 enable
  288. set_pcie_phy(pcie, (RALINK_PCIEPHY_P2_CTL_OFFSET + 0x000), 4, 1, 0x00); // rg_pe1_frc_phy_en //Force Port 0 disable control
  289. }
  290. static void setup_cm_memory_region(struct resource *mem_resource)
  291. {
  292. resource_size_t mask;
  293. if (mips_cps_numiocu(0)) {
  294. /* FIXME: hardware doesn't accept mask values with 1s after
  295. * 0s (e.g. 0xffef), so it would be great to warn if that's
  296. * about to happen */
  297. mask = ~(mem_resource->end - mem_resource->start);
  298. write_gcr_reg1_base(mem_resource->start);
  299. write_gcr_reg1_mask(mask | CM_GCR_REGn_MASK_CMTGT_IOCU0);
  300. printk("PCI coherence region base: 0x%08llx, mask/settings: 0x%08llx\n",
  301. (unsigned long long)read_gcr_reg1_base(),
  302. (unsigned long long)read_gcr_reg1_mask());
  303. }
  304. }
  305. static int mt7621_pci_parse_request_of_pci_ranges(struct mt7621_pcie *pcie)
  306. {
  307. struct device *dev = pcie->dev;
  308. struct device_node *node = dev->of_node;
  309. struct of_pci_range_parser parser;
  310. struct of_pci_range range;
  311. int err;
  312. if (of_pci_range_parser_init(&parser, node)) {
  313. dev_err(dev, "missing \"ranges\" property\n");
  314. return -EINVAL;
  315. }
  316. for_each_of_pci_range(&parser, &range) {
  317. struct resource *res = NULL;
  318. switch (range.flags & IORESOURCE_TYPE_BITS) {
  319. case IORESOURCE_IO:
  320. ioremap(range.cpu_addr, range.size);
  321. res = &pcie->io;
  322. pcie->offset.io = 0x00000000UL;
  323. break;
  324. case IORESOURCE_MEM:
  325. res = &pcie->mem;
  326. pcie->offset.mem = 0x00000000UL;
  327. break;
  328. }
  329. if (res != NULL)
  330. of_pci_range_to_resource(&range, node, res);
  331. }
  332. err = of_pci_parse_bus_range(node, &pcie->busn);
  333. if (err < 0) {
  334. dev_err(dev, "failed to parse bus ranges property: %d\n", err);
  335. pcie->busn.name = node->name;
  336. pcie->busn.start = 0;
  337. pcie->busn.end = 0xff;
  338. pcie->busn.flags = IORESOURCE_BUS;
  339. }
  340. return 0;
  341. }
  342. static int mt7621_pcie_parse_dt(struct mt7621_pcie *pcie)
  343. {
  344. struct device *dev = pcie->dev;
  345. struct device_node *node = dev->of_node;
  346. struct resource regs;
  347. const char *type;
  348. int err;
  349. type = of_get_property(node, "device_type", NULL);
  350. if (!type || strcmp(type, "pci") != 0) {
  351. dev_err(dev, "invalid \"device_type\" %s\n", type);
  352. return -EINVAL;
  353. }
  354. err = of_address_to_resource(node, 0, &regs);
  355. if (err) {
  356. dev_err(dev, "missing \"reg\" property\n");
  357. return err;
  358. }
  359. pcie->base = devm_ioremap_resource(dev, &regs);
  360. if (IS_ERR(pcie->base))
  361. return PTR_ERR(pcie->base);
  362. return 0;
  363. }
  364. static int mt7621_pcie_request_resources(struct mt7621_pcie *pcie,
  365. struct list_head *res)
  366. {
  367. struct device *dev = pcie->dev;
  368. int err;
  369. pci_add_resource_offset(res, &pcie->io, pcie->offset.io);
  370. pci_add_resource_offset(res, &pcie->mem, pcie->offset.mem);
  371. pci_add_resource(res, &pcie->busn);
  372. err = devm_request_pci_bus_resources(dev, res);
  373. if (err < 0)
  374. return err;
  375. return 0;
  376. }
  377. static int mt7621_pcie_register_host(struct pci_host_bridge *host,
  378. struct list_head *res)
  379. {
  380. struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
  381. list_splice_init(res, &host->windows);
  382. host->busnr = pcie->busn.start;
  383. host->dev.parent = pcie->dev;
  384. host->ops = &mt7621_pci_ops;
  385. host->map_irq = of_irq_parse_and_map_pci;
  386. host->swizzle_irq = pci_common_swizzle;
  387. host->sysdata = pcie;
  388. return pci_host_probe(host);
  389. }
  390. static int mt7621_pci_probe(struct platform_device *pdev)
  391. {
  392. struct device *dev = &pdev->dev;
  393. struct mt7621_pcie *pcie;
  394. struct pci_host_bridge *bridge;
  395. int err;
  396. u32 val = 0;
  397. LIST_HEAD(res);
  398. if (!dev->of_node)
  399. return -ENODEV;
  400. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
  401. if (!bridge)
  402. return -ENODEV;
  403. pcie = pci_host_bridge_priv(bridge);
  404. pcie->dev = dev;
  405. platform_set_drvdata(pdev, pcie);
  406. INIT_LIST_HEAD(&pcie->ports);
  407. err = mt7621_pcie_parse_dt(pcie);
  408. if (err) {
  409. dev_err(dev, "Parsing DT failed\n");
  410. return err;
  411. }
  412. /* set resources limits */
  413. iomem_resource.start = 0;
  414. iomem_resource.end = ~0UL; /* no limit */
  415. ioport_resource.start = 0;
  416. ioport_resource.end = ~0UL; /* no limit */
  417. val = RALINK_PCIE0_RST;
  418. val |= RALINK_PCIE1_RST;
  419. val |= RALINK_PCIE2_RST;
  420. ASSERT_SYSRST_PCIE(RALINK_PCIE0_RST | RALINK_PCIE1_RST | RALINK_PCIE2_RST);
  421. *(unsigned int *)(0xbe000060) &= ~(0x3<<10 | 0x3<<3);
  422. *(unsigned int *)(0xbe000060) |= 0x1<<10 | 0x1<<3;
  423. mdelay(100);
  424. *(unsigned int *)(0xbe000600) |= 0x1<<19 | 0x1<<8 | 0x1<<7; // use GPIO19/GPIO8/GPIO7 (PERST_N/UART_RXD3/UART_TXD3)
  425. mdelay(100);
  426. *(unsigned int *)(0xbe000620) &= ~(0x1<<19 | 0x1<<8 | 0x1<<7); // clear DATA
  427. mdelay(100);
  428. val = RALINK_PCIE0_RST;
  429. val |= RALINK_PCIE1_RST;
  430. val |= RALINK_PCIE2_RST;
  431. DEASSERT_SYSRST_PCIE(val);
  432. if ((*(unsigned int *)(0xbe00000c)&0xFFFF) == 0x0101) // MT7621 E2
  433. bypass_pipe_rst(pcie);
  434. set_phy_for_ssc(pcie);
  435. val = read_config(pcie, 0, 0x70c);
  436. printk("Port 0 N_FTS = %x\n", (unsigned int)val);
  437. val = read_config(pcie, 1, 0x70c);
  438. printk("Port 1 N_FTS = %x\n", (unsigned int)val);
  439. val = read_config(pcie, 2, 0x70c);
  440. printk("Port 2 N_FTS = %x\n", (unsigned int)val);
  441. rt_sysc_m32(0, RALINK_PCIE_RST, RALINK_RSTCTRL);
  442. rt_sysc_m32(0x30, 2 << 4, SYSC_REG_SYSTEM_CONFIG1);
  443. rt_sysc_m32(0x80000000, 0, RALINK_PCIE_CLK_GEN);
  444. rt_sysc_m32(0x7f000000, 0xa << 24, RALINK_PCIE_CLK_GEN1);
  445. rt_sysc_m32(0, 0x80000000, RALINK_PCIE_CLK_GEN);
  446. mdelay(50);
  447. rt_sysc_m32(RALINK_PCIE_RST, 0, RALINK_RSTCTRL);
  448. /* Use GPIO control instead of PERST_N */
  449. *(unsigned int *)(0xbe000620) |= 0x1<<19 | 0x1<<8 | 0x1<<7; // set DATA
  450. mdelay(1000);
  451. if ((pcie_read(pcie, RT6855_PCIE0_OFFSET + RALINK_PCI_STATUS) & 0x1) == 0) {
  452. printk("PCIE0 no card, disable it(RST&CLK)\n");
  453. ASSERT_SYSRST_PCIE(RALINK_PCIE0_RST);
  454. rt_sysc_m32(RALINK_PCIE0_CLK_EN, 0, RALINK_CLKCFG1);
  455. pcie_link_status &= ~(1<<0);
  456. } else {
  457. pcie_link_status |= 1<<0;
  458. val = pcie_read(pcie, RALINK_PCI_PCIMSK_ADDR);
  459. val |= (1<<20); // enable pcie1 interrupt
  460. pcie_write(pcie, val, RALINK_PCI_PCIMSK_ADDR);
  461. }
  462. if ((pcie_read(pcie, RT6855_PCIE1_OFFSET + RALINK_PCI_STATUS) & 0x1) == 0) {
  463. printk("PCIE1 no card, disable it(RST&CLK)\n");
  464. ASSERT_SYSRST_PCIE(RALINK_PCIE1_RST);
  465. rt_sysc_m32(RALINK_PCIE1_CLK_EN, 0, RALINK_CLKCFG1);
  466. pcie_link_status &= ~(1<<1);
  467. } else {
  468. pcie_link_status |= 1<<1;
  469. val = pcie_read(pcie, RALINK_PCI_PCIMSK_ADDR);
  470. val |= (1<<21); // enable pcie1 interrupt
  471. pcie_write(pcie, val, RALINK_PCI_PCIMSK_ADDR);
  472. }
  473. if ((pcie_read(pcie, RT6855_PCIE2_OFFSET + RALINK_PCI_STATUS) & 0x1) == 0) {
  474. printk("PCIE2 no card, disable it(RST&CLK)\n");
  475. ASSERT_SYSRST_PCIE(RALINK_PCIE2_RST);
  476. rt_sysc_m32(RALINK_PCIE2_CLK_EN, 0, RALINK_CLKCFG1);
  477. pcie_link_status &= ~(1<<2);
  478. } else {
  479. pcie_link_status |= 1<<2;
  480. val = pcie_read(pcie, RALINK_PCI_PCIMSK_ADDR);
  481. val |= (1<<22); // enable pcie2 interrupt
  482. pcie_write(pcie, val, RALINK_PCI_PCIMSK_ADDR);
  483. }
  484. if (pcie_link_status == 0)
  485. return 0;
  486. /*
  487. pcie(2/1/0) link status pcie2_num pcie1_num pcie0_num
  488. 3'b000 x x x
  489. 3'b001 x x 0
  490. 3'b010 x 0 x
  491. 3'b011 x 1 0
  492. 3'b100 0 x x
  493. 3'b101 1 x 0
  494. 3'b110 1 0 x
  495. 3'b111 2 1 0
  496. */
  497. switch (pcie_link_status) {
  498. case 2:
  499. val = pcie_read(pcie, RALINK_PCI_PCICFG_ADDR);
  500. val &= ~0x00ff0000;
  501. val |= 0x1 << 16; // port 0
  502. val |= 0x0 << 20; // port 1
  503. pcie_write(pcie, val, RALINK_PCI_PCICFG_ADDR);
  504. break;
  505. case 4:
  506. val = pcie_read(pcie, RALINK_PCI_PCICFG_ADDR);
  507. val &= ~0x0fff0000;
  508. val |= 0x1 << 16; //port0
  509. val |= 0x2 << 20; //port1
  510. val |= 0x0 << 24; //port2
  511. pcie_write(pcie, val, RALINK_PCI_PCICFG_ADDR);
  512. break;
  513. case 5:
  514. val = pcie_read(pcie, RALINK_PCI_PCICFG_ADDR);
  515. val &= ~0x0fff0000;
  516. val |= 0x0 << 16; //port0
  517. val |= 0x2 << 20; //port1
  518. val |= 0x1 << 24; //port2
  519. pcie_write(pcie, val, RALINK_PCI_PCICFG_ADDR);
  520. break;
  521. case 6:
  522. val = pcie_read(pcie, RALINK_PCI_PCICFG_ADDR);
  523. val &= ~0x0fff0000;
  524. val |= 0x2 << 16; //port0
  525. val |= 0x0 << 20; //port1
  526. val |= 0x1 << 24; //port2
  527. pcie_write(pcie, val, RALINK_PCI_PCICFG_ADDR);
  528. break;
  529. }
  530. /*
  531. ioport_resource.start = mt7621_res_pci_io1.start;
  532. ioport_resource.end = mt7621_res_pci_io1.end;
  533. */
  534. pcie_write(pcie, 0xffffffff, RALINK_PCI_MEMBASE);
  535. pcie_write(pcie, RALINK_PCI_IO_MAP_BASE, RALINK_PCI_IOBASE);
  536. //PCIe0
  537. if ((pcie_link_status & 0x1) != 0) {
  538. /* open 7FFF:2G; ENABLE */
  539. pcie_write(pcie, 0x7FFF0001,
  540. RT6855_PCIE0_OFFSET + RALINK_PCI_BAR0SETUP_ADDR);
  541. pcie_write(pcie, MEMORY_BASE,
  542. RT6855_PCIE0_OFFSET + RALINK_PCI_IMBASEBAR0_ADDR);
  543. pcie_write(pcie, 0x06040001,
  544. RT6855_PCIE0_OFFSET + RALINK_PCI_CLASS);
  545. printk("PCIE0 enabled\n");
  546. }
  547. //PCIe1
  548. if ((pcie_link_status & 0x2) != 0) {
  549. /* open 7FFF:2G; ENABLE */
  550. pcie_write(pcie, 0x7FFF0001,
  551. RT6855_PCIE1_OFFSET + RALINK_PCI_BAR0SETUP_ADDR);
  552. pcie_write(pcie, MEMORY_BASE,
  553. RT6855_PCIE1_OFFSET + RALINK_PCI_IMBASEBAR0_ADDR);
  554. pcie_write(pcie, 0x06040001,
  555. RT6855_PCIE1_OFFSET + RALINK_PCI_CLASS);
  556. printk("PCIE1 enabled\n");
  557. }
  558. //PCIe2
  559. if ((pcie_link_status & 0x4) != 0) {
  560. /* open 7FFF:2G; ENABLE */
  561. pcie_write(pcie, 0x7FFF0001,
  562. RT6855_PCIE2_OFFSET + RALINK_PCI_BAR0SETUP_ADDR);
  563. pcie_write(pcie, MEMORY_BASE,
  564. RT6855_PCIE2_OFFSET + RALINK_PCI_IMBASEBAR0_ADDR);
  565. pcie_write(pcie, 0x06040001,
  566. RT6855_PCIE2_OFFSET + RALINK_PCI_CLASS);
  567. printk("PCIE2 enabled\n");
  568. }
  569. switch (pcie_link_status) {
  570. case 7:
  571. val = read_config(pcie, 2, 0x4);
  572. write_config(pcie, 2, 0x4, val|0x4);
  573. val = read_config(pcie, 2, 0x70c);
  574. val &= ~(0xff)<<8;
  575. val |= 0x50<<8;
  576. write_config(pcie, 2, 0x70c, val);
  577. case 3:
  578. case 5:
  579. case 6:
  580. val = read_config(pcie, 1, 0x4);
  581. write_config(pcie, 1, 0x4, val|0x4);
  582. val = read_config(pcie, 1, 0x70c);
  583. val &= ~(0xff)<<8;
  584. val |= 0x50<<8;
  585. write_config(pcie, 1, 0x70c, val);
  586. default:
  587. val = read_config(pcie, 0, 0x4);
  588. write_config(pcie, 0, 0x4, val|0x4); //bus master enable
  589. val = read_config(pcie, 0, 0x70c);
  590. val &= ~(0xff)<<8;
  591. val |= 0x50<<8;
  592. write_config(pcie, 0, 0x70c, val);
  593. }
  594. err = mt7621_pci_parse_request_of_pci_ranges(pcie);
  595. if (err) {
  596. dev_err(dev, "Error requesting pci resources from ranges");
  597. return err;
  598. }
  599. setup_cm_memory_region(&pcie->mem);
  600. err = mt7621_pcie_request_resources(pcie, &res);
  601. if (err) {
  602. dev_err(dev, "Error requesting resources\n");
  603. return err;
  604. }
  605. err = mt7621_pcie_register_host(bridge, &res);
  606. if (err) {
  607. dev_err(dev, "Error registering host\n");
  608. return err;
  609. }
  610. return 0;
  611. }
  612. static const struct of_device_id mt7621_pci_ids[] = {
  613. { .compatible = "mediatek,mt7621-pci" },
  614. {},
  615. };
  616. MODULE_DEVICE_TABLE(of, mt7621_pci_ids);
  617. static struct platform_driver mt7621_pci_driver = {
  618. .probe = mt7621_pci_probe,
  619. .driver = {
  620. .name = "mt7621-pci",
  621. .of_match_table = of_match_ptr(mt7621_pci_ids),
  622. },
  623. };
  624. static int __init mt7621_pci_init(void)
  625. {
  626. return platform_driver_register(&mt7621_pci_driver);
  627. }
  628. arch_initcall(mt7621_pci_init);