pci-rt3883.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. * Ralink RT3662/RT3883 SoC PCI support
  3. *
  4. * Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * Parts of this file are based on Ralink's 2.6.21 BSP
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published
  10. * by the Free Software Foundation.
  11. */
  12. #include <linux/types.h>
  13. #include <linux/pci.h>
  14. #include <linux/io.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_pci.h>
  22. #include <linux/platform_device.h>
  23. #include <asm/mach-ralink/rt3883.h>
  24. #include <asm/mach-ralink/ralink_regs.h>
  25. #define RT3883_MEMORY_BASE 0x00000000
  26. #define RT3883_MEMORY_SIZE 0x02000000
  27. #define RT3883_PCI_REG_PCICFG 0x00
  28. #define RT3883_PCICFG_P2P_BR_DEVNUM_M 0xf
  29. #define RT3883_PCICFG_P2P_BR_DEVNUM_S 16
  30. #define RT3883_PCICFG_PCIRST BIT(1)
  31. #define RT3883_PCI_REG_PCIRAW 0x04
  32. #define RT3883_PCI_REG_PCIINT 0x08
  33. #define RT3883_PCI_REG_PCIENA 0x0c
  34. #define RT3883_PCI_REG_CFGADDR 0x20
  35. #define RT3883_PCI_REG_CFGDATA 0x24
  36. #define RT3883_PCI_REG_MEMBASE 0x28
  37. #define RT3883_PCI_REG_IOBASE 0x2c
  38. #define RT3883_PCI_REG_ARBCTL 0x80
  39. #define RT3883_PCI_REG_BASE(_x) (0x1000 + (_x) * 0x1000)
  40. #define RT3883_PCI_REG_BAR0SETUP(_x) (RT3883_PCI_REG_BASE((_x)) + 0x10)
  41. #define RT3883_PCI_REG_IMBASEBAR0(_x) (RT3883_PCI_REG_BASE((_x)) + 0x18)
  42. #define RT3883_PCI_REG_ID(_x) (RT3883_PCI_REG_BASE((_x)) + 0x30)
  43. #define RT3883_PCI_REG_CLASS(_x) (RT3883_PCI_REG_BASE((_x)) + 0x34)
  44. #define RT3883_PCI_REG_SUBID(_x) (RT3883_PCI_REG_BASE((_x)) + 0x38)
  45. #define RT3883_PCI_REG_STATUS(_x) (RT3883_PCI_REG_BASE((_x)) + 0x50)
  46. #define RT3883_PCI_MODE_NONE 0
  47. #define RT3883_PCI_MODE_PCI BIT(0)
  48. #define RT3883_PCI_MODE_PCIE BIT(1)
  49. #define RT3883_PCI_MODE_BOTH (RT3883_PCI_MODE_PCI | RT3883_PCI_MODE_PCIE)
  50. #define RT3883_PCI_IRQ_COUNT 32
  51. #define RT3883_P2P_BR_DEVNUM 1
  52. struct rt3883_pci_controller {
  53. void __iomem *base;
  54. spinlock_t lock;
  55. struct device_node *intc_of_node;
  56. struct irq_domain *irq_domain;
  57. struct pci_controller pci_controller;
  58. struct resource io_res;
  59. struct resource mem_res;
  60. bool pcie_ready;
  61. };
  62. static inline struct rt3883_pci_controller *
  63. pci_bus_to_rt3883_controller(struct pci_bus *bus)
  64. {
  65. struct pci_controller *hose;
  66. hose = (struct pci_controller *) bus->sysdata;
  67. return container_of(hose, struct rt3883_pci_controller, pci_controller);
  68. }
  69. static inline u32 rt3883_pci_r32(struct rt3883_pci_controller *rpc,
  70. unsigned reg)
  71. {
  72. return ioread32(rpc->base + reg);
  73. }
  74. static inline void rt3883_pci_w32(struct rt3883_pci_controller *rpc,
  75. u32 val, unsigned reg)
  76. {
  77. iowrite32(val, rpc->base + reg);
  78. }
  79. static inline u32 rt3883_pci_get_cfgaddr(unsigned int bus, unsigned int slot,
  80. unsigned int func, unsigned int where)
  81. {
  82. return (bus << 16) | (slot << 11) | (func << 8) | (where & 0xfc) |
  83. 0x80000000;
  84. }
  85. static u32 rt3883_pci_read_cfg32(struct rt3883_pci_controller *rpc,
  86. unsigned bus, unsigned slot,
  87. unsigned func, unsigned reg)
  88. {
  89. unsigned long flags;
  90. u32 address;
  91. u32 ret;
  92. address = rt3883_pci_get_cfgaddr(bus, slot, func, reg);
  93. spin_lock_irqsave(&rpc->lock, flags);
  94. rt3883_pci_w32(rpc, address, RT3883_PCI_REG_CFGADDR);
  95. ret = rt3883_pci_r32(rpc, RT3883_PCI_REG_CFGDATA);
  96. spin_unlock_irqrestore(&rpc->lock, flags);
  97. return ret;
  98. }
  99. static void rt3883_pci_write_cfg32(struct rt3883_pci_controller *rpc,
  100. unsigned bus, unsigned slot,
  101. unsigned func, unsigned reg, u32 val)
  102. {
  103. unsigned long flags;
  104. u32 address;
  105. address = rt3883_pci_get_cfgaddr(bus, slot, func, reg);
  106. spin_lock_irqsave(&rpc->lock, flags);
  107. rt3883_pci_w32(rpc, address, RT3883_PCI_REG_CFGADDR);
  108. rt3883_pci_w32(rpc, val, RT3883_PCI_REG_CFGDATA);
  109. spin_unlock_irqrestore(&rpc->lock, flags);
  110. }
  111. static void rt3883_pci_irq_handler(unsigned int irq, struct irq_desc *desc)
  112. {
  113. struct rt3883_pci_controller *rpc;
  114. u32 pending;
  115. rpc = irq_get_handler_data(irq);
  116. pending = rt3883_pci_r32(rpc, RT3883_PCI_REG_PCIINT) &
  117. rt3883_pci_r32(rpc, RT3883_PCI_REG_PCIENA);
  118. if (!pending) {
  119. spurious_interrupt();
  120. return;
  121. }
  122. while (pending) {
  123. unsigned bit = __ffs(pending);
  124. irq = irq_find_mapping(rpc->irq_domain, bit);
  125. generic_handle_irq(irq);
  126. pending &= ~BIT(bit);
  127. }
  128. }
  129. static void rt3883_pci_irq_unmask(struct irq_data *d)
  130. {
  131. struct rt3883_pci_controller *rpc;
  132. u32 t;
  133. rpc = irq_data_get_irq_chip_data(d);
  134. t = rt3883_pci_r32(rpc, RT3883_PCI_REG_PCIENA);
  135. rt3883_pci_w32(rpc, t | BIT(d->hwirq), RT3883_PCI_REG_PCIENA);
  136. /* flush write */
  137. rt3883_pci_r32(rpc, RT3883_PCI_REG_PCIENA);
  138. }
  139. static void rt3883_pci_irq_mask(struct irq_data *d)
  140. {
  141. struct rt3883_pci_controller *rpc;
  142. u32 t;
  143. rpc = irq_data_get_irq_chip_data(d);
  144. t = rt3883_pci_r32(rpc, RT3883_PCI_REG_PCIENA);
  145. rt3883_pci_w32(rpc, t & ~BIT(d->hwirq), RT3883_PCI_REG_PCIENA);
  146. /* flush write */
  147. rt3883_pci_r32(rpc, RT3883_PCI_REG_PCIENA);
  148. }
  149. static struct irq_chip rt3883_pci_irq_chip = {
  150. .name = "RT3883 PCI",
  151. .irq_mask = rt3883_pci_irq_mask,
  152. .irq_unmask = rt3883_pci_irq_unmask,
  153. .irq_mask_ack = rt3883_pci_irq_mask,
  154. };
  155. static int rt3883_pci_irq_map(struct irq_domain *d, unsigned int irq,
  156. irq_hw_number_t hw)
  157. {
  158. irq_set_chip_and_handler(irq, &rt3883_pci_irq_chip, handle_level_irq);
  159. irq_set_chip_data(irq, d->host_data);
  160. return 0;
  161. }
  162. static const struct irq_domain_ops rt3883_pci_irq_domain_ops = {
  163. .map = rt3883_pci_irq_map,
  164. .xlate = irq_domain_xlate_onecell,
  165. };
  166. static int rt3883_pci_irq_init(struct device *dev,
  167. struct rt3883_pci_controller *rpc)
  168. {
  169. int irq;
  170. irq = irq_of_parse_and_map(rpc->intc_of_node, 0);
  171. if (irq == 0) {
  172. dev_err(dev, "%s has no IRQ",
  173. of_node_full_name(rpc->intc_of_node));
  174. return -EINVAL;
  175. }
  176. /* disable all interrupts */
  177. rt3883_pci_w32(rpc, 0, RT3883_PCI_REG_PCIENA);
  178. rpc->irq_domain =
  179. irq_domain_add_linear(rpc->intc_of_node, RT3883_PCI_IRQ_COUNT,
  180. &rt3883_pci_irq_domain_ops,
  181. rpc);
  182. if (!rpc->irq_domain) {
  183. dev_err(dev, "unable to add IRQ domain\n");
  184. return -ENODEV;
  185. }
  186. irq_set_handler_data(irq, rpc);
  187. irq_set_chained_handler(irq, rt3883_pci_irq_handler);
  188. return 0;
  189. }
  190. static int rt3883_pci_config_read(struct pci_bus *bus, unsigned int devfn,
  191. int where, int size, u32 *val)
  192. {
  193. struct rt3883_pci_controller *rpc;
  194. unsigned long flags;
  195. u32 address;
  196. u32 data;
  197. rpc = pci_bus_to_rt3883_controller(bus);
  198. if (!rpc->pcie_ready && bus->number == 1)
  199. return PCIBIOS_DEVICE_NOT_FOUND;
  200. address = rt3883_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
  201. PCI_FUNC(devfn), where);
  202. spin_lock_irqsave(&rpc->lock, flags);
  203. rt3883_pci_w32(rpc, address, RT3883_PCI_REG_CFGADDR);
  204. data = rt3883_pci_r32(rpc, RT3883_PCI_REG_CFGDATA);
  205. spin_unlock_irqrestore(&rpc->lock, flags);
  206. switch (size) {
  207. case 1:
  208. *val = (data >> ((where & 3) << 3)) & 0xff;
  209. break;
  210. case 2:
  211. *val = (data >> ((where & 3) << 3)) & 0xffff;
  212. break;
  213. case 4:
  214. *val = data;
  215. break;
  216. }
  217. return PCIBIOS_SUCCESSFUL;
  218. }
  219. static int rt3883_pci_config_write(struct pci_bus *bus, unsigned int devfn,
  220. int where, int size, u32 val)
  221. {
  222. struct rt3883_pci_controller *rpc;
  223. unsigned long flags;
  224. u32 address;
  225. u32 data;
  226. rpc = pci_bus_to_rt3883_controller(bus);
  227. if (!rpc->pcie_ready && bus->number == 1)
  228. return PCIBIOS_DEVICE_NOT_FOUND;
  229. address = rt3883_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
  230. PCI_FUNC(devfn), where);
  231. spin_lock_irqsave(&rpc->lock, flags);
  232. rt3883_pci_w32(rpc, address, RT3883_PCI_REG_CFGADDR);
  233. data = rt3883_pci_r32(rpc, RT3883_PCI_REG_CFGDATA);
  234. switch (size) {
  235. case 1:
  236. data = (data & ~(0xff << ((where & 3) << 3))) |
  237. (val << ((where & 3) << 3));
  238. break;
  239. case 2:
  240. data = (data & ~(0xffff << ((where & 3) << 3))) |
  241. (val << ((where & 3) << 3));
  242. break;
  243. case 4:
  244. data = val;
  245. break;
  246. }
  247. rt3883_pci_w32(rpc, data, RT3883_PCI_REG_CFGDATA);
  248. spin_unlock_irqrestore(&rpc->lock, flags);
  249. return PCIBIOS_SUCCESSFUL;
  250. }
  251. static struct pci_ops rt3883_pci_ops = {
  252. .read = rt3883_pci_config_read,
  253. .write = rt3883_pci_config_write,
  254. };
  255. static void rt3883_pci_preinit(struct rt3883_pci_controller *rpc, unsigned mode)
  256. {
  257. u32 syscfg1;
  258. u32 rstctrl;
  259. u32 clkcfg1;
  260. u32 t;
  261. rstctrl = rt_sysc_r32(RT3883_SYSC_REG_RSTCTRL);
  262. syscfg1 = rt_sysc_r32(RT3883_SYSC_REG_SYSCFG1);
  263. clkcfg1 = rt_sysc_r32(RT3883_SYSC_REG_CLKCFG1);
  264. if (mode & RT3883_PCI_MODE_PCIE) {
  265. rstctrl |= RT3883_RSTCTRL_PCIE;
  266. rt_sysc_w32(rstctrl, RT3883_SYSC_REG_RSTCTRL);
  267. /* setup PCI PAD drive mode */
  268. syscfg1 &= ~(0x30);
  269. syscfg1 |= (2 << 4);
  270. rt_sysc_w32(syscfg1, RT3883_SYSC_REG_SYSCFG1);
  271. t = rt_sysc_r32(RT3883_SYSC_REG_PCIE_CLK_GEN0);
  272. t &= ~BIT(31);
  273. rt_sysc_w32(t, RT3883_SYSC_REG_PCIE_CLK_GEN0);
  274. t = rt_sysc_r32(RT3883_SYSC_REG_PCIE_CLK_GEN1);
  275. t &= 0x80ffffff;
  276. rt_sysc_w32(t, RT3883_SYSC_REG_PCIE_CLK_GEN1);
  277. t = rt_sysc_r32(RT3883_SYSC_REG_PCIE_CLK_GEN1);
  278. t |= 0xa << 24;
  279. rt_sysc_w32(t, RT3883_SYSC_REG_PCIE_CLK_GEN1);
  280. t = rt_sysc_r32(RT3883_SYSC_REG_PCIE_CLK_GEN0);
  281. t |= BIT(31);
  282. rt_sysc_w32(t, RT3883_SYSC_REG_PCIE_CLK_GEN0);
  283. msleep(50);
  284. rstctrl &= ~RT3883_RSTCTRL_PCIE;
  285. rt_sysc_w32(rstctrl, RT3883_SYSC_REG_RSTCTRL);
  286. }
  287. syscfg1 |= (RT3883_SYSCFG1_PCIE_RC_MODE | RT3883_SYSCFG1_PCI_HOST_MODE);
  288. clkcfg1 &= ~(RT3883_CLKCFG1_PCI_CLK_EN | RT3883_CLKCFG1_PCIE_CLK_EN);
  289. if (mode & RT3883_PCI_MODE_PCI) {
  290. clkcfg1 |= RT3883_CLKCFG1_PCI_CLK_EN;
  291. rstctrl &= ~RT3883_RSTCTRL_PCI;
  292. }
  293. if (mode & RT3883_PCI_MODE_PCIE) {
  294. clkcfg1 |= RT3883_CLKCFG1_PCIE_CLK_EN;
  295. rstctrl &= ~RT3883_RSTCTRL_PCIE;
  296. }
  297. rt_sysc_w32(syscfg1, RT3883_SYSC_REG_SYSCFG1);
  298. rt_sysc_w32(rstctrl, RT3883_SYSC_REG_RSTCTRL);
  299. rt_sysc_w32(clkcfg1, RT3883_SYSC_REG_CLKCFG1);
  300. msleep(500);
  301. /*
  302. * setup the device number of the P2P bridge
  303. * and de-assert the reset line
  304. */
  305. t = (RT3883_P2P_BR_DEVNUM << RT3883_PCICFG_P2P_BR_DEVNUM_S);
  306. rt3883_pci_w32(rpc, t, RT3883_PCI_REG_PCICFG);
  307. /* flush write */
  308. rt3883_pci_r32(rpc, RT3883_PCI_REG_PCICFG);
  309. msleep(500);
  310. if (mode & RT3883_PCI_MODE_PCIE) {
  311. msleep(500);
  312. t = rt3883_pci_r32(rpc, RT3883_PCI_REG_STATUS(1));
  313. rpc->pcie_ready = t & BIT(0);
  314. if (!rpc->pcie_ready) {
  315. /* reset the PCIe block */
  316. t = rt_sysc_r32(RT3883_SYSC_REG_RSTCTRL);
  317. t |= RT3883_RSTCTRL_PCIE;
  318. rt_sysc_w32(t, RT3883_SYSC_REG_RSTCTRL);
  319. t &= ~RT3883_RSTCTRL_PCIE;
  320. rt_sysc_w32(t, RT3883_SYSC_REG_RSTCTRL);
  321. /* turn off PCIe clock */
  322. t = rt_sysc_r32(RT3883_SYSC_REG_CLKCFG1);
  323. t &= ~RT3883_CLKCFG1_PCIE_CLK_EN;
  324. rt_sysc_w32(t, RT3883_SYSC_REG_CLKCFG1);
  325. t = rt_sysc_r32(RT3883_SYSC_REG_PCIE_CLK_GEN0);
  326. t &= ~0xf000c080;
  327. rt_sysc_w32(t, RT3883_SYSC_REG_PCIE_CLK_GEN0);
  328. }
  329. }
  330. /* enable PCI arbiter */
  331. rt3883_pci_w32(rpc, 0x79, RT3883_PCI_REG_ARBCTL);
  332. }
  333. static int rt3883_pci_probe(struct platform_device *pdev)
  334. {
  335. struct rt3883_pci_controller *rpc;
  336. struct device *dev = &pdev->dev;
  337. struct device_node *np = dev->of_node;
  338. struct resource *res;
  339. struct device_node *child;
  340. u32 val;
  341. int err;
  342. int mode;
  343. rpc = devm_kzalloc(dev, sizeof(*rpc), GFP_KERNEL);
  344. if (!rpc)
  345. return -ENOMEM;
  346. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  347. if (!res)
  348. return -EINVAL;
  349. rpc->base = devm_ioremap_resource(dev, res);
  350. if (IS_ERR(rpc->base))
  351. return PTR_ERR(rpc->base);
  352. /* find the interrupt controller child node */
  353. for_each_child_of_node(np, child) {
  354. if (of_get_property(child, "interrupt-controller", NULL) &&
  355. of_node_get(child)) {
  356. rpc->intc_of_node = child;
  357. break;
  358. }
  359. }
  360. if (!rpc->intc_of_node) {
  361. dev_err(dev, "%s has no %s child node",
  362. of_node_full_name(rpc->intc_of_node),
  363. "interrupt controller");
  364. return -EINVAL;
  365. }
  366. /* find the PCI host bridge child node */
  367. for_each_child_of_node(np, child) {
  368. if (child->type &&
  369. of_node_cmp(child->type, "pci") == 0 &&
  370. of_node_get(child)) {
  371. rpc->pci_controller.of_node = child;
  372. break;
  373. }
  374. }
  375. if (!rpc->pci_controller.of_node) {
  376. dev_err(dev, "%s has no %s child node",
  377. of_node_full_name(rpc->intc_of_node),
  378. "PCI host bridge");
  379. err = -EINVAL;
  380. goto err_put_intc_node;
  381. }
  382. mode = RT3883_PCI_MODE_NONE;
  383. for_each_available_child_of_node(rpc->pci_controller.of_node, child) {
  384. int devfn;
  385. if (!child->type ||
  386. of_node_cmp(child->type, "pci") != 0)
  387. continue;
  388. devfn = of_pci_get_devfn(child);
  389. if (devfn < 0)
  390. continue;
  391. switch (PCI_SLOT(devfn)) {
  392. case 1:
  393. mode |= RT3883_PCI_MODE_PCIE;
  394. break;
  395. case 17:
  396. case 18:
  397. mode |= RT3883_PCI_MODE_PCI;
  398. break;
  399. }
  400. }
  401. if (mode == RT3883_PCI_MODE_NONE) {
  402. dev_err(dev, "unable to determine PCI mode\n");
  403. err = -EINVAL;
  404. goto err_put_hb_node;
  405. }
  406. dev_info(dev, "mode:%s%s\n",
  407. (mode & RT3883_PCI_MODE_PCI) ? " PCI" : "",
  408. (mode & RT3883_PCI_MODE_PCIE) ? " PCIe" : "");
  409. rt3883_pci_preinit(rpc, mode);
  410. rpc->pci_controller.pci_ops = &rt3883_pci_ops;
  411. rpc->pci_controller.io_resource = &rpc->io_res;
  412. rpc->pci_controller.mem_resource = &rpc->mem_res;
  413. /* Load PCI I/O and memory resources from DT */
  414. pci_load_of_ranges(&rpc->pci_controller,
  415. rpc->pci_controller.of_node);
  416. rt3883_pci_w32(rpc, rpc->mem_res.start, RT3883_PCI_REG_MEMBASE);
  417. rt3883_pci_w32(rpc, rpc->io_res.start, RT3883_PCI_REG_IOBASE);
  418. ioport_resource.start = rpc->io_res.start;
  419. ioport_resource.end = rpc->io_res.end;
  420. /* PCI */
  421. rt3883_pci_w32(rpc, 0x03ff0000, RT3883_PCI_REG_BAR0SETUP(0));
  422. rt3883_pci_w32(rpc, RT3883_MEMORY_BASE, RT3883_PCI_REG_IMBASEBAR0(0));
  423. rt3883_pci_w32(rpc, 0x08021814, RT3883_PCI_REG_ID(0));
  424. rt3883_pci_w32(rpc, 0x00800001, RT3883_PCI_REG_CLASS(0));
  425. rt3883_pci_w32(rpc, 0x28801814, RT3883_PCI_REG_SUBID(0));
  426. /* PCIe */
  427. rt3883_pci_w32(rpc, 0x03ff0000, RT3883_PCI_REG_BAR0SETUP(1));
  428. rt3883_pci_w32(rpc, RT3883_MEMORY_BASE, RT3883_PCI_REG_IMBASEBAR0(1));
  429. rt3883_pci_w32(rpc, 0x08021814, RT3883_PCI_REG_ID(1));
  430. rt3883_pci_w32(rpc, 0x06040001, RT3883_PCI_REG_CLASS(1));
  431. rt3883_pci_w32(rpc, 0x28801814, RT3883_PCI_REG_SUBID(1));
  432. err = rt3883_pci_irq_init(dev, rpc);
  433. if (err)
  434. goto err_put_hb_node;
  435. /* PCIe */
  436. val = rt3883_pci_read_cfg32(rpc, 0, 0x01, 0, PCI_COMMAND);
  437. val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
  438. rt3883_pci_write_cfg32(rpc, 0, 0x01, 0, PCI_COMMAND, val);
  439. /* PCI */
  440. val = rt3883_pci_read_cfg32(rpc, 0, 0x00, 0, PCI_COMMAND);
  441. val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
  442. rt3883_pci_write_cfg32(rpc, 0, 0x00, 0, PCI_COMMAND, val);
  443. if (mode == RT3883_PCI_MODE_PCIE) {
  444. rt3883_pci_w32(rpc, 0x03ff0001, RT3883_PCI_REG_BAR0SETUP(0));
  445. rt3883_pci_w32(rpc, 0x03ff0001, RT3883_PCI_REG_BAR0SETUP(1));
  446. rt3883_pci_write_cfg32(rpc, 0, RT3883_P2P_BR_DEVNUM, 0,
  447. PCI_BASE_ADDRESS_0,
  448. RT3883_MEMORY_BASE);
  449. /* flush write */
  450. rt3883_pci_read_cfg32(rpc, 0, RT3883_P2P_BR_DEVNUM, 0,
  451. PCI_BASE_ADDRESS_0);
  452. } else {
  453. rt3883_pci_write_cfg32(rpc, 0, RT3883_P2P_BR_DEVNUM, 0,
  454. PCI_IO_BASE, 0x00000101);
  455. }
  456. register_pci_controller(&rpc->pci_controller);
  457. return 0;
  458. err_put_hb_node:
  459. of_node_put(rpc->pci_controller.of_node);
  460. err_put_intc_node:
  461. of_node_put(rpc->intc_of_node);
  462. return err;
  463. }
  464. int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  465. {
  466. struct of_irq dev_irq;
  467. int err;
  468. int irq;
  469. err = of_irq_map_pci(dev, &dev_irq);
  470. if (err) {
  471. pr_err("pci %s: unable to get irq map, err=%d\n",
  472. pci_name((struct pci_dev *) dev), err);
  473. return 0;
  474. }
  475. irq = irq_create_of_mapping(dev_irq.controller,
  476. dev_irq.specifier,
  477. dev_irq.size);
  478. if (irq == 0)
  479. pr_crit("pci %s: no irq found for pin %u\n",
  480. pci_name((struct pci_dev *) dev), pin);
  481. else
  482. pr_info("pci %s: using irq %d for pin %u\n",
  483. pci_name((struct pci_dev *) dev), irq, pin);
  484. return irq;
  485. }
  486. int pcibios_plat_dev_init(struct pci_dev *dev)
  487. {
  488. return 0;
  489. }
  490. static const struct of_device_id rt3883_pci_ids[] = {
  491. { .compatible = "ralink,rt3883-pci" },
  492. {},
  493. };
  494. MODULE_DEVICE_TABLE(of, rt3883_pci_ids);
  495. static struct platform_driver rt3883_pci_driver = {
  496. .probe = rt3883_pci_probe,
  497. .driver = {
  498. .name = "rt3883-pci",
  499. .owner = THIS_MODULE,
  500. .of_match_table = of_match_ptr(rt3883_pci_ids),
  501. },
  502. };
  503. static int __init rt3883_pci_init(void)
  504. {
  505. return platform_driver_register(&rt3883_pci_driver);
  506. }
  507. postcore_initcall(rt3883_pci_init);