pci-vr41xx.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * pci-vr41xx.c, PCI Control Unit routines for the NEC VR4100 series.
  3. *
  4. * Copyright (C) 2001-2003 MontaVista Software Inc.
  5. * Author: Yoichi Yuasa <source@mvista.com>
  6. * Copyright (C) 2004-2008 Yoichi Yuasa <yuasa@linux-mips.org>
  7. * Copyright (C) 2004 by Ralf Baechle (ralf@linux-mips.org)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. /*
  24. * Changes:
  25. * MontaVista Software Inc. <source@mvista.com>
  26. * - New creation, NEC VR4122 and VR4131 are supported.
  27. */
  28. #include <linux/init.h>
  29. #include <linux/pci.h>
  30. #include <linux/types.h>
  31. #include <asm/cpu.h>
  32. #include <asm/io.h>
  33. #include <asm/vr41xx/pci.h>
  34. #include <asm/vr41xx/vr41xx.h>
  35. #include "pci-vr41xx.h"
  36. extern struct pci_ops vr41xx_pci_ops;
  37. static void __iomem *pciu_base;
  38. #define pciu_read(offset) readl(pciu_base + (offset))
  39. #define pciu_write(offset, value) writel((value), pciu_base + (offset))
  40. static struct pci_master_address_conversion pci_master_memory1 = {
  41. .bus_base_address = PCI_MASTER_MEM1_BUS_BASE_ADDRESS,
  42. .address_mask = PCI_MASTER_MEM1_ADDRESS_MASK,
  43. .pci_base_address = PCI_MASTER_MEM1_PCI_BASE_ADDRESS,
  44. };
  45. static struct pci_target_address_conversion pci_target_memory1 = {
  46. .address_mask = PCI_TARGET_MEM1_ADDRESS_MASK,
  47. .bus_base_address = PCI_TARGET_MEM1_BUS_BASE_ADDRESS,
  48. };
  49. static struct pci_master_address_conversion pci_master_io = {
  50. .bus_base_address = PCI_MASTER_IO_BUS_BASE_ADDRESS,
  51. .address_mask = PCI_MASTER_IO_ADDRESS_MASK,
  52. .pci_base_address = PCI_MASTER_IO_PCI_BASE_ADDRESS,
  53. };
  54. static struct pci_mailbox_address pci_mailbox = {
  55. .base_address = PCI_MAILBOX_BASE_ADDRESS,
  56. };
  57. static struct pci_target_address_window pci_target_window1 = {
  58. .base_address = PCI_TARGET_WINDOW1_BASE_ADDRESS,
  59. };
  60. static struct resource pci_mem_resource = {
  61. .name = "PCI Memory resources",
  62. .start = PCI_MEM_RESOURCE_START,
  63. .end = PCI_MEM_RESOURCE_END,
  64. .flags = IORESOURCE_MEM,
  65. };
  66. static struct resource pci_io_resource = {
  67. .name = "PCI I/O resources",
  68. .start = PCI_IO_RESOURCE_START,
  69. .end = PCI_IO_RESOURCE_END,
  70. .flags = IORESOURCE_IO,
  71. };
  72. static struct pci_controller_unit_setup vr41xx_pci_controller_unit_setup = {
  73. .master_memory1 = &pci_master_memory1,
  74. .target_memory1 = &pci_target_memory1,
  75. .master_io = &pci_master_io,
  76. .exclusive_access = CANNOT_LOCK_FROM_DEVICE,
  77. .wait_time_limit_from_irdy_to_trdy = 0,
  78. .mailbox = &pci_mailbox,
  79. .target_window1 = &pci_target_window1,
  80. .master_latency_timer = 0x80,
  81. .retry_limit = 0,
  82. .arbiter_priority_control = PCI_ARBITRATION_MODE_FAIR,
  83. .take_away_gnt_mode = PCI_TAKE_AWAY_GNT_DISABLE,
  84. };
  85. static struct pci_controller vr41xx_pci_controller = {
  86. .pci_ops = &vr41xx_pci_ops,
  87. .mem_resource = &pci_mem_resource,
  88. .io_resource = &pci_io_resource,
  89. };
  90. void __init vr41xx_pciu_setup(struct pci_controller_unit_setup *setup)
  91. {
  92. vr41xx_pci_controller_unit_setup = *setup;
  93. }
  94. static int __init vr41xx_pciu_init(void)
  95. {
  96. struct pci_controller_unit_setup *setup;
  97. struct pci_master_address_conversion *master;
  98. struct pci_target_address_conversion *target;
  99. struct pci_mailbox_address *mailbox;
  100. struct pci_target_address_window *window;
  101. unsigned long vtclock, pci_clock_max;
  102. uint32_t val;
  103. setup = &vr41xx_pci_controller_unit_setup;
  104. if (request_mem_region(PCIU_BASE, PCIU_SIZE, "PCIU") == NULL)
  105. return -EBUSY;
  106. pciu_base = ioremap(PCIU_BASE, PCIU_SIZE);
  107. if (pciu_base == NULL) {
  108. release_mem_region(PCIU_BASE, PCIU_SIZE);
  109. return -EBUSY;
  110. }
  111. /* Disable PCI interrupt */
  112. vr41xx_disable_pciint();
  113. /* Supply VTClock to PCIU */
  114. vr41xx_supply_clock(PCIU_CLOCK);
  115. /* Dummy write, waiting for supply of VTClock. */
  116. vr41xx_disable_pciint();
  117. /* Select PCI clock */
  118. if (setup->pci_clock_max != 0)
  119. pci_clock_max = setup->pci_clock_max;
  120. else
  121. pci_clock_max = PCI_CLOCK_MAX;
  122. vtclock = vr41xx_get_vtclock_frequency();
  123. if (vtclock < pci_clock_max)
  124. pciu_write(PCICLKSELREG, EQUAL_VTCLOCK);
  125. else if ((vtclock / 2) < pci_clock_max)
  126. pciu_write(PCICLKSELREG, HALF_VTCLOCK);
  127. else if (current_cpu_data.processor_id >= PRID_VR4131_REV2_1 &&
  128. (vtclock / 3) < pci_clock_max)
  129. pciu_write(PCICLKSELREG, ONE_THIRD_VTCLOCK);
  130. else if ((vtclock / 4) < pci_clock_max)
  131. pciu_write(PCICLKSELREG, QUARTER_VTCLOCK);
  132. else {
  133. printk(KERN_ERR "PCI Clock is over 33MHz.\n");
  134. iounmap(pciu_base);
  135. return -EINVAL;
  136. }
  137. /* Supply PCI clock by PCI bus */
  138. vr41xx_supply_clock(PCI_CLOCK);
  139. if (setup->master_memory1 != NULL) {
  140. master = setup->master_memory1;
  141. val = IBA(master->bus_base_address) |
  142. MASTER_MSK(master->address_mask) |
  143. WINEN |
  144. PCIA(master->pci_base_address);
  145. pciu_write(PCIMMAW1REG, val);
  146. } else {
  147. val = pciu_read(PCIMMAW1REG);
  148. val &= ~WINEN;
  149. pciu_write(PCIMMAW1REG, val);
  150. }
  151. if (setup->master_memory2 != NULL) {
  152. master = setup->master_memory2;
  153. val = IBA(master->bus_base_address) |
  154. MASTER_MSK(master->address_mask) |
  155. WINEN |
  156. PCIA(master->pci_base_address);
  157. pciu_write(PCIMMAW2REG, val);
  158. } else {
  159. val = pciu_read(PCIMMAW2REG);
  160. val &= ~WINEN;
  161. pciu_write(PCIMMAW2REG, val);
  162. }
  163. if (setup->target_memory1 != NULL) {
  164. target = setup->target_memory1;
  165. val = TARGET_MSK(target->address_mask) |
  166. WINEN |
  167. ITA(target->bus_base_address);
  168. pciu_write(PCITAW1REG, val);
  169. } else {
  170. val = pciu_read(PCITAW1REG);
  171. val &= ~WINEN;
  172. pciu_write(PCITAW1REG, val);
  173. }
  174. if (setup->target_memory2 != NULL) {
  175. target = setup->target_memory2;
  176. val = TARGET_MSK(target->address_mask) |
  177. WINEN |
  178. ITA(target->bus_base_address);
  179. pciu_write(PCITAW2REG, val);
  180. } else {
  181. val = pciu_read(PCITAW2REG);
  182. val &= ~WINEN;
  183. pciu_write(PCITAW2REG, val);
  184. }
  185. if (setup->master_io != NULL) {
  186. master = setup->master_io;
  187. val = IBA(master->bus_base_address) |
  188. MASTER_MSK(master->address_mask) |
  189. WINEN |
  190. PCIIA(master->pci_base_address);
  191. pciu_write(PCIMIOAWREG, val);
  192. } else {
  193. val = pciu_read(PCIMIOAWREG);
  194. val &= ~WINEN;
  195. pciu_write(PCIMIOAWREG, val);
  196. }
  197. if (setup->exclusive_access == CANNOT_LOCK_FROM_DEVICE)
  198. pciu_write(PCIEXACCREG, UNLOCK);
  199. else
  200. pciu_write(PCIEXACCREG, 0);
  201. if (current_cpu_type() == CPU_VR4122)
  202. pciu_write(PCITRDYVREG, TRDYV(setup->wait_time_limit_from_irdy_to_trdy));
  203. pciu_write(LATTIMEREG, MLTIM(setup->master_latency_timer));
  204. if (setup->mailbox != NULL) {
  205. mailbox = setup->mailbox;
  206. val = MBADD(mailbox->base_address) | TYPE_32BITSPACE |
  207. MSI_MEMORY | PREF_APPROVAL;
  208. pciu_write(MAILBAREG, val);
  209. }
  210. if (setup->target_window1) {
  211. window = setup->target_window1;
  212. val = PMBA(window->base_address) | TYPE_32BITSPACE |
  213. MSI_MEMORY | PREF_APPROVAL;
  214. pciu_write(PCIMBA1REG, val);
  215. }
  216. if (setup->target_window2) {
  217. window = setup->target_window2;
  218. val = PMBA(window->base_address) | TYPE_32BITSPACE |
  219. MSI_MEMORY | PREF_APPROVAL;
  220. pciu_write(PCIMBA2REG, val);
  221. }
  222. val = pciu_read(RETVALREG);
  223. val &= ~RTYVAL_MASK;
  224. val |= RTYVAL(setup->retry_limit);
  225. pciu_write(RETVALREG, val);
  226. val = pciu_read(PCIAPCNTREG);
  227. val &= ~(TKYGNT | PAPC);
  228. switch (setup->arbiter_priority_control) {
  229. case PCI_ARBITRATION_MODE_ALTERNATE_0:
  230. val |= PAPC_ALTERNATE_0;
  231. break;
  232. case PCI_ARBITRATION_MODE_ALTERNATE_B:
  233. val |= PAPC_ALTERNATE_B;
  234. break;
  235. default:
  236. val |= PAPC_FAIR;
  237. break;
  238. }
  239. if (setup->take_away_gnt_mode == PCI_TAKE_AWAY_GNT_ENABLE)
  240. val |= TKYGNT_ENABLE;
  241. pciu_write(PCIAPCNTREG, val);
  242. pciu_write(COMMANDREG, PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  243. PCI_COMMAND_MASTER | PCI_COMMAND_PARITY |
  244. PCI_COMMAND_SERR);
  245. /* Clear bus error */
  246. pciu_read(BUSERRADREG);
  247. pciu_write(PCIENREG, PCIU_CONFIG_DONE);
  248. if (setup->mem_resource != NULL)
  249. vr41xx_pci_controller.mem_resource = setup->mem_resource;
  250. if (setup->io_resource != NULL) {
  251. vr41xx_pci_controller.io_resource = setup->io_resource;
  252. } else {
  253. set_io_port_base(IO_PORT_BASE);
  254. ioport_resource.start = IO_PORT_RESOURCE_START;
  255. ioport_resource.end = IO_PORT_RESOURCE_END;
  256. }
  257. if (setup->master_io) {
  258. void __iomem *io_map_base;
  259. struct resource *res = vr41xx_pci_controller.io_resource;
  260. master = setup->master_io;
  261. io_map_base = ioremap(master->bus_base_address,
  262. resource_size(res));
  263. if (!io_map_base)
  264. return -EBUSY;
  265. vr41xx_pci_controller.io_map_base = (unsigned long)io_map_base;
  266. }
  267. register_pci_controller(&vr41xx_pci_controller);
  268. return 0;
  269. }
  270. arch_initcall(vr41xx_pciu_init);