ops-emma2rh.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) NEC Electronics Corporation 2004-2006
  3. *
  4. * This file is based on the arch/mips/pci/ops-vr41xx.c
  5. *
  6. * Copyright 2001 MontaVista Software Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/pci.h>
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <asm/addrspace.h>
  26. #include <asm/emma/emma2rh.h>
  27. #define RTABORT (0x1<<9)
  28. #define RMABORT (0x1<<10)
  29. #define EMMA2RH_PCI_SLOT_NUM 9 /* 0000:09.0 is final PCI device */
  30. /*
  31. * access config space
  32. */
  33. static int check_args(struct pci_bus *bus, u32 devfn, u32 * bus_num)
  34. {
  35. /* check if the bus is top-level */
  36. if (bus->parent != NULL)
  37. *bus_num = bus->number;
  38. else
  39. *bus_num = 0;
  40. if (*bus_num == 0) {
  41. /* Type 0 */
  42. if (PCI_SLOT(devfn) >= 10)
  43. return PCIBIOS_DEVICE_NOT_FOUND;
  44. } else {
  45. /* Type 1 */
  46. if ((*bus_num >= 64) || (PCI_SLOT(devfn) >= 16))
  47. return PCIBIOS_DEVICE_NOT_FOUND;
  48. }
  49. return 0;
  50. }
  51. static inline int set_pci_configuration_address(unsigned char bus_num,
  52. unsigned int devfn, int where)
  53. {
  54. u32 config_win0;
  55. emma2rh_out32(EMMA2RH_PCI_INT, ~RMABORT);
  56. if (bus_num == 0)
  57. /*
  58. * Type 0 configuration
  59. */
  60. config_win0 = (1 << (22 + PCI_SLOT(devfn))) | (5 << 9);
  61. else
  62. /*
  63. * Type 1 configuration
  64. */
  65. config_win0 = (bus_num << 26) | (PCI_SLOT(devfn) << 22) |
  66. (1 << 15) | (5 << 9);
  67. emma2rh_out32(EMMA2RH_PCI_IWIN0_CTR, config_win0);
  68. return 0;
  69. }
  70. static int pci_config_read(struct pci_bus *bus, unsigned int devfn, int where,
  71. int size, uint32_t * val)
  72. {
  73. u32 bus_num;
  74. u32 base = KSEG1ADDR(EMMA2RH_PCI_CONFIG_BASE);
  75. u32 backup_win0;
  76. u32 data;
  77. *val = 0xffffffffU;
  78. if (check_args(bus, devfn, &bus_num) == PCIBIOS_DEVICE_NOT_FOUND)
  79. return PCIBIOS_DEVICE_NOT_FOUND;
  80. backup_win0 = emma2rh_in32(EMMA2RH_PCI_IWIN0_CTR);
  81. if (set_pci_configuration_address(bus_num, devfn, where) < 0)
  82. return PCIBIOS_DEVICE_NOT_FOUND;
  83. data =
  84. *(volatile u32 *)(base + (PCI_FUNC(devfn) << 8) +
  85. (where & 0xfffffffc));
  86. switch (size) {
  87. case 1:
  88. *val = (data >> ((where & 3) << 3)) & 0xffU;
  89. break;
  90. case 2:
  91. *val = (data >> ((where & 2) << 3)) & 0xffffU;
  92. break;
  93. case 4:
  94. *val = data;
  95. break;
  96. default:
  97. emma2rh_out32(EMMA2RH_PCI_IWIN0_CTR, backup_win0);
  98. return PCIBIOS_FUNC_NOT_SUPPORTED;
  99. }
  100. emma2rh_out32(EMMA2RH_PCI_IWIN0_CTR, backup_win0);
  101. if (emma2rh_in32(EMMA2RH_PCI_INT) & RMABORT)
  102. return PCIBIOS_DEVICE_NOT_FOUND;
  103. return PCIBIOS_SUCCESSFUL;
  104. }
  105. static int pci_config_write(struct pci_bus *bus, unsigned int devfn, int where,
  106. int size, u32 val)
  107. {
  108. u32 bus_num;
  109. u32 base = KSEG1ADDR(EMMA2RH_PCI_CONFIG_BASE);
  110. u32 backup_win0;
  111. u32 data;
  112. int shift;
  113. if (check_args(bus, devfn, &bus_num) == PCIBIOS_DEVICE_NOT_FOUND)
  114. return PCIBIOS_DEVICE_NOT_FOUND;
  115. backup_win0 = emma2rh_in32(EMMA2RH_PCI_IWIN0_CTR);
  116. if (set_pci_configuration_address(bus_num, devfn, where) < 0)
  117. return PCIBIOS_DEVICE_NOT_FOUND;
  118. /* read modify write */
  119. data =
  120. *(volatile u32 *)(base + (PCI_FUNC(devfn) << 8) +
  121. (where & 0xfffffffc));
  122. switch (size) {
  123. case 1:
  124. shift = (where & 3) << 3;
  125. data &= ~(0xffU << shift);
  126. data |= ((val & 0xffU) << shift);
  127. break;
  128. case 2:
  129. shift = (where & 2) << 3;
  130. data &= ~(0xffffU << shift);
  131. data |= ((val & 0xffffU) << shift);
  132. break;
  133. case 4:
  134. data = val;
  135. break;
  136. default:
  137. emma2rh_out32(EMMA2RH_PCI_IWIN0_CTR, backup_win0);
  138. return PCIBIOS_FUNC_NOT_SUPPORTED;
  139. }
  140. *(volatile u32 *)(base + (PCI_FUNC(devfn) << 8) +
  141. (where & 0xfffffffc)) = data;
  142. emma2rh_out32(EMMA2RH_PCI_IWIN0_CTR, backup_win0);
  143. if (emma2rh_in32(EMMA2RH_PCI_INT) & RMABORT)
  144. return PCIBIOS_DEVICE_NOT_FOUND;
  145. return PCIBIOS_SUCCESSFUL;
  146. }
  147. struct pci_ops emma2rh_pci_ops = {
  148. .read = pci_config_read,
  149. .write = pci_config_write,
  150. };