ops-loongson3.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/types.h>
  3. #include <linux/pci.h>
  4. #include <linux/kernel.h>
  5. #include <asm/mips-boards/bonito64.h>
  6. #include <loongson.h>
  7. #define PCI_ACCESS_READ 0
  8. #define PCI_ACCESS_WRITE 1
  9. #define HT1LO_PCICFG_BASE 0x1a000000
  10. #define HT1LO_PCICFG_BASE_TP1 0x1b000000
  11. static int loongson3_pci_config_access(unsigned char access_type,
  12. struct pci_bus *bus, unsigned int devfn,
  13. int where, u32 *data)
  14. {
  15. unsigned char busnum = bus->number;
  16. int function = PCI_FUNC(devfn);
  17. int device = PCI_SLOT(devfn);
  18. int reg = where & ~3;
  19. void *addrp;
  20. u64 addr;
  21. if (where < PCI_CFG_SPACE_SIZE) { /* standard config */
  22. addr = (busnum << 16) | (device << 11) | (function << 8) | reg;
  23. if (busnum == 0) {
  24. if (device > 31)
  25. return PCIBIOS_DEVICE_NOT_FOUND;
  26. addrp = (void *)TO_UNCAC(HT1LO_PCICFG_BASE | addr);
  27. } else {
  28. addrp = (void *)TO_UNCAC(HT1LO_PCICFG_BASE_TP1 | addr);
  29. }
  30. } else if (where < PCI_CFG_SPACE_EXP_SIZE) { /* extended config */
  31. struct pci_dev *rootdev;
  32. rootdev = pci_get_domain_bus_and_slot(0, 0, 0);
  33. if (!rootdev)
  34. return PCIBIOS_DEVICE_NOT_FOUND;
  35. addr = pci_resource_start(rootdev, 3);
  36. if (!addr)
  37. return PCIBIOS_DEVICE_NOT_FOUND;
  38. addr |= busnum << 20 | device << 15 | function << 12 | reg;
  39. addrp = (void *)TO_UNCAC(addr);
  40. } else {
  41. return PCIBIOS_DEVICE_NOT_FOUND;
  42. }
  43. if (access_type == PCI_ACCESS_WRITE)
  44. writel(*data, addrp);
  45. else {
  46. *data = readl(addrp);
  47. if (*data == 0xffffffff) {
  48. *data = -1;
  49. return PCIBIOS_DEVICE_NOT_FOUND;
  50. }
  51. }
  52. return PCIBIOS_SUCCESSFUL;
  53. }
  54. static int loongson3_pci_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  55. int where, int size, u32 *val)
  56. {
  57. u32 data = 0;
  58. int ret = loongson3_pci_config_access(PCI_ACCESS_READ,
  59. bus, devfn, where, &data);
  60. if (ret != PCIBIOS_SUCCESSFUL)
  61. return ret;
  62. if (size == 1)
  63. *val = (data >> ((where & 3) << 3)) & 0xff;
  64. else if (size == 2)
  65. *val = (data >> ((where & 3) << 3)) & 0xffff;
  66. else
  67. *val = data;
  68. return PCIBIOS_SUCCESSFUL;
  69. }
  70. static int loongson3_pci_pcibios_write(struct pci_bus *bus, unsigned int devfn,
  71. int where, int size, u32 val)
  72. {
  73. u32 data = 0;
  74. int ret;
  75. if (size == 4)
  76. data = val;
  77. else {
  78. ret = loongson3_pci_config_access(PCI_ACCESS_READ,
  79. bus, devfn, where, &data);
  80. if (ret != PCIBIOS_SUCCESSFUL)
  81. return ret;
  82. if (size == 1)
  83. data = (data & ~(0xff << ((where & 3) << 3))) |
  84. (val << ((where & 3) << 3));
  85. else if (size == 2)
  86. data = (data & ~(0xffff << ((where & 3) << 3))) |
  87. (val << ((where & 3) << 3));
  88. }
  89. ret = loongson3_pci_config_access(PCI_ACCESS_WRITE,
  90. bus, devfn, where, &data);
  91. return ret;
  92. }
  93. struct pci_ops loongson_pci_ops = {
  94. .read = loongson3_pci_pcibios_read,
  95. .write = loongson3_pci_pcibios_write
  96. };