0016-MIPS-ralink-add-MT7621-pcie-driver.patch 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. From 95d7eb13a864ef666cea7f0e86349e86d80d28ce Mon Sep 17 00:00:00 2001
  2. From: John Crispin <blogic@openwrt.org>
  3. Date: Sun, 16 Mar 2014 05:22:39 +0000
  4. Subject: [PATCH 16/57] MIPS: ralink: add MT7621 pcie driver
  5. Signed-off-by: John Crispin <blogic@openwrt.org>
  6. ---
  7. arch/mips/pci/Makefile | 1 +
  8. arch/mips/pci/pci-mt7621.c | 797 ++++++++++++++++++++++++++++++++++++++++++++
  9. 2 files changed, 798 insertions(+)
  10. create mode 100644 arch/mips/pci/pci-mt7621.c
  11. --- a/arch/mips/pci/Makefile
  12. +++ b/arch/mips/pci/Makefile
  13. @@ -42,6 +42,7 @@ obj-$(CONFIG_SIBYTE_BCM1x80) += pci-bcm1
  14. obj-$(CONFIG_SNI_RM) += fixup-sni.o ops-sni.o
  15. obj-$(CONFIG_LANTIQ) += fixup-lantiq.o
  16. obj-$(CONFIG_PCI_LANTIQ) += pci-lantiq.o ops-lantiq.o
  17. +obj-$(CONFIG_SOC_MT7621) += pci-mt7621.o
  18. obj-$(CONFIG_SOC_RT3883) += pci-rt3883.o
  19. obj-$(CONFIG_TANBAC_TB0219) += fixup-tb0219.o
  20. obj-$(CONFIG_TANBAC_TB0226) += fixup-tb0226.o
  21. --- /dev/null
  22. +++ b/arch/mips/pci/pci-mt7621.c
  23. @@ -0,0 +1,813 @@
  24. +/**************************************************************************
  25. + *
  26. + * BRIEF MODULE DESCRIPTION
  27. + * PCI init for Ralink RT2880 solution
  28. + *
  29. + * Copyright 2007 Ralink Inc. (bruce_chang@ralinktech.com.tw)
  30. + *
  31. + * This program is free software; you can redistribute it and/or modify it
  32. + * under the terms of the GNU General Public License as published by the
  33. + * Free Software Foundation; either version 2 of the License, or (at your
  34. + * option) any later version.
  35. + *
  36. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  37. + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  38. + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  39. + * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  43. + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  45. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. + *
  47. + * You should have received a copy of the GNU General Public License along
  48. + * with this program; if not, write to the Free Software Foundation, Inc.,
  49. + * 675 Mass Ave, Cambridge, MA 02139, USA.
  50. + *
  51. + *
  52. + **************************************************************************
  53. + * May 2007 Bruce Chang
  54. + * Initial Release
  55. + *
  56. + * May 2009 Bruce Chang
  57. + * support RT2880/RT3883 PCIe
  58. + *
  59. + * May 2011 Bruce Chang
  60. + * support RT6855/MT7620 PCIe
  61. + *
  62. + **************************************************************************
  63. + */
  64. +
  65. +#include <linux/types.h>
  66. +#include <linux/pci.h>
  67. +#include <linux/kernel.h>
  68. +#include <linux/slab.h>
  69. +#include <linux/version.h>
  70. +#include <asm/pci.h>
  71. +#include <asm/io.h>
  72. +#include <linux/init.h>
  73. +#include <linux/module.h>
  74. +#include <linux/delay.h>
  75. +#include <linux/of.h>
  76. +#include <linux/of_pci.h>
  77. +#include <linux/platform_device.h>
  78. +
  79. +#include <ralink_regs.h>
  80. +
  81. +extern void pcie_phy_init(void);
  82. +extern void chk_phy_pll(void);
  83. +
  84. +/*
  85. + * These functions and structures provide the BIOS scan and mapping of the PCI
  86. + * devices.
  87. + */
  88. +
  89. +#define CONFIG_PCIE_PORT0
  90. +#define CONFIG_PCIE_PORT1
  91. +#define CONFIG_PCIE_PORT2
  92. +#define RALINK_PCIE0_CLK_EN (1<<24)
  93. +#define RALINK_PCIE1_CLK_EN (1<<25)
  94. +#define RALINK_PCIE2_CLK_EN (1<<26)
  95. +
  96. +#define RALINK_PCI_CONFIG_ADDR 0x20
  97. +#define RALINK_PCI_CONFIG_DATA_VIRTUAL_REG 0x24
  98. +#define SURFBOARDINT_PCIE0 12 /* PCIE0 */
  99. +#define RALINK_INT_PCIE0 SURFBOARDINT_PCIE0
  100. +#define RALINK_INT_PCIE1 SURFBOARDINT_PCIE1
  101. +#define RALINK_INT_PCIE2 SURFBOARDINT_PCIE2
  102. +#define SURFBOARDINT_PCIE1 32 /* PCIE1 */
  103. +#define SURFBOARDINT_PCIE2 33 /* PCIE2 */
  104. +#define RALINK_PCI_MEMBASE *(volatile u32 *)(RALINK_PCI_BASE + 0x0028)
  105. +#define RALINK_PCI_IOBASE *(volatile u32 *)(RALINK_PCI_BASE + 0x002C)
  106. +#define RALINK_PCIE0_RST (1<<24)
  107. +#define RALINK_PCIE1_RST (1<<25)
  108. +#define RALINK_PCIE2_RST (1<<26)
  109. +#define RALINK_SYSCTL_BASE 0xBE000000
  110. +
  111. +#define RALINK_PCI_PCICFG_ADDR *(volatile u32 *)(RALINK_PCI_BASE + 0x0000)
  112. +#define RALINK_PCI_PCIMSK_ADDR *(volatile u32 *)(RALINK_PCI_BASE + 0x000C)
  113. +#define RALINK_PCI_BASE 0xBE140000
  114. +
  115. +#define RALINK_PCIEPHY_P0P1_CTL_OFFSET (RALINK_PCI_BASE + 0x9000)
  116. +#define RT6855_PCIE0_OFFSET 0x2000
  117. +#define RT6855_PCIE1_OFFSET 0x3000
  118. +#define RT6855_PCIE2_OFFSET 0x4000
  119. +
  120. +#define RALINK_PCI0_BAR0SETUP_ADDR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE0_OFFSET + 0x0010)
  121. +#define RALINK_PCI0_IMBASEBAR0_ADDR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE0_OFFSET + 0x0018)
  122. +#define RALINK_PCI0_ID *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE0_OFFSET + 0x0030)
  123. +#define RALINK_PCI0_CLASS *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE0_OFFSET + 0x0034)
  124. +#define RALINK_PCI0_SUBID *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE0_OFFSET + 0x0038)
  125. +#define RALINK_PCI0_STATUS *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE0_OFFSET + 0x0050)
  126. +#define RALINK_PCI0_DERR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE0_OFFSET + 0x0060)
  127. +#define RALINK_PCI0_ECRC *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE0_OFFSET + 0x0064)
  128. +
  129. +#define RALINK_PCI1_BAR0SETUP_ADDR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE1_OFFSET + 0x0010)
  130. +#define RALINK_PCI1_IMBASEBAR0_ADDR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE1_OFFSET + 0x0018)
  131. +#define RALINK_PCI1_ID *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE1_OFFSET + 0x0030)
  132. +#define RALINK_PCI1_CLASS *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE1_OFFSET + 0x0034)
  133. +#define RALINK_PCI1_SUBID *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE1_OFFSET + 0x0038)
  134. +#define RALINK_PCI1_STATUS *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE1_OFFSET + 0x0050)
  135. +#define RALINK_PCI1_DERR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE1_OFFSET + 0x0060)
  136. +#define RALINK_PCI1_ECRC *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE1_OFFSET + 0x0064)
  137. +
  138. +#define RALINK_PCI2_BAR0SETUP_ADDR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE2_OFFSET + 0x0010)
  139. +#define RALINK_PCI2_IMBASEBAR0_ADDR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE2_OFFSET + 0x0018)
  140. +#define RALINK_PCI2_ID *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE2_OFFSET + 0x0030)
  141. +#define RALINK_PCI2_CLASS *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE2_OFFSET + 0x0034)
  142. +#define RALINK_PCI2_SUBID *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE2_OFFSET + 0x0038)
  143. +#define RALINK_PCI2_STATUS *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE2_OFFSET + 0x0050)
  144. +#define RALINK_PCI2_DERR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE2_OFFSET + 0x0060)
  145. +#define RALINK_PCI2_ECRC *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE2_OFFSET + 0x0064)
  146. +
  147. +#define RALINK_PCIEPHY_P0P1_CTL_OFFSET (RALINK_PCI_BASE + 0x9000)
  148. +#define RALINK_PCIEPHY_P2_CTL_OFFSET (RALINK_PCI_BASE + 0xA000)
  149. +
  150. +
  151. +#define MV_WRITE(ofs, data) \
  152. + *(volatile u32 *)(RALINK_PCI_BASE+(ofs)) = cpu_to_le32(data)
  153. +#define MV_READ(ofs, data) \
  154. + *(data) = le32_to_cpu(*(volatile u32 *)(RALINK_PCI_BASE+(ofs)))
  155. +#define MV_READ_DATA(ofs) \
  156. + le32_to_cpu(*(volatile u32 *)(RALINK_PCI_BASE+(ofs)))
  157. +
  158. +#define MV_WRITE_16(ofs, data) \
  159. + *(volatile u16 *)(RALINK_PCI_BASE+(ofs)) = cpu_to_le16(data)
  160. +#define MV_READ_16(ofs, data) \
  161. + *(data) = le16_to_cpu(*(volatile u16 *)(RALINK_PCI_BASE+(ofs)))
  162. +
  163. +#define MV_WRITE_8(ofs, data) \
  164. + *(volatile u8 *)(RALINK_PCI_BASE+(ofs)) = data
  165. +#define MV_READ_8(ofs, data) \
  166. + *(data) = *(volatile u8 *)(RALINK_PCI_BASE+(ofs))
  167. +
  168. +
  169. +
  170. +#define RALINK_PCI_MM_MAP_BASE 0x60000000
  171. +#define RALINK_PCI_IO_MAP_BASE 0x1e160000
  172. +
  173. +#define RALINK_SYSTEM_CONTROL_BASE 0xbe000000
  174. +#define GPIO_PERST
  175. +#define ASSERT_SYSRST_PCIE(val) do { \
  176. + if (*(unsigned int *)(0xbe00000c) == 0x00030101) \
  177. + RALINK_RSTCTRL |= val; \
  178. + else \
  179. + RALINK_RSTCTRL &= ~val; \
  180. + } while(0)
  181. +#define DEASSERT_SYSRST_PCIE(val) do { \
  182. + if (*(unsigned int *)(0xbe00000c) == 0x00030101) \
  183. + RALINK_RSTCTRL &= ~val; \
  184. + else \
  185. + RALINK_RSTCTRL |= val; \
  186. + } while(0)
  187. +#define RALINK_SYSCFG1 *(unsigned int *)(RALINK_SYSTEM_CONTROL_BASE + 0x14)
  188. +#define RALINK_CLKCFG1 *(unsigned int *)(RALINK_SYSTEM_CONTROL_BASE + 0x30)
  189. +#define RALINK_RSTCTRL *(unsigned int *)(RALINK_SYSTEM_CONTROL_BASE + 0x34)
  190. +#define RALINK_GPIOMODE *(unsigned int *)(RALINK_SYSTEM_CONTROL_BASE + 0x60)
  191. +#define RALINK_PCIE_CLK_GEN *(unsigned int *)(RALINK_SYSTEM_CONTROL_BASE + 0x7c)
  192. +#define RALINK_PCIE_CLK_GEN1 *(unsigned int *)(RALINK_SYSTEM_CONTROL_BASE + 0x80)
  193. +#define PPLL_CFG1 *(unsigned int *)(RALINK_SYSTEM_CONTROL_BASE + 0x9c)
  194. +#define PPLL_DRV *(unsigned int *)(RALINK_SYSTEM_CONTROL_BASE + 0xa0)
  195. +//RALINK_SYSCFG1 bit
  196. +#define RALINK_PCI_HOST_MODE_EN (1<<7)
  197. +#define RALINK_PCIE_RC_MODE_EN (1<<8)
  198. +//RALINK_RSTCTRL bit
  199. +#define RALINK_PCIE_RST (1<<23)
  200. +#define RALINK_PCI_RST (1<<24)
  201. +//RALINK_CLKCFG1 bit
  202. +#define RALINK_PCI_CLK_EN (1<<19)
  203. +#define RALINK_PCIE_CLK_EN (1<<21)
  204. +//RALINK_GPIOMODE bit
  205. +#define PCI_SLOTx2 (1<<11)
  206. +#define PCI_SLOTx1 (2<<11)
  207. +//MTK PCIE PLL bit
  208. +#define PDRV_SW_SET (1<<31)
  209. +#define LC_CKDRVPD_ (1<<19)
  210. +
  211. +#define MEMORY_BASE 0x0
  212. +static int pcie_link_status = 0;
  213. +
  214. +#define PCI_ACCESS_READ_1 0
  215. +#define PCI_ACCESS_READ_2 1
  216. +#define PCI_ACCESS_READ_4 2
  217. +#define PCI_ACCESS_WRITE_1 3
  218. +#define PCI_ACCESS_WRITE_2 4
  219. +#define PCI_ACCESS_WRITE_4 5
  220. +
  221. +static int config_access(unsigned char access_type, struct pci_bus *bus,
  222. + unsigned int devfn, unsigned int where, u32 * data)
  223. +{
  224. + unsigned int slot = PCI_SLOT(devfn);
  225. + u8 func = PCI_FUNC(devfn);
  226. + uint32_t address_reg, data_reg;
  227. + unsigned int address;
  228. +
  229. + address_reg = RALINK_PCI_CONFIG_ADDR;
  230. + data_reg = RALINK_PCI_CONFIG_DATA_VIRTUAL_REG;
  231. +
  232. + address = (((where&0xF00)>>8)<<24) |(bus->number << 16) | (slot << 11) | (func << 8) | (where & 0xfc) | 0x80000000;
  233. + MV_WRITE(address_reg, address);
  234. +
  235. + switch(access_type) {
  236. + case PCI_ACCESS_WRITE_1:
  237. + MV_WRITE_8(data_reg+(where&0x3), *data);
  238. + break;
  239. + case PCI_ACCESS_WRITE_2:
  240. + MV_WRITE_16(data_reg+(where&0x3), *data);
  241. + break;
  242. + case PCI_ACCESS_WRITE_4:
  243. + MV_WRITE(data_reg, *data);
  244. + break;
  245. + case PCI_ACCESS_READ_1:
  246. + MV_READ_8( data_reg+(where&0x3), data);
  247. + break;
  248. + case PCI_ACCESS_READ_2:
  249. + MV_READ_16(data_reg+(where&0x3), data);
  250. + break;
  251. + case PCI_ACCESS_READ_4:
  252. + MV_READ(data_reg, data);
  253. + break;
  254. + default:
  255. + printk("no specify access type\n");
  256. + break;
  257. + }
  258. + return 0;
  259. +}
  260. +
  261. +static int
  262. +read_config_byte(struct pci_bus *bus, unsigned int devfn, int where, u8 * val)
  263. +{
  264. + return config_access(PCI_ACCESS_READ_1, bus, devfn, (unsigned int)where, (u32 *)val);
  265. +}
  266. +
  267. +static int
  268. +read_config_word(struct pci_bus *bus, unsigned int devfn, int where, u16 * val)
  269. +{
  270. + return config_access(PCI_ACCESS_READ_2, bus, devfn, (unsigned int)where, (u32 *)val);
  271. +}
  272. +
  273. +static int
  274. +read_config_dword(struct pci_bus *bus, unsigned int devfn, int where, u32 * val)
  275. +{
  276. + return config_access(PCI_ACCESS_READ_4, bus, devfn, (unsigned int)where, (u32 *)val);
  277. +}
  278. +
  279. +static int
  280. +write_config_byte(struct pci_bus *bus, unsigned int devfn, int where, u8 val)
  281. +{
  282. + if (config_access(PCI_ACCESS_WRITE_1, bus, devfn, (unsigned int)where, (u32 *)&val))
  283. + return -1;
  284. +
  285. + return PCIBIOS_SUCCESSFUL;
  286. +}
  287. +
  288. +static int
  289. +write_config_word(struct pci_bus *bus, unsigned int devfn, int where, u16 val)
  290. +{
  291. + if (config_access(PCI_ACCESS_WRITE_2, bus, devfn, where, (u32 *)&val))
  292. + return -1;
  293. +
  294. + return PCIBIOS_SUCCESSFUL;
  295. +}
  296. +
  297. +static int
  298. +write_config_dword(struct pci_bus *bus, unsigned int devfn, int where, u32 val)
  299. +{
  300. + if (config_access(PCI_ACCESS_WRITE_4, bus, devfn, where, &val))
  301. + return -1;
  302. +
  303. + return PCIBIOS_SUCCESSFUL;
  304. +}
  305. +
  306. +
  307. +static int
  308. +pci_config_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 * val)
  309. +{
  310. + switch (size) {
  311. + case 1:
  312. + return read_config_byte(bus, devfn, where, (u8 *) val);
  313. + case 2:
  314. + return read_config_word(bus, devfn, where, (u16 *) val);
  315. + default:
  316. + return read_config_dword(bus, devfn, where, val);
  317. + }
  318. +}
  319. +
  320. +static int
  321. +pci_config_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 val)
  322. +{
  323. + switch (size) {
  324. + case 1:
  325. + return write_config_byte(bus, devfn, where, (u8) val);
  326. + case 2:
  327. + return write_config_word(bus, devfn, where, (u16) val);
  328. + default:
  329. + return write_config_dword(bus, devfn, where, val);
  330. + }
  331. +}
  332. +
  333. +struct pci_ops mt7621_pci_ops= {
  334. + .read = pci_config_read,
  335. + .write = pci_config_write,
  336. +};
  337. +
  338. +static struct resource mt7621_res_pci_mem1 = {
  339. + .name = "PCI MEM1",
  340. + .start = RALINK_PCI_MM_MAP_BASE,
  341. + .end = (u32)((RALINK_PCI_MM_MAP_BASE + (unsigned char *)0x0fffffff)),
  342. + .flags = IORESOURCE_MEM,
  343. +};
  344. +static struct resource mt7621_res_pci_io1 = {
  345. + .name = "PCI I/O1",
  346. + .start = RALINK_PCI_IO_MAP_BASE,
  347. + .end = (u32)((RALINK_PCI_IO_MAP_BASE + (unsigned char *)0x0ffff)),
  348. + .flags = IORESOURCE_IO,
  349. +};
  350. +
  351. +static struct pci_controller mt7621_controller = {
  352. + .pci_ops = &mt7621_pci_ops,
  353. + .mem_resource = &mt7621_res_pci_mem1,
  354. + .io_resource = &mt7621_res_pci_io1,
  355. + .mem_offset = 0x00000000UL,
  356. + .io_offset = 0x00000000UL,
  357. + .io_map_base = 0xa0000000,
  358. +};
  359. +
  360. +static void
  361. +read_config(unsigned long bus, unsigned long dev, unsigned long func, unsigned long reg, unsigned long *val)
  362. +{
  363. + unsigned int address_reg, data_reg, address;
  364. +
  365. + address_reg = RALINK_PCI_CONFIG_ADDR;
  366. + data_reg = RALINK_PCI_CONFIG_DATA_VIRTUAL_REG;
  367. + address = (((reg & 0xF00)>>8)<<24) | (bus << 16) | (dev << 11) | (func << 8) | (reg & 0xfc) | 0x80000000 ;
  368. + MV_WRITE(address_reg, address);
  369. + MV_READ(data_reg, val);
  370. + return;
  371. +}
  372. +
  373. +static void
  374. +write_config(unsigned long bus, unsigned long dev, unsigned long func, unsigned long reg, unsigned long val)
  375. +{
  376. + unsigned int address_reg, data_reg, address;
  377. +
  378. + address_reg = RALINK_PCI_CONFIG_ADDR;
  379. + data_reg = RALINK_PCI_CONFIG_DATA_VIRTUAL_REG;
  380. + address = (((reg & 0xF00)>>8)<<24) | (bus << 16) | (dev << 11) | (func << 8) | (reg & 0xfc) | 0x80000000 ;
  381. + MV_WRITE(address_reg, address);
  382. + MV_WRITE(data_reg, val);
  383. + return;
  384. +}
  385. +
  386. +
  387. +int __init
  388. +pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  389. +{
  390. + u16 cmd;
  391. + u32 val;
  392. + int irq = 0;
  393. +
  394. + if ((dev->bus->number == 0) && (slot == 0)) {
  395. + write_config(0, 0, 0, PCI_BASE_ADDRESS_0, MEMORY_BASE);
  396. + read_config(0, 0, 0, PCI_BASE_ADDRESS_0, (unsigned long *)&val);
  397. + printk("BAR0 at slot 0 = %x\n", val);
  398. + printk("bus=0x%x, slot = 0x%x\n",dev->bus->number, slot);
  399. + } else if((dev->bus->number == 0) && (slot == 0x1)) {
  400. + write_config(0, 1, 0, PCI_BASE_ADDRESS_0, MEMORY_BASE);
  401. + read_config(0, 1, 0, PCI_BASE_ADDRESS_0, (unsigned long *)&val);
  402. + printk("BAR0 at slot 1 = %x\n", val);
  403. + printk("bus=0x%x, slot = 0x%x\n",dev->bus->number, slot);
  404. + } else if((dev->bus->number == 0) && (slot == 0x2)) {
  405. + write_config(0, 2, 0, PCI_BASE_ADDRESS_0, MEMORY_BASE);
  406. + read_config(0, 2, 0, PCI_BASE_ADDRESS_0, (unsigned long *)&val);
  407. + printk("BAR0 at slot 2 = %x\n", val);
  408. + printk("bus=0x%x, slot = 0x%x\n",dev->bus->number, slot);
  409. + } else if ((dev->bus->number == 1) && (slot == 0x0)) {
  410. + switch (pcie_link_status) {
  411. + case 2:
  412. + case 6:
  413. + irq = RALINK_INT_PCIE1;
  414. + break;
  415. + case 4:
  416. + irq = RALINK_INT_PCIE2;
  417. + break;
  418. + default:
  419. + irq = RALINK_INT_PCIE0;
  420. + }
  421. + printk("bus=0x%x, slot = 0x%x, irq=0x%x\n",dev->bus->number, slot, dev->irq);
  422. + } else if ((dev->bus->number == 2) && (slot == 0x0)) {
  423. + switch (pcie_link_status) {
  424. + case 5:
  425. + case 6:
  426. + irq = RALINK_INT_PCIE2;
  427. + break;
  428. + default:
  429. + irq = RALINK_INT_PCIE1;
  430. + }
  431. + printk("bus=0x%x, slot = 0x%x, irq=0x%x\n",dev->bus->number, slot, dev->irq);
  432. + } else if ((dev->bus->number == 2) && (slot == 0x1)) {
  433. + switch (pcie_link_status) {
  434. + case 5:
  435. + case 6:
  436. + irq = RALINK_INT_PCIE2;
  437. + break;
  438. + default:
  439. + irq = RALINK_INT_PCIE1;
  440. + }
  441. + printk("bus=0x%x, slot = 0x%x, irq=0x%x\n",dev->bus->number, slot, dev->irq);
  442. + } else if ((dev->bus->number ==3) && (slot == 0x0)) {
  443. + irq = RALINK_INT_PCIE2;
  444. + printk("bus=0x%x, slot = 0x%x, irq=0x%x\n",dev->bus->number, slot, dev->irq);
  445. + } else if ((dev->bus->number ==3) && (slot == 0x1)) {
  446. + irq = RALINK_INT_PCIE2;
  447. + printk("bus=0x%x, slot = 0x%x, irq=0x%x\n",dev->bus->number, slot, dev->irq);
  448. + } else if ((dev->bus->number ==3) && (slot == 0x2)) {
  449. + irq = RALINK_INT_PCIE2;
  450. + printk("bus=0x%x, slot = 0x%x, irq=0x%x\n",dev->bus->number, slot, dev->irq);
  451. + } else {
  452. + printk("bus=0x%x, slot = 0x%x\n",dev->bus->number, slot);
  453. + return 0;
  454. + }
  455. +
  456. + pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 0x14); //configure cache line size 0x14
  457. + pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xFF); //configure latency timer 0x10
  458. + pci_read_config_word(dev, PCI_COMMAND, &cmd);
  459. + cmd = cmd | PCI_COMMAND_MASTER | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
  460. + pci_write_config_word(dev, PCI_COMMAND, cmd);
  461. + pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  462. + return irq;
  463. +}
  464. +
  465. +void
  466. +set_pcie_phy(u32 *addr, int start_b, int bits, int val)
  467. +{
  468. +// printk("0x%p:", addr);
  469. +// printk(" %x", *addr);
  470. + *(unsigned int *)(addr) &= ~(((1<<bits) - 1)<<start_b);
  471. + *(unsigned int *)(addr) |= val << start_b;
  472. +// printk(" -> %x\n", *addr);
  473. +}
  474. +
  475. +void
  476. +bypass_pipe_rst(void)
  477. +{
  478. +#if defined (CONFIG_PCIE_PORT0)
  479. + /* PCIe Port 0 */
  480. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x02c), 12, 1, 0x01); // rg_pe1_pipe_rst_b
  481. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x02c), 4, 1, 0x01); // rg_pe1_pipe_cmd_frc[4]
  482. +#endif
  483. +#if defined (CONFIG_PCIE_PORT1)
  484. + /* PCIe Port 1 */
  485. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x12c), 12, 1, 0x01); // rg_pe1_pipe_rst_b
  486. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x12c), 4, 1, 0x01); // rg_pe1_pipe_cmd_frc[4]
  487. +#endif
  488. +#if defined (CONFIG_PCIE_PORT2)
  489. + /* PCIe Port 2 */
  490. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x02c), 12, 1, 0x01); // rg_pe1_pipe_rst_b
  491. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x02c), 4, 1, 0x01); // rg_pe1_pipe_cmd_frc[4]
  492. +#endif
  493. +}
  494. +
  495. +void
  496. +set_phy_for_ssc(void)
  497. +{
  498. + unsigned long reg = (*(volatile u32 *)(RALINK_SYSCTL_BASE + 0x10));
  499. +
  500. + reg = (reg >> 6) & 0x7;
  501. +#if defined (CONFIG_PCIE_PORT0) || defined (CONFIG_PCIE_PORT1)
  502. + /* Set PCIe Port0 & Port1 PHY to disable SSC */
  503. + /* Debug Xtal Type */
  504. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x400), 8, 1, 0x01); // rg_pe1_frc_h_xtal_type
  505. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x400), 9, 2, 0x00); // rg_pe1_h_xtal_type
  506. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x000), 4, 1, 0x01); // rg_pe1_frc_phy_en //Force Port 0 enable control
  507. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x100), 4, 1, 0x01); // rg_pe1_frc_phy_en //Force Port 1 enable control
  508. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x000), 5, 1, 0x00); // rg_pe1_phy_en //Port 0 disable
  509. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x100), 5, 1, 0x00); // rg_pe1_phy_en //Port 1 disable
  510. + if(reg <= 5 && reg >= 3) { // 40MHz Xtal
  511. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 6, 2, 0x01); // RG_PE1_H_PLL_PREDIV //Pre-divider ratio (for host mode)
  512. + printk("***** Xtal 40MHz *****\n");
  513. + } else { // 25MHz | 20MHz Xtal
  514. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 6, 2, 0x00); // RG_PE1_H_PLL_PREDIV //Pre-divider ratio (for host mode)
  515. + if (reg >= 6) {
  516. + printk("***** Xtal 25MHz *****\n");
  517. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4bc), 4, 2, 0x01); // RG_PE1_H_PLL_FBKSEL //Feedback clock select
  518. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x49c), 0,31, 0x18000000); // RG_PE1_H_LCDDS_PCW_NCPO //DDS NCPO PCW (for host mode)
  519. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4a4), 0,16, 0x18d); // RG_PE1_H_LCDDS_SSC_PRD //DDS SSC dither period control
  520. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4a8), 0,12, 0x4a); // RG_PE1_H_LCDDS_SSC_DELTA //DDS SSC dither amplitude control
  521. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4a8), 16,12, 0x4a); // RG_PE1_H_LCDDS_SSC_DELTA1 //DDS SSC dither amplitude control for initial
  522. + } else {
  523. + printk("***** Xtal 20MHz *****\n");
  524. + }
  525. + }
  526. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4a0), 5, 1, 0x01); // RG_PE1_LCDDS_CLK_PH_INV //DDS clock inversion
  527. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 22, 2, 0x02); // RG_PE1_H_PLL_BC
  528. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 18, 4, 0x06); // RG_PE1_H_PLL_BP
  529. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 12, 4, 0x02); // RG_PE1_H_PLL_IR
  530. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 8, 4, 0x01); // RG_PE1_H_PLL_IC
  531. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4ac), 16, 3, 0x00); // RG_PE1_H_PLL_BR
  532. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 1, 3, 0x02); // RG_PE1_PLL_DIVEN
  533. + if(reg <= 5 && reg >= 3) { // 40MHz Xtal
  534. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x414), 6, 2, 0x01); // rg_pe1_mstckdiv //value of da_pe1_mstckdiv when force mode enable
  535. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x414), 5, 1, 0x01); // rg_pe1_frc_mstckdiv //force mode enable of da_pe1_mstckdiv
  536. + }
  537. + /* Enable PHY and disable force mode */
  538. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x000), 5, 1, 0x01); // rg_pe1_phy_en //Port 0 enable
  539. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x100), 5, 1, 0x01); // rg_pe1_phy_en //Port 1 enable
  540. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x000), 4, 1, 0x00); // rg_pe1_frc_phy_en //Force Port 0 disable control
  541. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x100), 4, 1, 0x00); // rg_pe1_frc_phy_en //Force Port 1 disable control
  542. +#endif
  543. +#if defined (CONFIG_PCIE_PORT2)
  544. + /* Set PCIe Port2 PHY to disable SSC */
  545. + /* Debug Xtal Type */
  546. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x400), 8, 1, 0x01); // rg_pe1_frc_h_xtal_type
  547. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x400), 9, 2, 0x00); // rg_pe1_h_xtal_type
  548. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x000), 4, 1, 0x01); // rg_pe1_frc_phy_en //Force Port 0 enable control
  549. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x000), 5, 1, 0x00); // rg_pe1_phy_en //Port 0 disable
  550. + if(reg <= 5 && reg >= 3) { // 40MHz Xtal
  551. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 6, 2, 0x01); // RG_PE1_H_PLL_PREDIV //Pre-divider ratio (for host mode)
  552. + } else { // 25MHz | 20MHz Xtal
  553. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 6, 2, 0x00); // RG_PE1_H_PLL_PREDIV //Pre-divider ratio (for host mode)
  554. + if (reg >= 6) { // 25MHz Xtal
  555. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4bc), 4, 2, 0x01); // RG_PE1_H_PLL_FBKSEL //Feedback clock select
  556. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x49c), 0,31, 0x18000000); // RG_PE1_H_LCDDS_PCW_NCPO //DDS NCPO PCW (for host mode)
  557. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4a4), 0,16, 0x18d); // RG_PE1_H_LCDDS_SSC_PRD //DDS SSC dither period control
  558. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4a8), 0,12, 0x4a); // RG_PE1_H_LCDDS_SSC_DELTA //DDS SSC dither amplitude control
  559. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4a8), 16,12, 0x4a); // RG_PE1_H_LCDDS_SSC_DELTA1 //DDS SSC dither amplitude control for initial
  560. + }
  561. + }
  562. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4a0), 5, 1, 0x01); // RG_PE1_LCDDS_CLK_PH_INV //DDS clock inversion
  563. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 22, 2, 0x02); // RG_PE1_H_PLL_BC
  564. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 18, 4, 0x06); // RG_PE1_H_PLL_BP
  565. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 12, 4, 0x02); // RG_PE1_H_PLL_IR
  566. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 8, 4, 0x01); // RG_PE1_H_PLL_IC
  567. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4ac), 16, 3, 0x00); // RG_PE1_H_PLL_BR
  568. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 1, 3, 0x02); // RG_PE1_PLL_DIVEN
  569. + if(reg <= 5 && reg >= 3) { // 40MHz Xtal
  570. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x414), 6, 2, 0x01); // rg_pe1_mstckdiv //value of da_pe1_mstckdiv when force mode enable
  571. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x414), 5, 1, 0x01); // rg_pe1_frc_mstckdiv //force mode enable of da_pe1_mstckdiv
  572. + }
  573. + /* Enable PHY and disable force mode */
  574. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x000), 5, 1, 0x01); // rg_pe1_phy_en //Port 0 enable
  575. + set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x000), 4, 1, 0x00); // rg_pe1_frc_phy_en //Force Port 0 disable control
  576. +#endif
  577. +}
  578. +
  579. +static int mt7621_pci_probe(struct platform_device *pdev)
  580. +{
  581. + unsigned long val = 0;
  582. +
  583. + iomem_resource.start = 0;
  584. + iomem_resource.end= ~0;
  585. + ioport_resource.start= 0;
  586. + ioport_resource.end = ~0;
  587. +
  588. +#if defined (CONFIG_PCIE_PORT0)
  589. + val = RALINK_PCIE0_RST;
  590. +#endif
  591. +#if defined (CONFIG_PCIE_PORT1)
  592. + val |= RALINK_PCIE1_RST;
  593. +#endif
  594. +#if defined (CONFIG_PCIE_PORT2)
  595. + val |= RALINK_PCIE2_RST;
  596. +#endif
  597. + ASSERT_SYSRST_PCIE(RALINK_PCIE0_RST | RALINK_PCIE1_RST | RALINK_PCIE2_RST);
  598. + printk("pull PCIe RST: RALINK_RSTCTRL = %x\n", RALINK_RSTCTRL);
  599. +#if defined GPIO_PERST /* add GPIO control instead of PERST_N */ /*chhung*/
  600. + *(unsigned int *)(0xbe000060) &= ~(0x3<<10 | 0x3<<3);
  601. + *(unsigned int *)(0xbe000060) |= 0x1<<10 | 0x1<<3;
  602. + mdelay(100);
  603. + *(unsigned int *)(0xbe000600) |= 0x1<<19 | 0x1<<8 | 0x1<<7; // use GPIO19/GPIO8/GPIO7 (PERST_N/UART_RXD3/UART_TXD3)
  604. + mdelay(100);
  605. + *(unsigned int *)(0xbe000620) &= ~(0x1<<19 | 0x1<<8 | 0x1<<7); // clear DATA
  606. +
  607. + mdelay(100);
  608. +#else
  609. + *(unsigned int *)(0xbe000060) &= ~0x00000c00;
  610. +#endif
  611. +#if defined (CONFIG_PCIE_PORT0)
  612. + val = RALINK_PCIE0_RST;
  613. +#endif
  614. +#if defined (CONFIG_PCIE_PORT1)
  615. + val |= RALINK_PCIE1_RST;
  616. +#endif
  617. +#if defined (CONFIG_PCIE_PORT2)
  618. + val |= RALINK_PCIE2_RST;
  619. +#endif
  620. + DEASSERT_SYSRST_PCIE(val);
  621. + printk("release PCIe RST: RALINK_RSTCTRL = %x\n", RALINK_RSTCTRL);
  622. +
  623. + if ((*(unsigned int *)(0xbe00000c)&0xFFFF) == 0x0101) // MT7621 E2
  624. + bypass_pipe_rst();
  625. + set_phy_for_ssc();
  626. + printk("release PCIe RST: RALINK_RSTCTRL = %x\n", RALINK_RSTCTRL);
  627. +
  628. +#if defined (CONFIG_PCIE_PORT0)
  629. + read_config(0, 0, 0, 0x70c, &val);
  630. + printk("Port 0 N_FTS = %x\n", (unsigned int)val);
  631. +#endif
  632. +#if defined (CONFIG_PCIE_PORT1)
  633. + read_config(0, 1, 0, 0x70c, &val);
  634. + printk("Port 1 N_FTS = %x\n", (unsigned int)val);
  635. +#endif
  636. +#if defined (CONFIG_PCIE_PORT2)
  637. + read_config(0, 2, 0, 0x70c, &val);
  638. + printk("Port 2 N_FTS = %x\n", (unsigned int)val);
  639. +#endif
  640. +
  641. + RALINK_RSTCTRL = (RALINK_RSTCTRL | RALINK_PCIE_RST);
  642. + RALINK_SYSCFG1 &= ~(0x30);
  643. + RALINK_SYSCFG1 |= (2<<4);
  644. + RALINK_PCIE_CLK_GEN &= 0x7fffffff;
  645. + RALINK_PCIE_CLK_GEN1 &= 0x80ffffff;
  646. + RALINK_PCIE_CLK_GEN1 |= 0xa << 24;
  647. + RALINK_PCIE_CLK_GEN |= 0x80000000;
  648. + mdelay(50);
  649. + RALINK_RSTCTRL = (RALINK_RSTCTRL & ~RALINK_PCIE_RST);
  650. +
  651. +
  652. +#if defined GPIO_PERST /* add GPIO control instead of PERST_N */ /*chhung*/
  653. + *(unsigned int *)(0xbe000620) |= 0x1<<19 | 0x1<<8 | 0x1<<7; // set DATA
  654. + mdelay(100);
  655. +#else
  656. + RALINK_PCI_PCICFG_ADDR &= ~(1<<1); //de-assert PERST
  657. +#endif
  658. + mdelay(500);
  659. +
  660. +
  661. + mdelay(500);
  662. +#if defined (CONFIG_PCIE_PORT0)
  663. + if(( RALINK_PCI0_STATUS & 0x1) == 0)
  664. + {
  665. + printk("PCIE0 no card, disable it(RST&CLK)\n");
  666. + ASSERT_SYSRST_PCIE(RALINK_PCIE0_RST);
  667. + RALINK_CLKCFG1 = (RALINK_CLKCFG1 & ~RALINK_PCIE0_CLK_EN);
  668. + pcie_link_status &= ~(1<<0);
  669. + } else {
  670. + pcie_link_status |= 1<<0;
  671. + RALINK_PCI_PCIMSK_ADDR |= (1<<20); // enable pcie1 interrupt
  672. + }
  673. +#endif
  674. +#if defined (CONFIG_PCIE_PORT1)
  675. + if(( RALINK_PCI1_STATUS & 0x1) == 0)
  676. + {
  677. + printk("PCIE1 no card, disable it(RST&CLK)\n");
  678. + ASSERT_SYSRST_PCIE(RALINK_PCIE1_RST);
  679. + RALINK_CLKCFG1 = (RALINK_CLKCFG1 & ~RALINK_PCIE1_CLK_EN);
  680. + pcie_link_status &= ~(1<<1);
  681. + } else {
  682. + pcie_link_status |= 1<<1;
  683. + RALINK_PCI_PCIMSK_ADDR |= (1<<21); // enable pcie1 interrupt
  684. + }
  685. +#endif
  686. +#if defined (CONFIG_PCIE_PORT2)
  687. + if (( RALINK_PCI2_STATUS & 0x1) == 0) {
  688. + printk("PCIE2 no card, disable it(RST&CLK)\n");
  689. + ASSERT_SYSRST_PCIE(RALINK_PCIE2_RST);
  690. + RALINK_CLKCFG1 = (RALINK_CLKCFG1 & ~RALINK_PCIE2_CLK_EN);
  691. + pcie_link_status &= ~(1<<2);
  692. + } else {
  693. + pcie_link_status |= 1<<2;
  694. + RALINK_PCI_PCIMSK_ADDR |= (1<<22); // enable pcie2 interrupt
  695. + }
  696. +#endif
  697. + if (pcie_link_status == 0)
  698. + return 0;
  699. +
  700. +/*
  701. +pcie(2/1/0) link status pcie2_num pcie1_num pcie0_num
  702. +3'b000 x x x
  703. +3'b001 x x 0
  704. +3'b010 x 0 x
  705. +3'b011 x 1 0
  706. +3'b100 0 x x
  707. +3'b101 1 x 0
  708. +3'b110 1 0 x
  709. +3'b111 2 1 0
  710. +*/
  711. + switch(pcie_link_status) {
  712. + case 2:
  713. + RALINK_PCI_PCICFG_ADDR &= ~0x00ff0000;
  714. + RALINK_PCI_PCICFG_ADDR |= 0x1 << 16; //port0
  715. + RALINK_PCI_PCICFG_ADDR |= 0x0 << 20; //port1
  716. + break;
  717. + case 4:
  718. + RALINK_PCI_PCICFG_ADDR &= ~0x0fff0000;
  719. + RALINK_PCI_PCICFG_ADDR |= 0x1 << 16; //port0
  720. + RALINK_PCI_PCICFG_ADDR |= 0x2 << 20; //port1
  721. + RALINK_PCI_PCICFG_ADDR |= 0x0 << 24; //port2
  722. + break;
  723. + case 5:
  724. + RALINK_PCI_PCICFG_ADDR &= ~0x0fff0000;
  725. + RALINK_PCI_PCICFG_ADDR |= 0x0 << 16; //port0
  726. + RALINK_PCI_PCICFG_ADDR |= 0x2 << 20; //port1
  727. + RALINK_PCI_PCICFG_ADDR |= 0x1 << 24; //port2
  728. + break;
  729. + case 6:
  730. + RALINK_PCI_PCICFG_ADDR &= ~0x0fff0000;
  731. + RALINK_PCI_PCICFG_ADDR |= 0x2 << 16; //port0
  732. + RALINK_PCI_PCICFG_ADDR |= 0x0 << 20; //port1
  733. + RALINK_PCI_PCICFG_ADDR |= 0x1 << 24; //port2
  734. + break;
  735. + }
  736. + printk(" -> %x\n", RALINK_PCI_PCICFG_ADDR);
  737. + //printk(" RALINK_PCI_ARBCTL = %x\n", RALINK_PCI_ARBCTL);
  738. +
  739. +/*
  740. + ioport_resource.start = mt7621_res_pci_io1.start;
  741. + ioport_resource.end = mt7621_res_pci_io1.end;
  742. +*/
  743. +
  744. + RALINK_PCI_MEMBASE = 0xffffffff; //RALINK_PCI_MM_MAP_BASE;
  745. + RALINK_PCI_IOBASE = RALINK_PCI_IO_MAP_BASE;
  746. +
  747. +#if defined (CONFIG_PCIE_PORT0)
  748. + //PCIe0
  749. + if((pcie_link_status & 0x1) != 0) {
  750. + RALINK_PCI0_BAR0SETUP_ADDR = 0x7FFF0001; //open 7FFF:2G; ENABLE
  751. + RALINK_PCI0_IMBASEBAR0_ADDR = MEMORY_BASE;
  752. + RALINK_PCI0_CLASS = 0x06040001;
  753. + printk("PCIE0 enabled\n");
  754. + }
  755. +#endif
  756. +#if defined (CONFIG_PCIE_PORT1)
  757. + //PCIe1
  758. + if ((pcie_link_status & 0x2) != 0) {
  759. + RALINK_PCI1_BAR0SETUP_ADDR = 0x7FFF0001; //open 7FFF:2G; ENABLE
  760. + RALINK_PCI1_IMBASEBAR0_ADDR = MEMORY_BASE;
  761. + RALINK_PCI1_CLASS = 0x06040001;
  762. + printk("PCIE1 enabled\n");
  763. + }
  764. +#endif
  765. +#if defined (CONFIG_PCIE_PORT2)
  766. + //PCIe2
  767. + if ((pcie_link_status & 0x4) != 0) {
  768. + RALINK_PCI2_BAR0SETUP_ADDR = 0x7FFF0001; //open 7FFF:2G; ENABLE
  769. + RALINK_PCI2_IMBASEBAR0_ADDR = MEMORY_BASE;
  770. + RALINK_PCI2_CLASS = 0x06040001;
  771. + printk("PCIE2 enabled\n");
  772. + }
  773. +#endif
  774. +
  775. +
  776. + switch(pcie_link_status) {
  777. + case 7:
  778. + read_config(0, 2, 0, 0x4, &val);
  779. + write_config(0, 2, 0, 0x4, val|0x4);
  780. + // write_config(0, 1, 0, 0x4, val|0x7);
  781. + read_config(0, 2, 0, 0x70c, &val);
  782. + val &= ~(0xff)<<8;
  783. + val |= 0x50<<8;
  784. + write_config(0, 2, 0, 0x70c, val);
  785. + case 3:
  786. + case 5:
  787. + case 6:
  788. + read_config(0, 1, 0, 0x4, &val);
  789. + write_config(0, 1, 0, 0x4, val|0x4);
  790. + // write_config(0, 1, 0, 0x4, val|0x7);
  791. + read_config(0, 1, 0, 0x70c, &val);
  792. + val &= ~(0xff)<<8;
  793. + val |= 0x50<<8;
  794. + write_config(0, 1, 0, 0x70c, val);
  795. + default:
  796. + read_config(0, 0, 0, 0x4, &val);
  797. + write_config(0, 0, 0, 0x4, val|0x4); //bus master enable
  798. + // write_config(0, 0, 0, 0x4, val|0x7); //bus master enable
  799. + read_config(0, 0, 0, 0x70c, &val);
  800. + val &= ~(0xff)<<8;
  801. + val |= 0x50<<8;
  802. + write_config(0, 0, 0, 0x70c, val);
  803. + }
  804. +
  805. + pci_load_of_ranges(&mt7621_controller, pdev->dev.of_node);
  806. + register_pci_controller(&mt7621_controller);
  807. + return 0;
  808. +
  809. +}
  810. +
  811. +int pcibios_plat_dev_init(struct pci_dev *dev)
  812. +{
  813. + return 0;
  814. +}
  815. +
  816. +static const struct of_device_id mt7621_pci_ids[] = {
  817. + { .compatible = "mediatek,mt7621-pci" },
  818. + {},
  819. +};
  820. +MODULE_DEVICE_TABLE(of, mt7621_pci_ids);
  821. +
  822. +static struct platform_driver mt7621_pci_driver = {
  823. + .probe = mt7621_pci_probe,
  824. + .driver = {
  825. + .name = "mt7621-pci",
  826. + .owner = THIS_MODULE,
  827. + .of_match_table = of_match_ptr(mt7621_pci_ids),
  828. + },
  829. +};
  830. +
  831. +static int __init mt7621_pci_init(void)
  832. +{
  833. + return platform_driver_register(&mt7621_pci_driver);
  834. +}
  835. +
  836. +arch_initcall(mt7621_pci_init);