ops-bonito64.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (C) 1999, 2000, 2004 MIPS Technologies, Inc.
  3. * All rights reserved.
  4. * Authors: Carsten Langgaard <carstenl@mips.com>
  5. * Maciej W. Rozycki <macro@mips.com>
  6. *
  7. * This program is free software; you can distribute it and/or modify it
  8. * under the terms of the GNU General Public License (Version 2) as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  19. *
  20. * MIPS boards specific PCI support.
  21. */
  22. #include <linux/types.h>
  23. #include <linux/pci.h>
  24. #include <linux/kernel.h>
  25. #include <asm/mips-boards/bonito64.h>
  26. #define PCI_ACCESS_READ 0
  27. #define PCI_ACCESS_WRITE 1
  28. #define CFG_SPACE_REG(offset) (void *)CKSEG1ADDR(_pcictrl_bonito_pcicfg + (offset))
  29. #define ID_SEL_BEGIN 10
  30. #define MAX_DEV_NUM (31 - ID_SEL_BEGIN)
  31. static int bonito64_pcibios_config_access(unsigned char access_type,
  32. struct pci_bus *bus,
  33. unsigned int devfn, int where,
  34. u32 * data)
  35. {
  36. u32 busnum = bus->number;
  37. u32 addr, type;
  38. u32 dummy;
  39. void *addrp;
  40. int device = PCI_SLOT(devfn);
  41. int function = PCI_FUNC(devfn);
  42. int reg = where & ~3;
  43. if (busnum == 0) {
  44. /* Type 0 configuration for onboard PCI bus */
  45. if (device > MAX_DEV_NUM)
  46. return -1;
  47. addr = (1 << (device + ID_SEL_BEGIN)) | (function << 8) | reg;
  48. type = 0;
  49. } else {
  50. /* Type 1 configuration for offboard PCI bus */
  51. addr = (busnum << 16) | (device << 11) | (function << 8) | reg;
  52. type = 0x10000;
  53. }
  54. /* Clear aborts */
  55. BONITO_PCICMD |= BONITO_PCICMD_MABORT_CLR | BONITO_PCICMD_MTABORT_CLR;
  56. BONITO_PCIMAP_CFG = (addr >> 16) | type;
  57. /* Flush Bonito register block */
  58. dummy = BONITO_PCIMAP_CFG;
  59. mmiowb();
  60. addrp = CFG_SPACE_REG(addr & 0xffff);
  61. if (access_type == PCI_ACCESS_WRITE) {
  62. writel(cpu_to_le32(*data), addrp);
  63. /* Wait till done */
  64. while (BONITO_PCIMSTAT & 0xF);
  65. } else {
  66. *data = le32_to_cpu(readl(addrp));
  67. }
  68. /* Detect Master/Target abort */
  69. if (BONITO_PCICMD & (BONITO_PCICMD_MABORT_CLR |
  70. BONITO_PCICMD_MTABORT_CLR)) {
  71. /* Error occurred */
  72. /* Clear bits */
  73. BONITO_PCICMD |= (BONITO_PCICMD_MABORT_CLR |
  74. BONITO_PCICMD_MTABORT_CLR);
  75. return -1;
  76. }
  77. return 0;
  78. }
  79. /*
  80. * We can't address 8 and 16 bit words directly. Instead we have to
  81. * read/write a 32bit word and mask/modify the data we actually want.
  82. */
  83. static int bonito64_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  84. int where, int size, u32 * val)
  85. {
  86. u32 data = 0;
  87. if ((size == 2) && (where & 1))
  88. return PCIBIOS_BAD_REGISTER_NUMBER;
  89. else if ((size == 4) && (where & 3))
  90. return PCIBIOS_BAD_REGISTER_NUMBER;
  91. if (bonito64_pcibios_config_access(PCI_ACCESS_READ, bus, devfn, where,
  92. &data))
  93. return -1;
  94. if (size == 1)
  95. *val = (data >> ((where & 3) << 3)) & 0xff;
  96. else if (size == 2)
  97. *val = (data >> ((where & 3) << 3)) & 0xffff;
  98. else
  99. *val = data;
  100. return PCIBIOS_SUCCESSFUL;
  101. }
  102. static int bonito64_pcibios_write(struct pci_bus *bus, unsigned int devfn,
  103. int where, int size, u32 val)
  104. {
  105. u32 data = 0;
  106. if ((size == 2) && (where & 1))
  107. return PCIBIOS_BAD_REGISTER_NUMBER;
  108. else if ((size == 4) && (where & 3))
  109. return PCIBIOS_BAD_REGISTER_NUMBER;
  110. if (size == 4)
  111. data = val;
  112. else {
  113. if (bonito64_pcibios_config_access(PCI_ACCESS_READ, bus, devfn,
  114. where, &data))
  115. return -1;
  116. if (size == 1)
  117. data = (data & ~(0xff << ((where & 3) << 3))) |
  118. (val << ((where & 3) << 3));
  119. else if (size == 2)
  120. data = (data & ~(0xffff << ((where & 3) << 3))) |
  121. (val << ((where & 3) << 3));
  122. }
  123. if (bonito64_pcibios_config_access(PCI_ACCESS_WRITE, bus, devfn, where,
  124. &data))
  125. return -1;
  126. return PCIBIOS_SUCCESSFUL;
  127. }
  128. struct pci_ops bonito64_pci_ops = {
  129. .read = bonito64_pcibios_read,
  130. .write = bonito64_pcibios_write
  131. };