ops-sh7786.c 4.7 KB

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