ops-loongson2.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * fuloong2e specific PCI support.
  3. *
  4. * Copyright (C) 1999, 2000, 2004 MIPS Technologies, Inc.
  5. * All rights reserved.
  6. * Authors: Carsten Langgaard <carstenl@mips.com>
  7. * Maciej W. Rozycki <macro@mips.com>
  8. *
  9. * Copyright (C) 2009 Lemote Inc.
  10. * Author: Wu Zhangjin <wuzj@lemote.com>
  11. *
  12. * This program is free software; you can distribute it and/or modify it
  13. * under the terms of the GNU General Public License (Version 2) as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/pci.h>
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <loongson.h>
  21. #ifdef CONFIG_CS5536
  22. #include <cs5536/cs5536_pci.h>
  23. #include <cs5536/cs5536.h>
  24. #endif
  25. #define PCI_ACCESS_READ 0
  26. #define PCI_ACCESS_WRITE 1
  27. #define CFG_SPACE_REG(offset) \
  28. (void *)CKSEG1ADDR(LOONGSON_PCICFG_BASE | (offset))
  29. #define ID_SEL_BEGIN 11
  30. #define MAX_DEV_NUM (31 - ID_SEL_BEGIN)
  31. static int loongson_pcibios_config_access(unsigned char access_type,
  32. struct pci_bus *bus,
  33. unsigned int devfn, int where,
  34. u32 *data)
  35. {
  36. u32 busnum = bus->number;
  37. u32 addr, type;
  38. u32 dummy;
  39. void *addrp;
  40. int device = PCI_SLOT(devfn);
  41. int function = PCI_FUNC(devfn);
  42. int reg = where & ~3;
  43. if (busnum == 0) {
  44. /* board-specific part,currently,only fuloong2f,yeeloong2f
  45. * use CS5536, fuloong2e use via686b, gdium has no
  46. * south bridge
  47. */
  48. #ifdef CONFIG_CS5536
  49. /* cs5536_pci_conf_read4/write4() will call _rdmsr/_wrmsr() to
  50. * access the regsters PCI_MSR_ADDR, PCI_MSR_DATA_LO,
  51. * PCI_MSR_DATA_HI, which is bigger than PCI_MSR_CTRL, so, it
  52. * will not go this branch, but the others. so, no calling dead
  53. * loop here.
  54. */
  55. if ((PCI_IDSEL_CS5536 == device) && (reg < PCI_MSR_CTRL)) {
  56. switch (access_type) {
  57. case PCI_ACCESS_READ:
  58. *data = cs5536_pci_conf_read4(function, reg);
  59. break;
  60. case PCI_ACCESS_WRITE:
  61. cs5536_pci_conf_write4(function, reg, *data);
  62. break;
  63. }
  64. return 0;
  65. }
  66. #endif
  67. /* Type 0 configuration for onboard PCI bus */
  68. if (device > MAX_DEV_NUM)
  69. return -1;
  70. addr = (1 << (device + ID_SEL_BEGIN)) | (function << 8) | reg;
  71. type = 0;
  72. } else {
  73. /* Type 1 configuration for offboard PCI bus */
  74. addr = (busnum << 16) | (device << 11) | (function << 8) | reg;
  75. type = 0x10000;
  76. }
  77. /* Clear aborts */
  78. LOONGSON_PCICMD |= LOONGSON_PCICMD_MABORT_CLR | \
  79. LOONGSON_PCICMD_MTABORT_CLR;
  80. LOONGSON_PCIMAP_CFG = (addr >> 16) | type;
  81. /* Flush Bonito register block */
  82. dummy = LOONGSON_PCIMAP_CFG;
  83. mmiowb();
  84. addrp = CFG_SPACE_REG(addr & 0xffff);
  85. if (access_type == PCI_ACCESS_WRITE)
  86. writel(cpu_to_le32(*data), addrp);
  87. else
  88. *data = le32_to_cpu(readl(addrp));
  89. /* Detect Master/Target abort */
  90. if (LOONGSON_PCICMD & (LOONGSON_PCICMD_MABORT_CLR |
  91. LOONGSON_PCICMD_MTABORT_CLR)) {
  92. /* Error occurred */
  93. /* Clear bits */
  94. LOONGSON_PCICMD |= (LOONGSON_PCICMD_MABORT_CLR |
  95. LOONGSON_PCICMD_MTABORT_CLR);
  96. return -1;
  97. }
  98. return 0;
  99. }
  100. /*
  101. * We can't address 8 and 16 bit words directly. Instead we have to
  102. * read/write a 32bit word and mask/modify the data we actually want.
  103. */
  104. static int loongson_pcibios_read(struct pci_bus *bus, unsigned int devfn,
  105. int where, int size, u32 *val)
  106. {
  107. u32 data = 0;
  108. if ((size == 2) && (where & 1))
  109. return PCIBIOS_BAD_REGISTER_NUMBER;
  110. else if ((size == 4) && (where & 3))
  111. return PCIBIOS_BAD_REGISTER_NUMBER;
  112. if (loongson_pcibios_config_access(PCI_ACCESS_READ, bus, devfn, where,
  113. &data))
  114. return -1;
  115. if (size == 1)
  116. *val = (data >> ((where & 3) << 3)) & 0xff;
  117. else if (size == 2)
  118. *val = (data >> ((where & 3) << 3)) & 0xffff;
  119. else
  120. *val = data;
  121. return PCIBIOS_SUCCESSFUL;
  122. }
  123. static int loongson_pcibios_write(struct pci_bus *bus, unsigned int devfn,
  124. int where, int size, u32 val)
  125. {
  126. u32 data = 0;
  127. if ((size == 2) && (where & 1))
  128. return PCIBIOS_BAD_REGISTER_NUMBER;
  129. else if ((size == 4) && (where & 3))
  130. return PCIBIOS_BAD_REGISTER_NUMBER;
  131. if (size == 4)
  132. data = val;
  133. else {
  134. if (loongson_pcibios_config_access(PCI_ACCESS_READ, bus, devfn,
  135. where, &data))
  136. return -1;
  137. if (size == 1)
  138. data = (data & ~(0xff << ((where & 3) << 3))) |
  139. (val << ((where & 3) << 3));
  140. else if (size == 2)
  141. data = (data & ~(0xffff << ((where & 3) << 3))) |
  142. (val << ((where & 3) << 3));
  143. }
  144. if (loongson_pcibios_config_access(PCI_ACCESS_WRITE, bus, devfn, where,
  145. &data))
  146. return -1;
  147. return PCIBIOS_SUCCESSFUL;
  148. }
  149. struct pci_ops loongson_pci_ops = {
  150. .read = loongson_pcibios_read,
  151. .write = loongson_pcibios_write
  152. };
  153. #ifdef CONFIG_CS5536
  154. void _rdmsr(u32 msr, u32 *hi, u32 *lo)
  155. {
  156. struct pci_bus bus = {
  157. .number = PCI_BUS_CS5536
  158. };
  159. u32 devfn = PCI_DEVFN(PCI_IDSEL_CS5536, 0);
  160. loongson_pcibios_write(&bus, devfn, PCI_MSR_ADDR, 4, msr);
  161. loongson_pcibios_read(&bus, devfn, PCI_MSR_DATA_LO, 4, lo);
  162. loongson_pcibios_read(&bus, devfn, PCI_MSR_DATA_HI, 4, hi);
  163. }
  164. EXPORT_SYMBOL(_rdmsr);
  165. void _wrmsr(u32 msr, u32 hi, u32 lo)
  166. {
  167. struct pci_bus bus = {
  168. .number = PCI_BUS_CS5536
  169. };
  170. u32 devfn = PCI_DEVFN(PCI_IDSEL_CS5536, 0);
  171. loongson_pcibios_write(&bus, devfn, PCI_MSR_ADDR, 4, msr);
  172. loongson_pcibios_write(&bus, devfn, PCI_MSR_DATA_LO, 4, lo);
  173. loongson_pcibios_write(&bus, devfn, PCI_MSR_DATA_HI, 4, hi);
  174. }
  175. EXPORT_SYMBOL(_wrmsr);
  176. #endif