ops-msc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 1999, 2000, 2004, 2005 MIPS Technologies, Inc.
  3. * All rights reserved.
  4. * Authors: Carsten Langgaard <carstenl@mips.com>
  5. * Maciej W. Rozycki <macro@mips.com>
  6. * Copyright (C) 2005 Ralf Baechle (ralf@linux-mips.org)
  7. *
  8. * This program is free software; you can distribute it and/or modify it
  9. * under the terms of the GNU General Public License (Version 2) as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  20. *
  21. * MIPS boards specific PCI support.
  22. *
  23. */
  24. #include <linux/config.h>
  25. #include <linux/types.h>
  26. #include <linux/pci.h>
  27. #include <linux/kernel.h>
  28. #include <linux/init.h>
  29. #include <asm/mips-boards/msc01_pci.h>
  30. #define PCI_ACCESS_READ 0
  31. #define PCI_ACCESS_WRITE 1
  32. /*
  33. * PCI configuration cycle AD bus definition
  34. */
  35. /* Type 0 */
  36. #define PCI_CFG_TYPE0_REG_SHF 0
  37. #define PCI_CFG_TYPE0_FUNC_SHF 8
  38. /* Type 1 */
  39. #define PCI_CFG_TYPE1_REG_SHF 0
  40. #define PCI_CFG_TYPE1_FUNC_SHF 8
  41. #define PCI_CFG_TYPE1_DEV_SHF 11
  42. #define PCI_CFG_TYPE1_BUS_SHF 16
  43. static int msc_pcibios_config_access(unsigned char access_type,
  44. struct pci_bus *bus, unsigned int devfn, int where, u32 * data)
  45. {
  46. unsigned char busnum = bus->number;
  47. unsigned char type;
  48. u32 intr;
  49. #ifdef CONFIG_MIPS_BOARDS_GEN
  50. if ((busnum == 0) && (PCI_SLOT(devfn) == 17)) {
  51. /* MIPS Core boards have SOCit connected as device 17 */
  52. return -1;
  53. }
  54. #endif
  55. /* Clear status register bits. */
  56. MSC_WRITE(MSC01_PCI_INTSTAT,
  57. (MSC01_PCI_INTCFG_MA_BIT | MSC01_PCI_INTCFG_TA_BIT));
  58. /* Setup address */
  59. if (busnum == 0)
  60. type = 0; /* Type 0 */
  61. else
  62. type = 1; /* Type 1 */
  63. MSC_WRITE(MSC01_PCI_CFGADDR,
  64. ((busnum << MSC01_PCI_CFGADDR_BNUM_SHF) |
  65. (PCI_SLOT(devfn) << MSC01_PCI_CFGADDR_DNUM_SHF)
  66. | (PCI_FUNC(devfn) <<
  67. MSC01_PCI_CFGADDR_FNUM_SHF) | ((where /
  68. 4) <<
  69. MSC01_PCI_CFGADDR_RNUM_SHF)
  70. | (type)));
  71. /* Perform access */
  72. if (access_type == PCI_ACCESS_WRITE)
  73. MSC_WRITE(MSC01_PCI_CFGDATA, *data);
  74. else
  75. MSC_READ(MSC01_PCI_CFGDATA, *data);
  76. /* Detect Master/Target abort */
  77. MSC_READ(MSC01_PCI_INTSTAT, intr);
  78. if (intr & (MSC01_PCI_INTCFG_MA_BIT |
  79. MSC01_PCI_INTCFG_TA_BIT)) {
  80. /* Error occurred */
  81. /* Clear bits */
  82. MSC_READ(MSC01_PCI_INTSTAT, intr);
  83. MSC_WRITE(MSC01_PCI_INTSTAT,
  84. (MSC01_PCI_INTCFG_MA_BIT |
  85. MSC01_PCI_INTCFG_TA_BIT));
  86. return -1;
  87. }
  88. return 0;
  89. }
  90. /*
  91. * We can't address 8 and 16 bit words directly. Instead we have to
  92. * read/write a 32bit word and mask/modify the data we actually want.
  93. */
  94. static int msc_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  95. int where, int size, u32 * val)
  96. {
  97. u32 data = 0;
  98. if ((size == 2) && (where & 1))
  99. return PCIBIOS_BAD_REGISTER_NUMBER;
  100. else if ((size == 4) && (where & 3))
  101. return PCIBIOS_BAD_REGISTER_NUMBER;
  102. if (msc_pcibios_config_access(PCI_ACCESS_READ, bus, devfn, where,
  103. &data))
  104. return -1;
  105. if (size == 1)
  106. *val = (data >> ((where & 3) << 3)) & 0xff;
  107. else if (size == 2)
  108. *val = (data >> ((where & 3) << 3)) & 0xffff;
  109. else
  110. *val = data;
  111. return PCIBIOS_SUCCESSFUL;
  112. }
  113. static int msc_pcibios_write(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 (size == 4)
  122. data = val;
  123. else {
  124. if (msc_pcibios_config_access(PCI_ACCESS_READ, bus, devfn,
  125. where, &data))
  126. return -1;
  127. if (size == 1)
  128. data = (data & ~(0xff << ((where & 3) << 3))) |
  129. (val << ((where & 3) << 3));
  130. else if (size == 2)
  131. data = (data & ~(0xffff << ((where & 3) << 3))) |
  132. (val << ((where & 3) << 3));
  133. }
  134. if (msc_pcibios_config_access(PCI_ACCESS_WRITE, bus, devfn, where,
  135. &data))
  136. return -1;
  137. return PCIBIOS_SUCCESSFUL;
  138. }
  139. struct pci_ops msc_pci_ops = {
  140. .read = msc_pcibios_read,
  141. .write = msc_pcibios_write
  142. };