ops-msc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/types.h>
  25. #include <linux/pci.h>
  26. #include <linux/kernel.h>
  27. #include <asm/mips-boards/msc01_pci.h>
  28. #define PCI_ACCESS_READ 0
  29. #define PCI_ACCESS_WRITE 1
  30. /*
  31. * PCI configuration cycle AD bus definition
  32. */
  33. /* Type 0 */
  34. #define PCI_CFG_TYPE0_REG_SHF 0
  35. #define PCI_CFG_TYPE0_FUNC_SHF 8
  36. /* Type 1 */
  37. #define PCI_CFG_TYPE1_REG_SHF 0
  38. #define PCI_CFG_TYPE1_FUNC_SHF 8
  39. #define PCI_CFG_TYPE1_DEV_SHF 11
  40. #define PCI_CFG_TYPE1_BUS_SHF 16
  41. static int msc_pcibios_config_access(unsigned char access_type,
  42. struct pci_bus *bus, unsigned int devfn, int where, u32 * data)
  43. {
  44. unsigned char busnum = bus->number;
  45. u32 intr;
  46. /* Clear status register bits. */
  47. MSC_WRITE(MSC01_PCI_INTSTAT,
  48. (MSC01_PCI_INTCFG_MA_BIT | MSC01_PCI_INTCFG_TA_BIT));
  49. MSC_WRITE(MSC01_PCI_CFGADDR,
  50. ((busnum << MSC01_PCI_CFGADDR_BNUM_SHF) |
  51. (PCI_SLOT(devfn) << MSC01_PCI_CFGADDR_DNUM_SHF) |
  52. (PCI_FUNC(devfn) << MSC01_PCI_CFGADDR_FNUM_SHF) |
  53. ((where / 4) << MSC01_PCI_CFGADDR_RNUM_SHF)));
  54. /* Perform access */
  55. if (access_type == PCI_ACCESS_WRITE)
  56. MSC_WRITE(MSC01_PCI_CFGDATA, *data);
  57. else
  58. MSC_READ(MSC01_PCI_CFGDATA, *data);
  59. /* Detect Master/Target abort */
  60. MSC_READ(MSC01_PCI_INTSTAT, intr);
  61. if (intr & (MSC01_PCI_INTCFG_MA_BIT | MSC01_PCI_INTCFG_TA_BIT)) {
  62. /* Error occurred */
  63. /* Clear bits */
  64. MSC_WRITE(MSC01_PCI_INTSTAT,
  65. (MSC01_PCI_INTCFG_MA_BIT | MSC01_PCI_INTCFG_TA_BIT));
  66. return -1;
  67. }
  68. return 0;
  69. }
  70. /*
  71. * We can't address 8 and 16 bit words directly. Instead we have to
  72. * read/write a 32bit word and mask/modify the data we actually want.
  73. */
  74. static int msc_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  75. int where, int size, u32 * val)
  76. {
  77. u32 data = 0;
  78. if ((size == 2) && (where & 1))
  79. return PCIBIOS_BAD_REGISTER_NUMBER;
  80. else if ((size == 4) && (where & 3))
  81. return PCIBIOS_BAD_REGISTER_NUMBER;
  82. if (msc_pcibios_config_access(PCI_ACCESS_READ, bus, devfn, where,
  83. &data))
  84. return -1;
  85. if (size == 1)
  86. *val = (data >> ((where & 3) << 3)) & 0xff;
  87. else if (size == 2)
  88. *val = (data >> ((where & 3) << 3)) & 0xffff;
  89. else
  90. *val = data;
  91. return PCIBIOS_SUCCESSFUL;
  92. }
  93. static int msc_pcibios_write(struct pci_bus *bus, unsigned int devfn,
  94. int where, int size, u32 val)
  95. {
  96. u32 data = 0;
  97. if ((size == 2) && (where & 1))
  98. return PCIBIOS_BAD_REGISTER_NUMBER;
  99. else if ((size == 4) && (where & 3))
  100. return PCIBIOS_BAD_REGISTER_NUMBER;
  101. if (size == 4)
  102. data = val;
  103. else {
  104. if (msc_pcibios_config_access(PCI_ACCESS_READ, bus, devfn,
  105. where, &data))
  106. return -1;
  107. if (size == 1)
  108. data = (data & ~(0xff << ((where & 3) << 3))) |
  109. (val << ((where & 3) << 3));
  110. else if (size == 2)
  111. data = (data & ~(0xffff << ((where & 3) << 3))) |
  112. (val << ((where & 3) << 3));
  113. }
  114. if (msc_pcibios_config_access(PCI_ACCESS_WRITE, bus, devfn, where,
  115. &data))
  116. return -1;
  117. return PCIBIOS_SUCCESSFUL;
  118. }
  119. struct pci_ops msc_pci_ops = {
  120. .read = msc_pcibios_read,
  121. .write = msc_pcibios_write
  122. };