olpc.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. 0x01000001, 0x0, 0x0, 0x0, /* EECP - see EHCI spec section 2.1.7 */
  137. 0x2020, 0x0, 0x0, 0x0, /* (EHCI page 8) 60 SBRN (R/O),
  138. 61 FLADJ (R/W), PORTWAKECAP */
  139. };
  140. static uint32_t ff_loc = ~0;
  141. static uint32_t zero_loc;
  142. static int bar_probing; /* Set after a write of ~0 to a BAR */
  143. static int is_lx;
  144. #define NB_SLOT 0x1 /* Northbridge - GX chip - Device 1 */
  145. #define SB_SLOT 0xf /* Southbridge - CS5536 chip - Device F */
  146. static int is_simulated(unsigned int bus, unsigned int devfn)
  147. {
  148. return (!bus && ((PCI_SLOT(devfn) == NB_SLOT) ||
  149. (PCI_SLOT(devfn) == SB_SLOT)));
  150. }
  151. static uint32_t *hdr_addr(const uint32_t *hdr, int reg)
  152. {
  153. uint32_t addr;
  154. /*
  155. * This is a little bit tricky. The header maps consist of
  156. * 0x20 bytes of size masks, followed by 0x70 bytes of header data.
  157. * In the normal case, when not probing a BAR's size, we want
  158. * to access the header data, so we add 0x20 to the reg offset,
  159. * thus skipping the size mask area.
  160. * In the BAR probing case, we want to access the size mask for
  161. * the BAR, so we subtract 0x10 (the config header offset for
  162. * BAR0), and don't skip the size mask area.
  163. */
  164. addr = (uint32_t)hdr + reg + (bar_probing ? -0x10 : 0x20);
  165. bar_probing = 0;
  166. return (uint32_t *)addr;
  167. }
  168. static int pci_olpc_read(unsigned int seg, unsigned int bus,
  169. unsigned int devfn, int reg, int len, uint32_t *value)
  170. {
  171. uint32_t *addr;
  172. WARN_ON(seg);
  173. /* Use the hardware mechanism for non-simulated devices */
  174. if (!is_simulated(bus, devfn))
  175. return pci_direct_conf1.read(seg, bus, devfn, reg, len, value);
  176. /*
  177. * No device has config registers past 0x70, so we save table space
  178. * by not storing entries for the nonexistent registers
  179. */
  180. if (reg >= 0x70)
  181. addr = &zero_loc;
  182. else {
  183. switch (devfn) {
  184. case 0x8:
  185. addr = hdr_addr(is_lx ? lxnb_hdr : gxnb_hdr, reg);
  186. break;
  187. case 0x9:
  188. addr = hdr_addr(is_lx ? lxfb_hdr : gxfb_hdr, reg);
  189. break;
  190. case 0xa:
  191. addr = is_lx ? hdr_addr(aes_hdr, reg) : &ff_loc;
  192. break;
  193. case 0x78:
  194. addr = hdr_addr(isa_hdr, reg);
  195. break;
  196. case 0x7b:
  197. addr = hdr_addr(ac97_hdr, reg);
  198. break;
  199. case 0x7c:
  200. addr = hdr_addr(ohci_hdr, reg);
  201. break;
  202. case 0x7d:
  203. addr = hdr_addr(ehci_hdr, reg);
  204. break;
  205. default:
  206. addr = &ff_loc;
  207. break;
  208. }
  209. }
  210. switch (len) {
  211. case 1:
  212. *value = *(uint8_t *)addr;
  213. break;
  214. case 2:
  215. *value = *(uint16_t *)addr;
  216. break;
  217. case 4:
  218. *value = *addr;
  219. break;
  220. default:
  221. BUG();
  222. }
  223. return 0;
  224. }
  225. static int pci_olpc_write(unsigned int seg, unsigned int bus,
  226. unsigned int devfn, int reg, int len, uint32_t value)
  227. {
  228. WARN_ON(seg);
  229. /* Use the hardware mechanism for non-simulated devices */
  230. if (!is_simulated(bus, devfn))
  231. return pci_direct_conf1.write(seg, bus, devfn, reg, len, value);
  232. /* XXX we may want to extend this to simulate EHCI power management */
  233. /*
  234. * Mostly we just discard writes, but if the write is a size probe
  235. * (i.e. writing ~0 to a BAR), we remember it and arrange to return
  236. * the appropriate size mask on the next read. This is cheating
  237. * to some extent, because it depends on the fact that the next
  238. * access after such a write will always be a read to the same BAR.
  239. */
  240. if ((reg >= 0x10) && (reg < 0x2c)) {
  241. /* write is to a BAR */
  242. if (value == ~0)
  243. bar_probing = 1;
  244. } else {
  245. /*
  246. * No warning on writes to ROM BAR, CMD, LATENCY_TIMER,
  247. * CACHE_LINE_SIZE, or PM registers.
  248. */
  249. if ((reg != PCI_ROM_ADDRESS) && (reg != PCI_COMMAND_MASTER) &&
  250. (reg != PCI_LATENCY_TIMER) &&
  251. (reg != PCI_CACHE_LINE_SIZE) && (reg != 0x44))
  252. printk(KERN_WARNING "OLPC PCI: Config write to devfn"
  253. " %x reg %x value %x\n", devfn, reg, value);
  254. }
  255. return 0;
  256. }
  257. static const struct pci_raw_ops pci_olpc_conf = {
  258. .read = pci_olpc_read,
  259. .write = pci_olpc_write,
  260. };
  261. int __init pci_olpc_init(void)
  262. {
  263. printk(KERN_INFO "PCI: Using configuration type OLPC XO-1\n");
  264. raw_pci_ops = &pci_olpc_conf;
  265. is_lx = is_geode_lx();
  266. return 0;
  267. }