ops-loongson2.c 5.4 KB

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