ops-sh7786.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 DEFINE_SPINLOCK(sh7786_pcie_lock);
  21. static int sh7786_pcie_config_access(unsigned char access_type,
  22. struct pci_bus *bus, unsigned int devfn, int where, u32 *data)
  23. {
  24. struct pci_channel *chan = bus->sysdata;
  25. int dev, func, type;
  26. dev = PCI_SLOT(devfn);
  27. func = PCI_FUNC(devfn);
  28. type = !!bus->parent;
  29. if (bus->number > 255 || dev > 31 || func > 7)
  30. return PCIBIOS_FUNC_NOT_SUPPORTED;
  31. if (bus->parent == NULL && dev)
  32. return PCIBIOS_DEVICE_NOT_FOUND;
  33. /* Clear errors */
  34. pci_write_reg(chan, pci_read_reg(chan, SH4A_PCIEERRFR), SH4A_PCIEERRFR);
  35. /* Set the PIO address */
  36. pci_write_reg(chan, (bus->number << 24) | (dev << 19) |
  37. (func << 16) | (where & ~3), SH4A_PCIEPAR);
  38. /* Enable the configuration access */
  39. pci_write_reg(chan, (1 << 31) | (type << 8), SH4A_PCIEPCTLR);
  40. /* Check for errors */
  41. if (pci_read_reg(chan, SH4A_PCIEERRFR) & 0x10)
  42. return PCIBIOS_DEVICE_NOT_FOUND;
  43. /* Check for master and target aborts */
  44. if (pci_read_reg(chan, SH4A_PCIEPCICONF1) & ((1 << 29) | (1 << 28)))
  45. return PCIBIOS_DEVICE_NOT_FOUND;
  46. if (access_type == PCI_ACCESS_READ)
  47. *data = pci_read_reg(chan, SH4A_PCIEPDR);
  48. else
  49. pci_write_reg(chan, *data, SH4A_PCIEPDR);
  50. /* Disable the configuration access */
  51. pci_write_reg(chan, 0, SH4A_PCIEPCTLR);
  52. return PCIBIOS_SUCCESSFUL;
  53. }
  54. static int sh7786_pcie_read(struct pci_bus *bus, unsigned int devfn,
  55. int where, int size, u32 *val)
  56. {
  57. unsigned long flags;
  58. int ret;
  59. u32 data;
  60. if ((size == 2) && (where & 1))
  61. return PCIBIOS_BAD_REGISTER_NUMBER;
  62. else if ((size == 4) && (where & 3))
  63. return PCIBIOS_BAD_REGISTER_NUMBER;
  64. spin_lock_irqsave(&sh7786_pcie_lock, flags);
  65. ret = sh7786_pcie_config_access(PCI_ACCESS_READ, bus,
  66. devfn, where, &data);
  67. if (ret != PCIBIOS_SUCCESSFUL) {
  68. *val = 0xffffffff;
  69. goto out;
  70. }
  71. if (size == 1)
  72. *val = (data >> ((where & 3) << 3)) & 0xff;
  73. else if (size == 2)
  74. *val = (data >> ((where & 2) << 3)) & 0xffff;
  75. else
  76. *val = data;
  77. dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x "
  78. "where=0x%04x size=%d val=0x%08lx\n", bus->number,
  79. devfn, where, size, (unsigned long)*val);
  80. out:
  81. spin_unlock_irqrestore(&sh7786_pcie_lock, flags);
  82. return ret;
  83. }
  84. static int sh7786_pcie_write(struct pci_bus *bus, unsigned int devfn,
  85. int where, int size, u32 val)
  86. {
  87. unsigned long flags;
  88. int shift, ret;
  89. u32 data;
  90. if ((size == 2) && (where & 1))
  91. return PCIBIOS_BAD_REGISTER_NUMBER;
  92. else if ((size == 4) && (where & 3))
  93. return PCIBIOS_BAD_REGISTER_NUMBER;
  94. spin_lock_irqsave(&sh7786_pcie_lock, flags);
  95. ret = sh7786_pcie_config_access(PCI_ACCESS_READ, bus,
  96. devfn, where, &data);
  97. if (ret != PCIBIOS_SUCCESSFUL)
  98. goto out;
  99. dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x "
  100. "where=0x%04x size=%d val=%08lx\n", bus->number,
  101. devfn, where, size, (unsigned long)val);
  102. if (size == 1) {
  103. shift = (where & 3) << 3;
  104. data &= ~(0xff << shift);
  105. data |= ((val & 0xff) << shift);
  106. } else if (size == 2) {
  107. shift = (where & 2) << 3;
  108. data &= ~(0xffff << shift);
  109. data |= ((val & 0xffff) << shift);
  110. } else
  111. data = val;
  112. ret = sh7786_pcie_config_access(PCI_ACCESS_WRITE, bus,
  113. devfn, where, &data);
  114. out:
  115. spin_unlock_irqrestore(&sh7786_pcie_lock, flags);
  116. return ret;
  117. }
  118. struct pci_ops sh7786_pci_ops = {
  119. .read = sh7786_pcie_read,
  120. .write = sh7786_pcie_write,
  121. };