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 <linux/init.h>
  26. #include <asm/mips-boards/bonito64.h>
  27. #define PCI_ACCESS_READ 0
  28. #define PCI_ACCESS_WRITE 1
  29. #define CFG_SPACE_REG(offset) (void *)CKSEG1ADDR(_pcictrl_bonito_pcicfg + (offset))
  30. #define ID_SEL_BEGIN 10
  31. #define MAX_DEV_NUM (31 - ID_SEL_BEGIN)
  32. static int bonito64_pcibios_config_access(unsigned char access_type,
  33. struct pci_bus *bus,
  34. unsigned int devfn, int where,
  35. u32 * data)
  36. {
  37. u32 busnum = bus->number;
  38. u32 addr, type;
  39. u32 dummy;
  40. void *addrp;
  41. int device = PCI_SLOT(devfn);
  42. int function = PCI_FUNC(devfn);
  43. int reg = where & ~3;
  44. if (busnum == 0) {
  45. /* Type 0 configuration for onboard PCI bus */
  46. if (device > MAX_DEV_NUM)
  47. return -1;
  48. addr = (1 << (device + ID_SEL_BEGIN)) | (function << 8) | reg;
  49. type = 0;
  50. } else {
  51. /* Type 1 configuration for offboard PCI bus */
  52. addr = (busnum << 16) | (device << 11) | (function << 8) | reg;
  53. type = 0x10000;
  54. }
  55. /* Clear aborts */
  56. BONITO_PCICMD |= BONITO_PCICMD_MABORT_CLR | BONITO_PCICMD_MTABORT_CLR;
  57. BONITO_PCIMAP_CFG = (addr >> 16) | type;
  58. /* Flush Bonito register block */
  59. dummy = BONITO_PCIMAP_CFG;
  60. mmiowb();
  61. addrp = CFG_SPACE_REG(addr & 0xffff);
  62. if (access_type == PCI_ACCESS_WRITE) {
  63. writel(cpu_to_le32(*data), addrp);
  64. /* Wait till done */
  65. while (BONITO_PCIMSTAT & 0xF);
  66. } else {
  67. *data = le32_to_cpu(readl(addrp));
  68. }
  69. /* Detect Master/Target abort */
  70. if (BONITO_PCICMD & (BONITO_PCICMD_MABORT_CLR |
  71. BONITO_PCICMD_MTABORT_CLR)) {
  72. /* Error occurred */
  73. /* Clear bits */
  74. BONITO_PCICMD |= (BONITO_PCICMD_MABORT_CLR |
  75. BONITO_PCICMD_MTABORT_CLR);
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. /*
  81. * We can't address 8 and 16 bit words directly. Instead we have to
  82. * read/write a 32bit word and mask/modify the data we actually want.
  83. */
  84. static int bonito64_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  85. int where, int size, u32 * val)
  86. {
  87. u32 data = 0;
  88. if ((size == 2) && (where & 1))
  89. return PCIBIOS_BAD_REGISTER_NUMBER;
  90. else if ((size == 4) && (where & 3))
  91. return PCIBIOS_BAD_REGISTER_NUMBER;
  92. if (bonito64_pcibios_config_access(PCI_ACCESS_READ, bus, devfn, where,
  93. &data))
  94. return -1;
  95. if (size == 1)
  96. *val = (data >> ((where & 3) << 3)) & 0xff;
  97. else if (size == 2)
  98. *val = (data >> ((where & 3) << 3)) & 0xffff;
  99. else
  100. *val = data;
  101. return PCIBIOS_SUCCESSFUL;
  102. }
  103. static int bonito64_pcibios_write(struct pci_bus *bus, unsigned int devfn,
  104. int where, int size, u32 val)
  105. {
  106. u32 data = 0;
  107. if ((size == 2) && (where & 1))
  108. return PCIBIOS_BAD_REGISTER_NUMBER;
  109. else if ((size == 4) && (where & 3))
  110. return PCIBIOS_BAD_REGISTER_NUMBER;
  111. if (size == 4)
  112. data = val;
  113. else {
  114. if (bonito64_pcibios_config_access(PCI_ACCESS_READ, bus, devfn,
  115. where, &data))
  116. return -1;
  117. if (size == 1)
  118. data = (data & ~(0xff << ((where & 3) << 3))) |
  119. (val << ((where & 3) << 3));
  120. else if (size == 2)
  121. data = (data & ~(0xffff << ((where & 3) << 3))) |
  122. (val << ((where & 3) << 3));
  123. }
  124. if (bonito64_pcibios_config_access(PCI_ACCESS_WRITE, bus, devfn, where,
  125. &data))
  126. return -1;
  127. return PCIBIOS_SUCCESSFUL;
  128. }
  129. struct pci_ops bonito64_pci_ops = {
  130. .read = bonito64_pcibios_read,
  131. .write = bonito64_pcibios_write
  132. };