ops-loongson3.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. u_int64_t addr, type;
  17. void *addrp;
  18. int device = PCI_SLOT(devfn);
  19. int function = PCI_FUNC(devfn);
  20. int reg = where & ~3;
  21. addr = (busnum << 16) | (device << 11) | (function << 8) | reg;
  22. if (busnum == 0) {
  23. if (device > 31)
  24. return PCIBIOS_DEVICE_NOT_FOUND;
  25. addrp = (void *)(TO_UNCAC(HT1LO_PCICFG_BASE) | (addr & 0xffff));
  26. type = 0;
  27. } else {
  28. addrp = (void *)(TO_UNCAC(HT1LO_PCICFG_BASE_TP1) | (addr));
  29. type = 0x10000;
  30. }
  31. if (access_type == PCI_ACCESS_WRITE)
  32. writel(*data, addrp);
  33. else {
  34. *data = readl(addrp);
  35. if (*data == 0xffffffff) {
  36. *data = -1;
  37. return PCIBIOS_DEVICE_NOT_FOUND;
  38. }
  39. }
  40. return PCIBIOS_SUCCESSFUL;
  41. }
  42. static int loongson3_pci_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  43. int where, int size, u32 *val)
  44. {
  45. u32 data = 0;
  46. int ret = loongson3_pci_config_access(PCI_ACCESS_READ,
  47. bus, devfn, where, &data);
  48. if (ret != PCIBIOS_SUCCESSFUL)
  49. return ret;
  50. if (size == 1)
  51. *val = (data >> ((where & 3) << 3)) & 0xff;
  52. else if (size == 2)
  53. *val = (data >> ((where & 3) << 3)) & 0xffff;
  54. else
  55. *val = data;
  56. return PCIBIOS_SUCCESSFUL;
  57. }
  58. static int loongson3_pci_pcibios_write(struct pci_bus *bus, unsigned int devfn,
  59. int where, int size, u32 val)
  60. {
  61. u32 data = 0;
  62. int ret;
  63. if (size == 4)
  64. data = val;
  65. else {
  66. ret = loongson3_pci_config_access(PCI_ACCESS_READ,
  67. bus, devfn, where, &data);
  68. if (ret != PCIBIOS_SUCCESSFUL)
  69. return ret;
  70. if (size == 1)
  71. data = (data & ~(0xff << ((where & 3) << 3))) |
  72. (val << ((where & 3) << 3));
  73. else if (size == 2)
  74. data = (data & ~(0xffff << ((where & 3) << 3))) |
  75. (val << ((where & 3) << 3));
  76. }
  77. ret = loongson3_pci_config_access(PCI_ACCESS_WRITE,
  78. bus, devfn, where, &data);
  79. return ret;
  80. }
  81. struct pci_ops loongson_pci_ops = {
  82. .read = loongson3_pci_pcibios_read,
  83. .write = loongson3_pci_pcibios_write
  84. };