ops-bonito64.c 4.3 KB

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