ops-msc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 1999, 2000, 2004, 2005 MIPS Technologies, Inc.
  4. * All rights reserved.
  5. * Authors: Carsten Langgaard <[email protected]>
  6. * Maciej W. Rozycki <[email protected]>
  7. * Copyright (C) 2005 Ralf Baechle ([email protected])
  8. *
  9. * MIPS boards specific PCI support.
  10. */
  11. #include <linux/types.h>
  12. #include <linux/pci.h>
  13. #include <linux/kernel.h>
  14. #include <asm/mips-boards/msc01_pci.h>
  15. #define PCI_ACCESS_READ 0
  16. #define PCI_ACCESS_WRITE 1
  17. /*
  18. * PCI configuration cycle AD bus definition
  19. */
  20. /* Type 0 */
  21. #define PCI_CFG_TYPE0_REG_SHF 0
  22. #define PCI_CFG_TYPE0_FUNC_SHF 8
  23. /* Type 1 */
  24. #define PCI_CFG_TYPE1_REG_SHF 0
  25. #define PCI_CFG_TYPE1_FUNC_SHF 8
  26. #define PCI_CFG_TYPE1_DEV_SHF 11
  27. #define PCI_CFG_TYPE1_BUS_SHF 16
  28. static int msc_pcibios_config_access(unsigned char access_type,
  29. struct pci_bus *bus, unsigned int devfn, int where, u32 * data)
  30. {
  31. unsigned char busnum = bus->number;
  32. u32 intr;
  33. /* Clear status register bits. */
  34. MSC_WRITE(MSC01_PCI_INTSTAT,
  35. (MSC01_PCI_INTCFG_MA_BIT | MSC01_PCI_INTCFG_TA_BIT));
  36. MSC_WRITE(MSC01_PCI_CFGADDR,
  37. ((busnum << MSC01_PCI_CFGADDR_BNUM_SHF) |
  38. (PCI_SLOT(devfn) << MSC01_PCI_CFGADDR_DNUM_SHF) |
  39. (PCI_FUNC(devfn) << MSC01_PCI_CFGADDR_FNUM_SHF) |
  40. ((where / 4) << MSC01_PCI_CFGADDR_RNUM_SHF)));
  41. /* Perform access */
  42. if (access_type == PCI_ACCESS_WRITE)
  43. MSC_WRITE(MSC01_PCI_CFGDATA, *data);
  44. else
  45. MSC_READ(MSC01_PCI_CFGDATA, *data);
  46. /* Detect Master/Target abort */
  47. MSC_READ(MSC01_PCI_INTSTAT, intr);
  48. if (intr & (MSC01_PCI_INTCFG_MA_BIT | MSC01_PCI_INTCFG_TA_BIT)) {
  49. /* Error occurred */
  50. /* Clear bits */
  51. MSC_WRITE(MSC01_PCI_INTSTAT,
  52. (MSC01_PCI_INTCFG_MA_BIT | MSC01_PCI_INTCFG_TA_BIT));
  53. return -1;
  54. }
  55. return 0;
  56. }
  57. /*
  58. * We can't address 8 and 16 bit words directly. Instead we have to
  59. * read/write a 32bit word and mask/modify the data we actually want.
  60. */
  61. static int msc_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  62. int where, int size, u32 * val)
  63. {
  64. u32 data = 0;
  65. if ((size == 2) && (where & 1))
  66. return PCIBIOS_BAD_REGISTER_NUMBER;
  67. else if ((size == 4) && (where & 3))
  68. return PCIBIOS_BAD_REGISTER_NUMBER;
  69. if (msc_pcibios_config_access(PCI_ACCESS_READ, bus, devfn, where,
  70. &data))
  71. return -1;
  72. if (size == 1)
  73. *val = (data >> ((where & 3) << 3)) & 0xff;
  74. else if (size == 2)
  75. *val = (data >> ((where & 3) << 3)) & 0xffff;
  76. else
  77. *val = data;
  78. return PCIBIOS_SUCCESSFUL;
  79. }
  80. static int msc_pcibios_write(struct pci_bus *bus, unsigned int devfn,
  81. int where, int size, u32 val)
  82. {
  83. u32 data = 0;
  84. if ((size == 2) && (where & 1))
  85. return PCIBIOS_BAD_REGISTER_NUMBER;
  86. else if ((size == 4) && (where & 3))
  87. return PCIBIOS_BAD_REGISTER_NUMBER;
  88. if (size == 4)
  89. data = val;
  90. else {
  91. if (msc_pcibios_config_access(PCI_ACCESS_READ, bus, devfn,
  92. where, &data))
  93. return -1;
  94. if (size == 1)
  95. data = (data & ~(0xff << ((where & 3) << 3))) |
  96. (val << ((where & 3) << 3));
  97. else if (size == 2)
  98. data = (data & ~(0xffff << ((where & 3) << 3))) |
  99. (val << ((where & 3) << 3));
  100. }
  101. if (msc_pcibios_config_access(PCI_ACCESS_WRITE, bus, devfn, where,
  102. &data))
  103. return -1;
  104. return PCIBIOS_SUCCESSFUL;
  105. }
  106. struct pci_ops msc_pci_ops = {
  107. .read = msc_pcibios_read,
  108. .write = msc_pcibios_write
  109. };