ops-bonito64.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. /*
  30. * PCI configuration cycle AD bus definition
  31. */
  32. /* Type 0 */
  33. #define PCI_CFG_TYPE0_REG_SHF 0
  34. #define PCI_CFG_TYPE0_FUNC_SHF 8
  35. /* Type 1 */
  36. #define PCI_CFG_TYPE1_REG_SHF 0
  37. #define PCI_CFG_TYPE1_FUNC_SHF 8
  38. #define PCI_CFG_TYPE1_DEV_SHF 11
  39. #define PCI_CFG_TYPE1_BUS_SHF 16
  40. static int bonito64_pcibios_config_access(unsigned char access_type,
  41. struct pci_bus *bus,
  42. unsigned int devfn, int where,
  43. u32 * data)
  44. {
  45. unsigned char busnum = bus->number;
  46. u32 dummy;
  47. u64 pci_addr;
  48. /* Algorithmics Bonito64 system controller. */
  49. if ((busnum == 0) && (PCI_SLOT(devfn) > 21)) {
  50. /* We number bus 0 devices from 0..21 */
  51. return -1;
  52. }
  53. /* Clear cause register bits */
  54. BONITO_PCICMD |= (BONITO_PCICMD_MABORT_CLR |
  55. BONITO_PCICMD_MTABORT_CLR);
  56. /*
  57. * Setup pattern to be used as PCI "address" for
  58. * Type 0 cycle
  59. */
  60. if (busnum == 0) {
  61. /* IDSEL */
  62. pci_addr = (u64) 1 << (PCI_SLOT(devfn) + 10);
  63. } else {
  64. /* Bus number */
  65. pci_addr = busnum << PCI_CFG_TYPE1_BUS_SHF;
  66. /* Device number */
  67. pci_addr |=
  68. PCI_SLOT(devfn) << PCI_CFG_TYPE1_DEV_SHF;
  69. }
  70. /* Function (same for Type 0/1) */
  71. pci_addr |= PCI_FUNC(devfn) << PCI_CFG_TYPE0_FUNC_SHF;
  72. /* Register number (same for Type 0/1) */
  73. pci_addr |= (where & ~0x3) << PCI_CFG_TYPE0_REG_SHF;
  74. if (busnum == 0) {
  75. /* Type 0 */
  76. BONITO_PCIMAP_CFG = pci_addr >> 16;
  77. } else {
  78. /* Type 1 */
  79. BONITO_PCIMAP_CFG = (pci_addr >> 16) | 0x10000;
  80. }
  81. pci_addr &= 0xffff;
  82. /* Flush Bonito register block */
  83. dummy = BONITO_PCIMAP_CFG;
  84. iob(); /* sync */
  85. /* Perform access */
  86. if (access_type == PCI_ACCESS_WRITE) {
  87. *(volatile u32 *) (_pcictrl_bonito_pcicfg + (u32)pci_addr) = *(u32 *) data;
  88. /* Wait till done */
  89. while (BONITO_PCIMSTAT & 0xF);
  90. } else {
  91. *(u32 *) data = *(volatile u32 *) (_pcictrl_bonito_pcicfg + (u32)pci_addr);
  92. }
  93. /* Detect Master/Target abort */
  94. if (BONITO_PCICMD & (BONITO_PCICMD_MABORT_CLR |
  95. BONITO_PCICMD_MTABORT_CLR)) {
  96. /* Error occurred */
  97. /* Clear bits */
  98. BONITO_PCICMD |= (BONITO_PCICMD_MABORT_CLR |
  99. BONITO_PCICMD_MTABORT_CLR);
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. /*
  105. * We can't address 8 and 16 bit words directly. Instead we have to
  106. * read/write a 32bit word and mask/modify the data we actually want.
  107. */
  108. static int bonito64_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  109. int where, int size, u32 * val)
  110. {
  111. u32 data = 0;
  112. if ((size == 2) && (where & 1))
  113. return PCIBIOS_BAD_REGISTER_NUMBER;
  114. else if ((size == 4) && (where & 3))
  115. return PCIBIOS_BAD_REGISTER_NUMBER;
  116. if (bonito64_pcibios_config_access(PCI_ACCESS_READ, bus, devfn, where,
  117. &data))
  118. return -1;
  119. if (size == 1)
  120. *val = (data >> ((where & 3) << 3)) & 0xff;
  121. else if (size == 2)
  122. *val = (data >> ((where & 3) << 3)) & 0xffff;
  123. else
  124. *val = data;
  125. return PCIBIOS_SUCCESSFUL;
  126. }
  127. static int bonito64_pcibios_write(struct pci_bus *bus, unsigned int devfn,
  128. int where, int size, u32 val)
  129. {
  130. u32 data = 0;
  131. if ((size == 2) && (where & 1))
  132. return PCIBIOS_BAD_REGISTER_NUMBER;
  133. else if ((size == 4) && (where & 3))
  134. return PCIBIOS_BAD_REGISTER_NUMBER;
  135. if (size == 4)
  136. data = val;
  137. else {
  138. if (bonito64_pcibios_config_access(PCI_ACCESS_READ, bus, devfn,
  139. where, &data))
  140. return -1;
  141. if (size == 1)
  142. data = (data & ~(0xff << ((where & 3) << 3))) |
  143. (val << ((where & 3) << 3));
  144. else if (size == 2)
  145. data = (data & ~(0xffff << ((where & 3) << 3))) |
  146. (val << ((where & 3) << 3));
  147. }
  148. if (bonito64_pcibios_config_access(PCI_ACCESS_WRITE, bus, devfn, where,
  149. &data))
  150. return -1;
  151. return PCIBIOS_SUCCESSFUL;
  152. }
  153. struct pci_ops bonito64_pci_ops = {
  154. .read = bonito64_pcibios_read,
  155. .write = bonito64_pcibios_write
  156. };