ops-sh7786.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Generic SH7786 PCI-Express operations.
  4. *
  5. * Copyright (C) 2009 - 2010 Paul Mundt
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/pci.h>
  10. #include <linux/io.h>
  11. #include <linux/spinlock.h>
  12. #include "pcie-sh7786.h"
  13. enum {
  14. PCI_ACCESS_READ,
  15. PCI_ACCESS_WRITE,
  16. };
  17. static int sh7786_pcie_config_access(unsigned char access_type,
  18. struct pci_bus *bus, unsigned int devfn, int where, u32 *data)
  19. {
  20. struct pci_channel *chan = bus->sysdata;
  21. int dev, func, type, reg;
  22. dev = PCI_SLOT(devfn);
  23. func = PCI_FUNC(devfn);
  24. type = !!bus->parent;
  25. reg = where & ~3;
  26. if (bus->number > 255 || dev > 31 || func > 7)
  27. return PCIBIOS_FUNC_NOT_SUPPORTED;
  28. /*
  29. * While each channel has its own memory-mapped extended config
  30. * space, it's generally only accessible when in endpoint mode.
  31. * When in root complex mode, the controller is unable to target
  32. * itself with either type 0 or type 1 accesses, and indeed, any
  33. * controller initiated target transfer to its own config space
  34. * result in a completer abort.
  35. *
  36. * Each channel effectively only supports a single device, but as
  37. * the same channel <-> device access works for any PCI_SLOT()
  38. * value, we cheat a bit here and bind the controller's config
  39. * space to devfn 0 in order to enable self-enumeration. In this
  40. * case the regular PAR/PDR path is sidelined and the mangled
  41. * config access itself is initiated as a SuperHyway transaction.
  42. */
  43. if (pci_is_root_bus(bus)) {
  44. if (dev == 0) {
  45. if (access_type == PCI_ACCESS_READ)
  46. *data = pci_read_reg(chan, PCI_REG(reg));
  47. else
  48. pci_write_reg(chan, *data, PCI_REG(reg));
  49. return PCIBIOS_SUCCESSFUL;
  50. } else if (dev > 1)
  51. return PCIBIOS_DEVICE_NOT_FOUND;
  52. }
  53. /* Clear errors */
  54. pci_write_reg(chan, pci_read_reg(chan, SH4A_PCIEERRFR), SH4A_PCIEERRFR);
  55. /* Set the PIO address */
  56. pci_write_reg(chan, (bus->number << 24) | (dev << 19) |
  57. (func << 16) | reg, SH4A_PCIEPAR);
  58. /* Enable the configuration access */
  59. pci_write_reg(chan, (1 << 31) | (type << 8), SH4A_PCIEPCTLR);
  60. /* Check for errors */
  61. if (pci_read_reg(chan, SH4A_PCIEERRFR) & 0x10)
  62. return PCIBIOS_DEVICE_NOT_FOUND;
  63. /* Check for master and target aborts */
  64. if (pci_read_reg(chan, SH4A_PCIEPCICONF1) & ((1 << 29) | (1 << 28)))
  65. return PCIBIOS_DEVICE_NOT_FOUND;
  66. if (access_type == PCI_ACCESS_READ)
  67. *data = pci_read_reg(chan, SH4A_PCIEPDR);
  68. else
  69. pci_write_reg(chan, *data, SH4A_PCIEPDR);
  70. /* Disable the configuration access */
  71. pci_write_reg(chan, 0, SH4A_PCIEPCTLR);
  72. return PCIBIOS_SUCCESSFUL;
  73. }
  74. static int sh7786_pcie_read(struct pci_bus *bus, unsigned int devfn,
  75. int where, int size, u32 *val)
  76. {
  77. unsigned long flags;
  78. int ret;
  79. u32 data;
  80. if ((size == 2) && (where & 1))
  81. return PCIBIOS_BAD_REGISTER_NUMBER;
  82. else if ((size == 4) && (where & 3))
  83. return PCIBIOS_BAD_REGISTER_NUMBER;
  84. raw_spin_lock_irqsave(&pci_config_lock, flags);
  85. ret = sh7786_pcie_config_access(PCI_ACCESS_READ, bus,
  86. devfn, where, &data);
  87. if (ret != PCIBIOS_SUCCESSFUL) {
  88. *val = 0xffffffff;
  89. goto out;
  90. }
  91. if (size == 1)
  92. *val = (data >> ((where & 3) << 3)) & 0xff;
  93. else if (size == 2)
  94. *val = (data >> ((where & 2) << 3)) & 0xffff;
  95. else
  96. *val = data;
  97. dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x "
  98. "where=0x%04x size=%d val=0x%08lx\n", bus->number,
  99. devfn, where, size, (unsigned long)*val);
  100. out:
  101. raw_spin_unlock_irqrestore(&pci_config_lock, flags);
  102. return ret;
  103. }
  104. static int sh7786_pcie_write(struct pci_bus *bus, unsigned int devfn,
  105. int where, int size, u32 val)
  106. {
  107. unsigned long flags;
  108. int shift, ret;
  109. u32 data;
  110. if ((size == 2) && (where & 1))
  111. return PCIBIOS_BAD_REGISTER_NUMBER;
  112. else if ((size == 4) && (where & 3))
  113. return PCIBIOS_BAD_REGISTER_NUMBER;
  114. raw_spin_lock_irqsave(&pci_config_lock, flags);
  115. ret = sh7786_pcie_config_access(PCI_ACCESS_READ, bus,
  116. devfn, where, &data);
  117. if (ret != PCIBIOS_SUCCESSFUL)
  118. goto out;
  119. dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x "
  120. "where=0x%04x size=%d val=%08lx\n", bus->number,
  121. devfn, where, size, (unsigned long)val);
  122. if (size == 1) {
  123. shift = (where & 3) << 3;
  124. data &= ~(0xff << shift);
  125. data |= ((val & 0xff) << shift);
  126. } else if (size == 2) {
  127. shift = (where & 2) << 3;
  128. data &= ~(0xffff << shift);
  129. data |= ((val & 0xffff) << shift);
  130. } else
  131. data = val;
  132. ret = sh7786_pcie_config_access(PCI_ACCESS_WRITE, bus,
  133. devfn, where, &data);
  134. out:
  135. raw_spin_unlock_irqrestore(&pci_config_lock, flags);
  136. return ret;
  137. }
  138. struct pci_ops sh7786_pci_ops = {
  139. .read = sh7786_pcie_read,
  140. .write = sh7786_pcie_write,
  141. };