ops-sh7786.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. return PCIBIOS_SUCCESSFUL;
  51. }
  52. static int sh7786_pcie_read(struct pci_bus *bus, unsigned int devfn,
  53. int where, int size, u32 *val)
  54. {
  55. unsigned long flags;
  56. int ret;
  57. u32 data;
  58. if ((size == 2) && (where & 1))
  59. return PCIBIOS_BAD_REGISTER_NUMBER;
  60. else if ((size == 4) && (where & 3))
  61. return PCIBIOS_BAD_REGISTER_NUMBER;
  62. spin_lock_irqsave(&sh7786_pcie_lock, flags);
  63. ret = sh7786_pcie_config_access(PCI_ACCESS_READ, bus,
  64. devfn, where, &data);
  65. if (ret != PCIBIOS_SUCCESSFUL) {
  66. *val = 0xffffffff;
  67. goto out;
  68. }
  69. if (size == 1)
  70. *val = (data >> ((where & 3) << 3)) & 0xff;
  71. else if (size == 2)
  72. *val = (data >> ((where & 2) << 3)) & 0xffff;
  73. else
  74. *val = data;
  75. dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x "
  76. "where=0x%04x size=%d val=0x%08lx\n", bus->number,
  77. devfn, where, size, (unsigned long)*val);
  78. out:
  79. spin_unlock_irqrestore(&sh7786_pcie_lock, flags);
  80. return ret;
  81. }
  82. static int sh7786_pcie_write(struct pci_bus *bus, unsigned int devfn,
  83. int where, int size, u32 val)
  84. {
  85. unsigned long flags;
  86. int shift, ret;
  87. u32 data;
  88. if ((size == 2) && (where & 1))
  89. return PCIBIOS_BAD_REGISTER_NUMBER;
  90. else if ((size == 4) && (where & 3))
  91. return PCIBIOS_BAD_REGISTER_NUMBER;
  92. spin_lock_irqsave(&sh7786_pcie_lock, flags);
  93. ret = sh7786_pcie_config_access(PCI_ACCESS_READ, bus,
  94. devfn, where, &data);
  95. if (ret != PCIBIOS_SUCCESSFUL)
  96. goto out;
  97. dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x "
  98. "where=0x%04x size=%d val=%08lx\n", bus->number,
  99. devfn, where, size, (unsigned long)val);
  100. if (size == 1) {
  101. shift = (where & 3) << 3;
  102. data &= ~(0xff << shift);
  103. data |= ((val & 0xff) << shift);
  104. } else if (size == 2) {
  105. shift = (where & 2) << 3;
  106. data &= ~(0xffff << shift);
  107. data |= ((val & 0xffff) << shift);
  108. } else
  109. data = val;
  110. ret = sh7786_pcie_config_access(PCI_ACCESS_WRITE, bus,
  111. devfn, where, &data);
  112. out:
  113. spin_unlock_irqrestore(&sh7786_pcie_lock, flags);
  114. return ret;
  115. }
  116. struct pci_ops sh7786_pci_ops = {
  117. .read = sh7786_pcie_read,
  118. .write = sh7786_pcie_write,
  119. };