ops-gt64xxx_pci0.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 1999, 2000, 2004 MIPS Technologies, Inc.
  4. * All rights reserved.
  5. * Authors: Carsten Langgaard <[email protected]>
  6. * Maciej W. Rozycki <[email protected]>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/pci.h>
  10. #include <linux/kernel.h>
  11. #include <asm/gt64120.h>
  12. #define PCI_ACCESS_READ 0
  13. #define PCI_ACCESS_WRITE 1
  14. /*
  15. * PCI configuration cycle AD bus definition
  16. */
  17. /* Type 0 */
  18. #define PCI_CFG_TYPE0_REG_SHF 0
  19. #define PCI_CFG_TYPE0_FUNC_SHF 8
  20. /* Type 1 */
  21. #define PCI_CFG_TYPE1_REG_SHF 0
  22. #define PCI_CFG_TYPE1_FUNC_SHF 8
  23. #define PCI_CFG_TYPE1_DEV_SHF 11
  24. #define PCI_CFG_TYPE1_BUS_SHF 16
  25. static int gt64xxx_pci0_pcibios_config_access(unsigned char access_type,
  26. struct pci_bus *bus, unsigned int devfn, int where, u32 * data)
  27. {
  28. unsigned char busnum = bus->number;
  29. u32 intr;
  30. if ((busnum == 0) && (devfn >= PCI_DEVFN(31, 0)))
  31. return -1; /* Because of a bug in the galileo (for slot 31). */
  32. /* Clear cause register bits */
  33. GT_WRITE(GT_INTRCAUSE_OFS, ~(GT_INTRCAUSE_MASABORT0_BIT |
  34. GT_INTRCAUSE_TARABORT0_BIT));
  35. /* Setup address */
  36. GT_WRITE(GT_PCI0_CFGADDR_OFS,
  37. (busnum << GT_PCI0_CFGADDR_BUSNUM_SHF) |
  38. (devfn << GT_PCI0_CFGADDR_FUNCTNUM_SHF) |
  39. ((where / 4) << GT_PCI0_CFGADDR_REGNUM_SHF) |
  40. GT_PCI0_CFGADDR_CONFIGEN_BIT);
  41. if (access_type == PCI_ACCESS_WRITE) {
  42. if (busnum == 0 && PCI_SLOT(devfn) == 0) {
  43. /*
  44. * The Galileo system controller is acting
  45. * differently than other devices.
  46. */
  47. GT_WRITE(GT_PCI0_CFGDATA_OFS, *data);
  48. } else
  49. __GT_WRITE(GT_PCI0_CFGDATA_OFS, *data);
  50. } else {
  51. if (busnum == 0 && PCI_SLOT(devfn) == 0) {
  52. /*
  53. * The Galileo system controller is acting
  54. * differently than other devices.
  55. */
  56. *data = GT_READ(GT_PCI0_CFGDATA_OFS);
  57. } else
  58. *data = __GT_READ(GT_PCI0_CFGDATA_OFS);
  59. }
  60. /* Check for master or target abort */
  61. intr = GT_READ(GT_INTRCAUSE_OFS);
  62. if (intr & (GT_INTRCAUSE_MASABORT0_BIT | GT_INTRCAUSE_TARABORT0_BIT)) {
  63. /* Error occurred */
  64. /* Clear bits */
  65. GT_WRITE(GT_INTRCAUSE_OFS, ~(GT_INTRCAUSE_MASABORT0_BIT |
  66. GT_INTRCAUSE_TARABORT0_BIT));
  67. return -1;
  68. }
  69. return 0;
  70. }
  71. /*
  72. * We can't address 8 and 16 bit words directly. Instead we have to
  73. * read/write a 32bit word and mask/modify the data we actually want.
  74. */
  75. static int gt64xxx_pci0_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  76. int where, int size, u32 * val)
  77. {
  78. u32 data = 0;
  79. if (gt64xxx_pci0_pcibios_config_access(PCI_ACCESS_READ, bus, devfn,
  80. where, &data))
  81. return PCIBIOS_DEVICE_NOT_FOUND;
  82. if (size == 1)
  83. *val = (data >> ((where & 3) << 3)) & 0xff;
  84. else if (size == 2)
  85. *val = (data >> ((where & 3) << 3)) & 0xffff;
  86. else
  87. *val = data;
  88. return PCIBIOS_SUCCESSFUL;
  89. }
  90. static int gt64xxx_pci0_pcibios_write(struct pci_bus *bus, unsigned int devfn,
  91. int where, int size, u32 val)
  92. {
  93. u32 data = 0;
  94. if (size == 4)
  95. data = val;
  96. else {
  97. if (gt64xxx_pci0_pcibios_config_access(PCI_ACCESS_READ, bus,
  98. devfn, where, &data))
  99. return PCIBIOS_DEVICE_NOT_FOUND;
  100. if (size == 1)
  101. data = (data & ~(0xff << ((where & 3) << 3))) |
  102. (val << ((where & 3) << 3));
  103. else if (size == 2)
  104. data = (data & ~(0xffff << ((where & 3) << 3))) |
  105. (val << ((where & 3) << 3));
  106. }
  107. if (gt64xxx_pci0_pcibios_config_access(PCI_ACCESS_WRITE, bus, devfn,
  108. where, &data))
  109. return PCIBIOS_DEVICE_NOT_FOUND;
  110. return PCIBIOS_SUCCESSFUL;
  111. }
  112. struct pci_ops gt64xxx_pci0_ops = {
  113. .read = gt64xxx_pci0_pcibios_read,
  114. .write = gt64xxx_pci0_pcibios_write
  115. };