ops-bonito64.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Carsten Langgaard, carstenl@mips.com
  3. * Copyright (C) 1999, 2000 MIPS Technologies, Inc. All rights reserved.
  4. *
  5. * This program is free software; you can distribute it and/or modify it
  6. * under the terms of the GNU General Public License (Version 2) as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  17. *
  18. * MIPS boards specific PCI support.
  19. */
  20. #include <linux/config.h>
  21. #include <linux/types.h>
  22. #include <linux/pci.h>
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <asm/mips-boards/bonito64.h>
  26. #define PCI_ACCESS_READ 0
  27. #define PCI_ACCESS_WRITE 1
  28. /*
  29. * PCI configuration cycle AD bus definition
  30. */
  31. /* Type 0 */
  32. #define PCI_CFG_TYPE0_REG_SHF 0
  33. #define PCI_CFG_TYPE0_FUNC_SHF 8
  34. /* Type 1 */
  35. #define PCI_CFG_TYPE1_REG_SHF 0
  36. #define PCI_CFG_TYPE1_FUNC_SHF 8
  37. #define PCI_CFG_TYPE1_DEV_SHF 11
  38. #define PCI_CFG_TYPE1_BUS_SHF 16
  39. static int bonito64_pcibios_config_access(unsigned char access_type,
  40. struct pci_bus *bus,
  41. unsigned int devfn, int where,
  42. u32 * data)
  43. {
  44. unsigned char busnum = bus->number;
  45. u32 dummy;
  46. u64 pci_addr;
  47. /* Algorithmics Bonito64 system controller. */
  48. if ((busnum == 0) && (PCI_SLOT(devfn) > 21)) {
  49. /* We number bus 0 devices from 0..21 */
  50. return -1;
  51. }
  52. #ifdef CONFIG_MIPS_BOARDS_GEN
  53. if ((busnum == 0) && (PCI_SLOT(devfn) == 17)) {
  54. /* MIPS Core boards have Bonito connected as device 17 */
  55. return -1;
  56. }
  57. #endif
  58. /* Clear cause register bits */
  59. BONITO_PCICMD |= (BONITO_PCICMD_MABORT_CLR |
  60. BONITO_PCICMD_MTABORT_CLR);
  61. /*
  62. * Setup pattern to be used as PCI "address" for
  63. * Type 0 cycle
  64. */
  65. if (busnum == 0) {
  66. /* IDSEL */
  67. pci_addr = (u64) 1 << (PCI_SLOT(devfn) + 10);
  68. } else {
  69. /* Bus number */
  70. pci_addr = busnum << PCI_CFG_TYPE1_BUS_SHF;
  71. /* Device number */
  72. pci_addr |=
  73. PCI_SLOT(devfn) << PCI_CFG_TYPE1_DEV_SHF;
  74. }
  75. /* Function (same for Type 0/1) */
  76. pci_addr |= PCI_FUNC(devfn) << PCI_CFG_TYPE0_FUNC_SHF;
  77. /* Register number (same for Type 0/1) */
  78. pci_addr |= (where & ~0x3) << PCI_CFG_TYPE0_REG_SHF;
  79. if (busnum == 0) {
  80. /* Type 0 */
  81. BONITO_PCIMAP_CFG = pci_addr >> 16;
  82. } else {
  83. /* Type 1 */
  84. BONITO_PCIMAP_CFG = (pci_addr >> 16) | 0x10000;
  85. }
  86. pci_addr &= 0xffff;
  87. /* Flush Bonito register block */
  88. dummy = BONITO_PCIMAP_CFG;
  89. iob(); /* sync */
  90. /* Perform access */
  91. if (access_type == PCI_ACCESS_WRITE) {
  92. *(volatile u32 *) (_pcictrl_bonito_pcicfg + (u32)pci_addr) = *(u32 *) data;
  93. /* Wait till done */
  94. while (BONITO_PCIMSTAT & 0xF);
  95. } else {
  96. *(u32 *) data = *(volatile u32 *) (_pcictrl_bonito_pcicfg + (u32)pci_addr);
  97. }
  98. /* Detect Master/Target abort */
  99. if (BONITO_PCICMD & (BONITO_PCICMD_MABORT_CLR |
  100. BONITO_PCICMD_MTABORT_CLR)) {
  101. /* Error occurred */
  102. /* Clear bits */
  103. BONITO_PCICMD |= (BONITO_PCICMD_MABORT_CLR |
  104. BONITO_PCICMD_MTABORT_CLR);
  105. return -1;
  106. }
  107. return 0;
  108. }
  109. /*
  110. * We can't address 8 and 16 bit words directly. Instead we have to
  111. * read/write a 32bit word and mask/modify the data we actually want.
  112. */
  113. static int bonito64_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  114. int where, int size, u32 * val)
  115. {
  116. u32 data = 0;
  117. if ((size == 2) && (where & 1))
  118. return PCIBIOS_BAD_REGISTER_NUMBER;
  119. else if ((size == 4) && (where & 3))
  120. return PCIBIOS_BAD_REGISTER_NUMBER;
  121. if (bonito64_pcibios_config_access(PCI_ACCESS_READ, bus, devfn, where,
  122. &data))
  123. return -1;
  124. if (size == 1)
  125. *val = (data >> ((where & 3) << 3)) & 0xff;
  126. else if (size == 2)
  127. *val = (data >> ((where & 3) << 3)) & 0xffff;
  128. else
  129. *val = data;
  130. return PCIBIOS_SUCCESSFUL;
  131. }
  132. static int bonito64_pcibios_write(struct pci_bus *bus, unsigned int devfn,
  133. int where, int size, u32 val)
  134. {
  135. u32 data = 0;
  136. if ((size == 2) && (where & 1))
  137. return PCIBIOS_BAD_REGISTER_NUMBER;
  138. else if ((size == 4) && (where & 3))
  139. return PCIBIOS_BAD_REGISTER_NUMBER;
  140. if (size == 4)
  141. data = val;
  142. else {
  143. if (bonito64_pcibios_config_access(PCI_ACCESS_READ, bus, devfn,
  144. where, &data))
  145. return -1;
  146. if (size == 1)
  147. data = (data & ~(0xff << ((where & 3) << 3))) |
  148. (val << ((where & 3) << 3));
  149. else if (size == 2)
  150. data = (data & ~(0xffff << ((where & 3) << 3))) |
  151. (val << ((where & 3) << 3));
  152. }
  153. if (bonito64_pcibios_config_access(PCI_ACCESS_WRITE, bus, devfn, where,
  154. &data))
  155. return -1;
  156. return PCIBIOS_SUCCESSFUL;
  157. }
  158. struct pci_ops bonito64_pci_ops = {
  159. .read = bonito64_pcibios_read,
  160. .write = bonito64_pcibios_write
  161. };