pci-rcar-gen2.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * pci-rcar-gen2: internal PCI bus support
  3. *
  4. * Copyright (C) 2013 Renesas Solutions Corp.
  5. * Copyright (C) 2013 Cogent Embedded, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/pci.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/slab.h>
  21. /* AHB-PCI Bridge PCI communication registers */
  22. #define RCAR_AHBPCI_PCICOM_OFFSET 0x800
  23. #define RCAR_PCIAHB_WIN1_CTR_REG (RCAR_AHBPCI_PCICOM_OFFSET + 0x00)
  24. #define RCAR_PCIAHB_WIN2_CTR_REG (RCAR_AHBPCI_PCICOM_OFFSET + 0x04)
  25. #define RCAR_PCIAHB_PREFETCH0 0x0
  26. #define RCAR_PCIAHB_PREFETCH4 0x1
  27. #define RCAR_PCIAHB_PREFETCH8 0x2
  28. #define RCAR_PCIAHB_PREFETCH16 0x3
  29. #define RCAR_AHBPCI_WIN1_CTR_REG (RCAR_AHBPCI_PCICOM_OFFSET + 0x10)
  30. #define RCAR_AHBPCI_WIN2_CTR_REG (RCAR_AHBPCI_PCICOM_OFFSET + 0x14)
  31. #define RCAR_AHBPCI_WIN_CTR_MEM (3 << 1)
  32. #define RCAR_AHBPCI_WIN_CTR_CFG (5 << 1)
  33. #define RCAR_AHBPCI_WIN1_HOST (1 << 30)
  34. #define RCAR_AHBPCI_WIN1_DEVICE (1 << 31)
  35. #define RCAR_PCI_INT_ENABLE_REG (RCAR_AHBPCI_PCICOM_OFFSET + 0x20)
  36. #define RCAR_PCI_INT_STATUS_REG (RCAR_AHBPCI_PCICOM_OFFSET + 0x24)
  37. #define RCAR_PCI_INT_A (1 << 16)
  38. #define RCAR_PCI_INT_B (1 << 17)
  39. #define RCAR_PCI_INT_PME (1 << 19)
  40. #define RCAR_AHB_BUS_CTR_REG (RCAR_AHBPCI_PCICOM_OFFSET + 0x30)
  41. #define RCAR_AHB_BUS_MMODE_HTRANS (1 << 0)
  42. #define RCAR_AHB_BUS_MMODE_BYTE_BURST (1 << 1)
  43. #define RCAR_AHB_BUS_MMODE_WR_INCR (1 << 2)
  44. #define RCAR_AHB_BUS_MMODE_HBUS_REQ (1 << 7)
  45. #define RCAR_AHB_BUS_SMODE_READYCTR (1 << 17)
  46. #define RCAR_AHB_BUS_MODE (RCAR_AHB_BUS_MMODE_HTRANS | \
  47. RCAR_AHB_BUS_MMODE_BYTE_BURST | \
  48. RCAR_AHB_BUS_MMODE_WR_INCR | \
  49. RCAR_AHB_BUS_MMODE_HBUS_REQ | \
  50. RCAR_AHB_BUS_SMODE_READYCTR)
  51. #define RCAR_USBCTR_REG (RCAR_AHBPCI_PCICOM_OFFSET + 0x34)
  52. #define RCAR_USBCTR_USBH_RST (1 << 0)
  53. #define RCAR_USBCTR_PCICLK_MASK (1 << 1)
  54. #define RCAR_USBCTR_PLL_RST (1 << 2)
  55. #define RCAR_USBCTR_DIRPD (1 << 8)
  56. #define RCAR_USBCTR_PCIAHB_WIN2_EN (1 << 9)
  57. #define RCAR_USBCTR_PCIAHB_WIN1_256M (0 << 10)
  58. #define RCAR_USBCTR_PCIAHB_WIN1_512M (1 << 10)
  59. #define RCAR_USBCTR_PCIAHB_WIN1_1G (2 << 10)
  60. #define RCAR_USBCTR_PCIAHB_WIN1_2G (3 << 10)
  61. #define RCAR_USBCTR_PCIAHB_WIN1_MASK (3 << 10)
  62. #define RCAR_PCI_ARBITER_CTR_REG (RCAR_AHBPCI_PCICOM_OFFSET + 0x40)
  63. #define RCAR_PCI_ARBITER_PCIREQ0 (1 << 0)
  64. #define RCAR_PCI_ARBITER_PCIREQ1 (1 << 1)
  65. #define RCAR_PCI_ARBITER_PCIBP_MODE (1 << 12)
  66. #define RCAR_PCI_UNIT_REV_REG (RCAR_AHBPCI_PCICOM_OFFSET + 0x48)
  67. /* Number of internal PCI controllers */
  68. #define RCAR_PCI_NR_CONTROLLERS 3
  69. struct rcar_pci_priv {
  70. struct device *dev;
  71. void __iomem *reg;
  72. struct resource io_res;
  73. struct resource mem_res;
  74. struct resource *cfg_res;
  75. int irq;
  76. };
  77. /* PCI configuration space operations */
  78. static void __iomem *rcar_pci_cfg_base(struct pci_bus *bus, unsigned int devfn,
  79. int where)
  80. {
  81. struct pci_sys_data *sys = bus->sysdata;
  82. struct rcar_pci_priv *priv = sys->private_data;
  83. int slot, val;
  84. if (sys->busnr != bus->number || PCI_FUNC(devfn))
  85. return NULL;
  86. /* Only one EHCI/OHCI device built-in */
  87. slot = PCI_SLOT(devfn);
  88. if (slot > 2)
  89. return NULL;
  90. val = slot ? RCAR_AHBPCI_WIN1_DEVICE | RCAR_AHBPCI_WIN_CTR_CFG :
  91. RCAR_AHBPCI_WIN1_HOST | RCAR_AHBPCI_WIN_CTR_CFG;
  92. iowrite32(val, priv->reg + RCAR_AHBPCI_WIN1_CTR_REG);
  93. return priv->reg + (slot >> 1) * 0x100 + where;
  94. }
  95. static int rcar_pci_read_config(struct pci_bus *bus, unsigned int devfn,
  96. int where, int size, u32 *val)
  97. {
  98. void __iomem *reg = rcar_pci_cfg_base(bus, devfn, where);
  99. if (!reg)
  100. return PCIBIOS_DEVICE_NOT_FOUND;
  101. switch (size) {
  102. case 1:
  103. *val = ioread8(reg);
  104. break;
  105. case 2:
  106. *val = ioread16(reg);
  107. break;
  108. default:
  109. *val = ioread32(reg);
  110. break;
  111. }
  112. return PCIBIOS_SUCCESSFUL;
  113. }
  114. static int rcar_pci_write_config(struct pci_bus *bus, unsigned int devfn,
  115. int where, int size, u32 val)
  116. {
  117. void __iomem *reg = rcar_pci_cfg_base(bus, devfn, where);
  118. if (!reg)
  119. return PCIBIOS_DEVICE_NOT_FOUND;
  120. switch (size) {
  121. case 1:
  122. iowrite8(val, reg);
  123. break;
  124. case 2:
  125. iowrite16(val, reg);
  126. break;
  127. default:
  128. iowrite32(val, reg);
  129. break;
  130. }
  131. return PCIBIOS_SUCCESSFUL;
  132. }
  133. /* PCI interrupt mapping */
  134. static int __init rcar_pci_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  135. {
  136. struct pci_sys_data *sys = dev->bus->sysdata;
  137. struct rcar_pci_priv *priv = sys->private_data;
  138. return priv->irq;
  139. }
  140. /* PCI host controller setup */
  141. static int __init rcar_pci_setup(int nr, struct pci_sys_data *sys)
  142. {
  143. struct rcar_pci_priv *priv = sys->private_data;
  144. void __iomem *reg = priv->reg;
  145. u32 val;
  146. pm_runtime_enable(priv->dev);
  147. pm_runtime_get_sync(priv->dev);
  148. val = ioread32(reg + RCAR_PCI_UNIT_REV_REG);
  149. dev_info(priv->dev, "PCI: bus%u revision %x\n", sys->busnr, val);
  150. /* Disable Direct Power Down State and assert reset */
  151. val = ioread32(reg + RCAR_USBCTR_REG) & ~RCAR_USBCTR_DIRPD;
  152. val |= RCAR_USBCTR_USBH_RST | RCAR_USBCTR_PLL_RST;
  153. iowrite32(val, reg + RCAR_USBCTR_REG);
  154. udelay(4);
  155. /* De-assert reset and set PCIAHB window1 size to 1GB */
  156. val &= ~(RCAR_USBCTR_PCIAHB_WIN1_MASK | RCAR_USBCTR_PCICLK_MASK |
  157. RCAR_USBCTR_USBH_RST | RCAR_USBCTR_PLL_RST);
  158. iowrite32(val | RCAR_USBCTR_PCIAHB_WIN1_1G, reg + RCAR_USBCTR_REG);
  159. /* Configure AHB master and slave modes */
  160. iowrite32(RCAR_AHB_BUS_MODE, reg + RCAR_AHB_BUS_CTR_REG);
  161. /* Configure PCI arbiter */
  162. val = ioread32(reg + RCAR_PCI_ARBITER_CTR_REG);
  163. val |= RCAR_PCI_ARBITER_PCIREQ0 | RCAR_PCI_ARBITER_PCIREQ1 |
  164. RCAR_PCI_ARBITER_PCIBP_MODE;
  165. iowrite32(val, reg + RCAR_PCI_ARBITER_CTR_REG);
  166. /* PCI-AHB mapping: 0x40000000-0x80000000 */
  167. iowrite32(0x40000000 | RCAR_PCIAHB_PREFETCH16,
  168. reg + RCAR_PCIAHB_WIN1_CTR_REG);
  169. /* AHB-PCI mapping: OHCI/EHCI registers */
  170. val = priv->mem_res.start | RCAR_AHBPCI_WIN_CTR_MEM;
  171. iowrite32(val, reg + RCAR_AHBPCI_WIN2_CTR_REG);
  172. /* Enable AHB-PCI bridge PCI configuration access */
  173. iowrite32(RCAR_AHBPCI_WIN1_HOST | RCAR_AHBPCI_WIN_CTR_CFG,
  174. reg + RCAR_AHBPCI_WIN1_CTR_REG);
  175. /* Set PCI-AHB Window1 address */
  176. iowrite32(0x40000000 | PCI_BASE_ADDRESS_MEM_PREFETCH,
  177. reg + PCI_BASE_ADDRESS_1);
  178. /* Set AHB-PCI bridge PCI communication area address */
  179. val = priv->cfg_res->start + RCAR_AHBPCI_PCICOM_OFFSET;
  180. iowrite32(val, reg + PCI_BASE_ADDRESS_0);
  181. val = ioread32(reg + PCI_COMMAND);
  182. val |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY |
  183. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
  184. iowrite32(val, reg + PCI_COMMAND);
  185. /* Enable PCI interrupts */
  186. iowrite32(RCAR_PCI_INT_A | RCAR_PCI_INT_B | RCAR_PCI_INT_PME,
  187. reg + RCAR_PCI_INT_ENABLE_REG);
  188. /* Add PCI resources */
  189. pci_add_resource(&sys->resources, &priv->io_res);
  190. pci_add_resource(&sys->resources, &priv->mem_res);
  191. return 1;
  192. }
  193. static struct pci_ops rcar_pci_ops = {
  194. .read = rcar_pci_read_config,
  195. .write = rcar_pci_write_config,
  196. };
  197. static struct hw_pci rcar_hw_pci __initdata = {
  198. .map_irq = rcar_pci_map_irq,
  199. .ops = &rcar_pci_ops,
  200. .setup = rcar_pci_setup,
  201. };
  202. static int rcar_pci_count __initdata;
  203. static int __init rcar_pci_add_controller(struct rcar_pci_priv *priv)
  204. {
  205. void **private_data;
  206. int count;
  207. if (rcar_hw_pci.nr_controllers < rcar_pci_count)
  208. goto add_priv;
  209. /* (Re)allocate private data pointer array if needed */
  210. count = rcar_pci_count + RCAR_PCI_NR_CONTROLLERS;
  211. private_data = kzalloc(count * sizeof(void *), GFP_KERNEL);
  212. if (!private_data)
  213. return -ENOMEM;
  214. rcar_pci_count = count;
  215. if (rcar_hw_pci.private_data) {
  216. memcpy(private_data, rcar_hw_pci.private_data,
  217. rcar_hw_pci.nr_controllers * sizeof(void *));
  218. kfree(rcar_hw_pci.private_data);
  219. }
  220. rcar_hw_pci.private_data = private_data;
  221. add_priv:
  222. /* Add private data pointer to the array */
  223. rcar_hw_pci.private_data[rcar_hw_pci.nr_controllers++] = priv;
  224. return 0;
  225. }
  226. static int __init rcar_pci_probe(struct platform_device *pdev)
  227. {
  228. struct resource *cfg_res, *mem_res;
  229. struct rcar_pci_priv *priv;
  230. void __iomem *reg;
  231. cfg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  232. reg = devm_ioremap_resource(&pdev->dev, cfg_res);
  233. if (IS_ERR(reg))
  234. return PTR_ERR(reg);
  235. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  236. if (!mem_res || !mem_res->start)
  237. return -ENODEV;
  238. priv = devm_kzalloc(&pdev->dev,
  239. sizeof(struct rcar_pci_priv), GFP_KERNEL);
  240. if (!priv)
  241. return -ENOMEM;
  242. priv->mem_res = *mem_res;
  243. /*
  244. * The controller does not support/use port I/O,
  245. * so setup a dummy port I/O region here.
  246. */
  247. priv->io_res.start = priv->mem_res.start;
  248. priv->io_res.end = priv->mem_res.end;
  249. priv->io_res.flags = IORESOURCE_IO;
  250. priv->cfg_res = cfg_res;
  251. priv->irq = platform_get_irq(pdev, 0);
  252. priv->reg = reg;
  253. priv->dev = &pdev->dev;
  254. return rcar_pci_add_controller(priv);
  255. }
  256. static struct platform_driver rcar_pci_driver = {
  257. .driver = {
  258. .name = "pci-rcar-gen2",
  259. },
  260. };
  261. static int __init rcar_pci_init(void)
  262. {
  263. int retval;
  264. retval = platform_driver_probe(&rcar_pci_driver, rcar_pci_probe);
  265. if (!retval)
  266. pci_common_init(&rcar_hw_pci);
  267. /* Private data pointer array is not needed any more */
  268. kfree(rcar_hw_pci.private_data);
  269. rcar_hw_pci.private_data = NULL;
  270. return retval;
  271. }
  272. subsys_initcall(rcar_pci_init);
  273. MODULE_LICENSE("GPL v2");
  274. MODULE_DESCRIPTION("Renesas R-Car Gen2 internal PCI");
  275. MODULE_AUTHOR("Valentine Barshak <valentine.barshak@cogentembedded.com>");