ops-bonito64.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <carstenl@mips.com>
  6. * Maciej W. Rozycki <macro@mips.com>
  7. *
  8. * MIPS boards specific PCI support.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/pci.h>
  12. #include <linux/kernel.h>
  13. #include <asm/mips-boards/bonito64.h>
  14. #define PCI_ACCESS_READ 0
  15. #define PCI_ACCESS_WRITE 1
  16. #define CFG_SPACE_REG(offset) (void *)CKSEG1ADDR(_pcictrl_bonito_pcicfg + (offset))
  17. #define ID_SEL_BEGIN 10
  18. #define MAX_DEV_NUM (31 - ID_SEL_BEGIN)
  19. static int bonito64_pcibios_config_access(unsigned char access_type,
  20. struct pci_bus *bus,
  21. unsigned int devfn, int where,
  22. u32 * data)
  23. {
  24. u32 busnum = bus->number;
  25. u32 addr, type;
  26. u32 dummy;
  27. void *addrp;
  28. int device = PCI_SLOT(devfn);
  29. int function = PCI_FUNC(devfn);
  30. int reg = where & ~3;
  31. if (busnum == 0) {
  32. /* Type 0 configuration for onboard PCI bus */
  33. if (device > MAX_DEV_NUM)
  34. return -1;
  35. addr = (1 << (device + ID_SEL_BEGIN)) | (function << 8) | reg;
  36. type = 0;
  37. } else {
  38. /* Type 1 configuration for offboard PCI bus */
  39. addr = (busnum << 16) | (device << 11) | (function << 8) | reg;
  40. type = 0x10000;
  41. }
  42. /* Clear aborts */
  43. BONITO_PCICMD |= BONITO_PCICMD_MABORT_CLR | BONITO_PCICMD_MTABORT_CLR;
  44. BONITO_PCIMAP_CFG = (addr >> 16) | type;
  45. /* Flush Bonito register block */
  46. dummy = BONITO_PCIMAP_CFG;
  47. mmiowb();
  48. addrp = CFG_SPACE_REG(addr & 0xffff);
  49. if (access_type == PCI_ACCESS_WRITE) {
  50. writel(cpu_to_le32(*data), addrp);
  51. /* Wait till done */
  52. while (BONITO_PCIMSTAT & 0xF);
  53. } else {
  54. *data = le32_to_cpu(readl(addrp));
  55. }
  56. /* Detect Master/Target abort */
  57. if (BONITO_PCICMD & (BONITO_PCICMD_MABORT_CLR |
  58. BONITO_PCICMD_MTABORT_CLR)) {
  59. /* Error occurred */
  60. /* Clear bits */
  61. BONITO_PCICMD |= (BONITO_PCICMD_MABORT_CLR |
  62. BONITO_PCICMD_MTABORT_CLR);
  63. return -1;
  64. }
  65. return 0;
  66. }
  67. /*
  68. * We can't address 8 and 16 bit words directly. Instead we have to
  69. * read/write a 32bit word and mask/modify the data we actually want.
  70. */
  71. static int bonito64_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  72. int where, int size, u32 * val)
  73. {
  74. u32 data = 0;
  75. if ((size == 2) && (where & 1))
  76. return PCIBIOS_BAD_REGISTER_NUMBER;
  77. else if ((size == 4) && (where & 3))
  78. return PCIBIOS_BAD_REGISTER_NUMBER;
  79. if (bonito64_pcibios_config_access(PCI_ACCESS_READ, bus, devfn, where,
  80. &data))
  81. return -1;
  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 bonito64_pcibios_write(struct pci_bus *bus, unsigned int devfn,
  91. int where, int size, u32 val)
  92. {
  93. u32 data = 0;
  94. if ((size == 2) && (where & 1))
  95. return PCIBIOS_BAD_REGISTER_NUMBER;
  96. else if ((size == 4) && (where & 3))
  97. return PCIBIOS_BAD_REGISTER_NUMBER;
  98. if (size == 4)
  99. data = val;
  100. else {
  101. if (bonito64_pcibios_config_access(PCI_ACCESS_READ, bus, devfn,
  102. where, &data))
  103. return -1;
  104. if (size == 1)
  105. data = (data & ~(0xff << ((where & 3) << 3))) |
  106. (val << ((where & 3) << 3));
  107. else if (size == 2)
  108. data = (data & ~(0xffff << ((where & 3) << 3))) |
  109. (val << ((where & 3) << 3));
  110. }
  111. if (bonito64_pcibios_config_access(PCI_ACCESS_WRITE, bus, devfn, where,
  112. &data))
  113. return -1;
  114. return PCIBIOS_SUCCESSFUL;
  115. }
  116. struct pci_ops bonito64_pci_ops = {
  117. .read = bonito64_pcibios_read,
  118. .write = bonito64_pcibios_write
  119. };