olpc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Low-level PCI config space access for OLPC systems who lack the VSA
  4. * PCI virtualization software.
  5. *
  6. * Copyright © 2006 Advanced Micro Devices, Inc.
  7. *
  8. * The AMD Geode chipset (ie: GX2 processor, cs5536 I/O companion device)
  9. * has some I/O functions (display, southbridge, sound, USB HCIs, etc)
  10. * that more or less behave like PCI devices, but the hardware doesn't
  11. * directly implement the PCI configuration space headers. AMD provides
  12. * "VSA" (Virtual System Architecture) software that emulates PCI config
  13. * space for these devices, by trapping I/O accesses to PCI config register
  14. * (CF8/CFC) and running some code in System Management Mode interrupt state.
  15. * On the OLPC platform, we don't want to use that VSA code because
  16. * (a) it slows down suspend/resume, and (b) recompiling it requires special
  17. * compilers that are hard to get. So instead of letting the complex VSA
  18. * code simulate the PCI config registers for the on-chip devices, we
  19. * just simulate them the easy way, by inserting the code into the
  20. * pci_write_config and pci_read_config path. Most of the config registers
  21. * are read-only anyway, so the bulk of the simulation is just table lookup.
  22. */
  23. #include <linux/pci.h>
  24. #include <linux/init.h>
  25. #include <asm/olpc.h>
  26. #include <asm/geode.h>
  27. #include <asm/pci_x86.h>
  28. /*
  29. * In the tables below, the first two line (8 longwords) are the
  30. * size masks that are used when the higher level PCI code determines
  31. * the size of the region by writing ~0 to a base address register
  32. * and reading back the result.
  33. *
  34. * The following lines are the values that are read during normal
  35. * PCI config access cycles, i.e. not after just having written
  36. * ~0 to a base address register.
  37. */
  38. static const uint32_t lxnb_hdr[] = { /* dev 1 function 0 - devfn = 8 */
  39. 0x0, 0x0, 0x0, 0x0,
  40. 0x0, 0x0, 0x0, 0x0,
  41. 0x281022, 0x2200005, 0x6000021, 0x80f808, /* AMD Vendor ID */
  42. 0x0, 0x0, 0x0, 0x0, /* No virtual registers, hence no BAR */
  43. 0x0, 0x0, 0x0, 0x28100b,
  44. 0x0, 0x0, 0x0, 0x0,
  45. 0x0, 0x0, 0x0, 0x0,
  46. 0x0, 0x0, 0x0, 0x0,
  47. 0x0, 0x0, 0x0, 0x0,
  48. };
  49. static const uint32_t gxnb_hdr[] = { /* dev 1 function 0 - devfn = 8 */
  50. 0xfffffffd, 0x0, 0x0, 0x0,
  51. 0x0, 0x0, 0x0, 0x0,
  52. 0x28100b, 0x2200005, 0x6000021, 0x80f808, /* NSC Vendor ID */
  53. 0xac1d, 0x0, 0x0, 0x0, /* I/O BAR - base of virtual registers */
  54. 0x0, 0x0, 0x0, 0x28100b,
  55. 0x0, 0x0, 0x0, 0x0,
  56. 0x0, 0x0, 0x0, 0x0,
  57. 0x0, 0x0, 0x0, 0x0,
  58. 0x0, 0x0, 0x0, 0x0,
  59. };
  60. static const uint32_t lxfb_hdr[] = { /* dev 1 function 1 - devfn = 9 */
  61. 0xff000008, 0xffffc000, 0xffffc000, 0xffffc000,
  62. 0xffffc000, 0x0, 0x0, 0x0,
  63. 0x20811022, 0x2200003, 0x3000000, 0x0, /* AMD Vendor ID */
  64. 0xfd000000, 0xfe000000, 0xfe004000, 0xfe008000, /* FB, GP, VG, DF */
  65. 0xfe00c000, 0x0, 0x0, 0x30100b, /* VIP */
  66. 0x0, 0x0, 0x0, 0x10e, /* INTA, IRQ14 for graphics accel */
  67. 0x0, 0x0, 0x0, 0x0,
  68. 0x3d0, 0x3c0, 0xa0000, 0x0, /* VG IO, VG IO, EGA FB, MONO FB */
  69. 0x0, 0x0, 0x0, 0x0,
  70. };
  71. static const uint32_t gxfb_hdr[] = { /* dev 1 function 1 - devfn = 9 */
  72. 0xff800008, 0xffffc000, 0xffffc000, 0xffffc000,
  73. 0x0, 0x0, 0x0, 0x0,
  74. 0x30100b, 0x2200003, 0x3000000, 0x0, /* NSC Vendor ID */
  75. 0xfd000000, 0xfe000000, 0xfe004000, 0xfe008000, /* FB, GP, VG, DF */
  76. 0x0, 0x0, 0x0, 0x30100b,
  77. 0x0, 0x0, 0x0, 0x0,
  78. 0x0, 0x0, 0x0, 0x0,
  79. 0x3d0, 0x3c0, 0xa0000, 0x0, /* VG IO, VG IO, EGA FB, MONO FB */
  80. 0x0, 0x0, 0x0, 0x0,
  81. };
  82. static const uint32_t aes_hdr[] = { /* dev 1 function 2 - devfn = 0xa */
  83. 0xffffc000, 0x0, 0x0, 0x0,
  84. 0x0, 0x0, 0x0, 0x0,
  85. 0x20821022, 0x2a00006, 0x10100000, 0x8, /* NSC Vendor ID */
  86. 0xfe010000, 0x0, 0x0, 0x0, /* AES registers */
  87. 0x0, 0x0, 0x0, 0x20821022,
  88. 0x0, 0x0, 0x0, 0x0,
  89. 0x0, 0x0, 0x0, 0x0,
  90. 0x0, 0x0, 0x0, 0x0,
  91. 0x0, 0x0, 0x0, 0x0,
  92. };
  93. static const uint32_t isa_hdr[] = { /* dev f function 0 - devfn = 78 */
  94. 0xfffffff9, 0xffffff01, 0xffffffc1, 0xffffffe1,
  95. 0xffffff81, 0xffffffc1, 0x0, 0x0,
  96. 0x20901022, 0x2a00049, 0x6010003, 0x802000,
  97. 0x18b1, 0x1001, 0x1801, 0x1881, /* SMB-8 GPIO-256 MFGPT-64 IRQ-32 */
  98. 0x1401, 0x1841, 0x0, 0x20901022, /* PMS-128 ACPI-64 */
  99. 0x0, 0x0, 0x0, 0x0,
  100. 0x0, 0x0, 0x0, 0x0,
  101. 0x0, 0x0, 0x0, 0xaa5b, /* IRQ steering */
  102. 0x0, 0x0, 0x0, 0x0,
  103. };
  104. static const uint32_t ac97_hdr[] = { /* dev f function 3 - devfn = 7b */
  105. 0xffffff81, 0x0, 0x0, 0x0,
  106. 0x0, 0x0, 0x0, 0x0,
  107. 0x20931022, 0x2a00041, 0x4010001, 0x0,
  108. 0x1481, 0x0, 0x0, 0x0, /* I/O BAR-128 */
  109. 0x0, 0x0, 0x0, 0x20931022,
  110. 0x0, 0x0, 0x0, 0x205, /* IntB, IRQ5 */
  111. 0x0, 0x0, 0x0, 0x0,
  112. 0x0, 0x0, 0x0, 0x0,
  113. 0x0, 0x0, 0x0, 0x0,
  114. };
  115. static const uint32_t ohci_hdr[] = { /* dev f function 4 - devfn = 7c */
  116. 0xfffff000, 0x0, 0x0, 0x0,
  117. 0x0, 0x0, 0x0, 0x0,
  118. 0x20941022, 0x2300006, 0xc031002, 0x0,
  119. 0xfe01a000, 0x0, 0x0, 0x0, /* MEMBAR-1000 */
  120. 0x0, 0x0, 0x0, 0x20941022,
  121. 0x0, 0x40, 0x0, 0x40a, /* CapPtr INT-D, IRQA */
  122. 0xc8020001, 0x0, 0x0, 0x0, /* Capabilities - 40 is R/O,
  123. 44 is mask 8103 (power control) */
  124. 0x0, 0x0, 0x0, 0x0,
  125. 0x0, 0x0, 0x0, 0x0,
  126. };
  127. static const uint32_t ehci_hdr[] = { /* dev f function 4 - devfn = 7d */
  128. 0xfffff000, 0x0, 0x0, 0x0,
  129. 0x0, 0x0, 0x0, 0x0,
  130. 0x20951022, 0x2300006, 0xc032002, 0x0,
  131. 0xfe01b000, 0x0, 0x0, 0x0, /* MEMBAR-1000 */
  132. 0x0, 0x0, 0x0, 0x20951022,
  133. 0x0, 0x40, 0x0, 0x40a, /* CapPtr INT-D, IRQA */
  134. 0xc8020001, 0x0, 0x0, 0x0, /* Capabilities - 40 is R/O, 44 is
  135. mask 8103 (power control) */
  136. #if 0
  137. 0x1, 0x40080000, 0x0, 0x0, /* EECP - see EHCI spec section 2.1.7 */
  138. #endif
  139. 0x01000001, 0x0, 0x0, 0x0, /* EECP - see EHCI spec section 2.1.7 */
  140. 0x2020, 0x0, 0x0, 0x0, /* (EHCI page 8) 60 SBRN (R/O),
  141. 61 FLADJ (R/W), PORTWAKECAP */
  142. };
  143. static uint32_t ff_loc = ~0;
  144. static uint32_t zero_loc;
  145. static int bar_probing; /* Set after a write of ~0 to a BAR */
  146. static int is_lx;
  147. #define NB_SLOT 0x1 /* Northbridge - GX chip - Device 1 */
  148. #define SB_SLOT 0xf /* Southbridge - CS5536 chip - Device F */
  149. static int is_simulated(unsigned int bus, unsigned int devfn)
  150. {
  151. return (!bus && ((PCI_SLOT(devfn) == NB_SLOT) ||
  152. (PCI_SLOT(devfn) == SB_SLOT)));
  153. }
  154. static uint32_t *hdr_addr(const uint32_t *hdr, int reg)
  155. {
  156. uint32_t addr;
  157. /*
  158. * This is a little bit tricky. The header maps consist of
  159. * 0x20 bytes of size masks, followed by 0x70 bytes of header data.
  160. * In the normal case, when not probing a BAR's size, we want
  161. * to access the header data, so we add 0x20 to the reg offset,
  162. * thus skipping the size mask area.
  163. * In the BAR probing case, we want to access the size mask for
  164. * the BAR, so we subtract 0x10 (the config header offset for
  165. * BAR0), and don't skip the size mask area.
  166. */
  167. addr = (uint32_t)hdr + reg + (bar_probing ? -0x10 : 0x20);
  168. bar_probing = 0;
  169. return (uint32_t *)addr;
  170. }
  171. static int pci_olpc_read(unsigned int seg, unsigned int bus,
  172. unsigned int devfn, int reg, int len, uint32_t *value)
  173. {
  174. uint32_t *addr;
  175. WARN_ON(seg);
  176. /* Use the hardware mechanism for non-simulated devices */
  177. if (!is_simulated(bus, devfn))
  178. return pci_direct_conf1.read(seg, bus, devfn, reg, len, value);
  179. /*
  180. * No device has config registers past 0x70, so we save table space
  181. * by not storing entries for the nonexistent registers
  182. */
  183. if (reg >= 0x70)
  184. addr = &zero_loc;
  185. else {
  186. switch (devfn) {
  187. case 0x8:
  188. addr = hdr_addr(is_lx ? lxnb_hdr : gxnb_hdr, reg);
  189. break;
  190. case 0x9:
  191. addr = hdr_addr(is_lx ? lxfb_hdr : gxfb_hdr, reg);
  192. break;
  193. case 0xa:
  194. addr = is_lx ? hdr_addr(aes_hdr, reg) : &ff_loc;
  195. break;
  196. case 0x78:
  197. addr = hdr_addr(isa_hdr, reg);
  198. break;
  199. case 0x7b:
  200. addr = hdr_addr(ac97_hdr, reg);
  201. break;
  202. case 0x7c:
  203. addr = hdr_addr(ohci_hdr, reg);
  204. break;
  205. case 0x7d:
  206. addr = hdr_addr(ehci_hdr, reg);
  207. break;
  208. default:
  209. addr = &ff_loc;
  210. break;
  211. }
  212. }
  213. switch (len) {
  214. case 1:
  215. *value = *(uint8_t *)addr;
  216. break;
  217. case 2:
  218. *value = *(uint16_t *)addr;
  219. break;
  220. case 4:
  221. *value = *addr;
  222. break;
  223. default:
  224. BUG();
  225. }
  226. return 0;
  227. }
  228. static int pci_olpc_write(unsigned int seg, unsigned int bus,
  229. unsigned int devfn, int reg, int len, uint32_t value)
  230. {
  231. WARN_ON(seg);
  232. /* Use the hardware mechanism for non-simulated devices */
  233. if (!is_simulated(bus, devfn))
  234. return pci_direct_conf1.write(seg, bus, devfn, reg, len, value);
  235. /* XXX we may want to extend this to simulate EHCI power management */
  236. /*
  237. * Mostly we just discard writes, but if the write is a size probe
  238. * (i.e. writing ~0 to a BAR), we remember it and arrange to return
  239. * the appropriate size mask on the next read. This is cheating
  240. * to some extent, because it depends on the fact that the next
  241. * access after such a write will always be a read to the same BAR.
  242. */
  243. if ((reg >= 0x10) && (reg < 0x2c)) {
  244. /* write is to a BAR */
  245. if (value == ~0)
  246. bar_probing = 1;
  247. } else {
  248. /*
  249. * No warning on writes to ROM BAR, CMD, LATENCY_TIMER,
  250. * CACHE_LINE_SIZE, or PM registers.
  251. */
  252. if ((reg != PCI_ROM_ADDRESS) && (reg != PCI_COMMAND_MASTER) &&
  253. (reg != PCI_LATENCY_TIMER) &&
  254. (reg != PCI_CACHE_LINE_SIZE) && (reg != 0x44))
  255. printk(KERN_WARNING "OLPC PCI: Config write to devfn"
  256. " %x reg %x value %x\n", devfn, reg, value);
  257. }
  258. return 0;
  259. }
  260. static const struct pci_raw_ops pci_olpc_conf = {
  261. .read = pci_olpc_read,
  262. .write = pci_olpc_write,
  263. };
  264. int __init pci_olpc_init(void)
  265. {
  266. printk(KERN_INFO "PCI: Using configuration type OLPC XO-1\n");
  267. raw_pci_ops = &pci_olpc_conf;
  268. is_lx = is_geode_lx();
  269. return 0;
  270. }