olpc.c 9.4 KB

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