hcd-ehci-pci.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * QEMU USB EHCI Emulation
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or(at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "hw/usb/hcd-ehci.h"
  18. #include "qemu/range.h"
  19. typedef struct EHCIPCIInfo {
  20. const char *name;
  21. uint16_t vendor_id;
  22. uint16_t device_id;
  23. uint8_t revision;
  24. } EHCIPCIInfo;
  25. static int usb_ehci_pci_initfn(PCIDevice *dev)
  26. {
  27. EHCIPCIState *i = PCI_EHCI(dev);
  28. EHCIState *s = &i->ehci;
  29. uint8_t *pci_conf = dev->config;
  30. pci_set_byte(&pci_conf[PCI_CLASS_PROG], 0x20);
  31. /* capabilities pointer */
  32. pci_set_byte(&pci_conf[PCI_CAPABILITY_LIST], 0x00);
  33. /* pci_set_byte(&pci_conf[PCI_CAPABILITY_LIST], 0x50); */
  34. pci_set_byte(&pci_conf[PCI_INTERRUPT_PIN], 4); /* interrupt pin D */
  35. pci_set_byte(&pci_conf[PCI_MIN_GNT], 0);
  36. pci_set_byte(&pci_conf[PCI_MAX_LAT], 0);
  37. /* pci_conf[0x50] = 0x01; *//* power management caps */
  38. pci_set_byte(&pci_conf[USB_SBRN], USB_RELEASE_2); /* release # (2.1.4) */
  39. pci_set_byte(&pci_conf[0x61], 0x20); /* frame length adjustment (2.1.5) */
  40. pci_set_word(&pci_conf[0x62], 0x00); /* port wake up capability (2.1.6) */
  41. pci_conf[0x64] = 0x00;
  42. pci_conf[0x65] = 0x00;
  43. pci_conf[0x66] = 0x00;
  44. pci_conf[0x67] = 0x00;
  45. pci_conf[0x68] = 0x01;
  46. pci_conf[0x69] = 0x00;
  47. pci_conf[0x6a] = 0x00;
  48. pci_conf[0x6b] = 0x00; /* USBLEGSUP */
  49. pci_conf[0x6c] = 0x00;
  50. pci_conf[0x6d] = 0x00;
  51. pci_conf[0x6e] = 0x00;
  52. pci_conf[0x6f] = 0xc0; /* USBLEFCTLSTS */
  53. s->irq = pci_allocate_irq(dev);
  54. s->as = pci_get_address_space(dev);
  55. usb_ehci_realize(s, DEVICE(dev), NULL);
  56. pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mem);
  57. return 0;
  58. }
  59. static void usb_ehci_pci_init(Object *obj)
  60. {
  61. EHCIPCIState *i = PCI_EHCI(obj);
  62. EHCIState *s = &i->ehci;
  63. s->caps[0x09] = 0x68; /* EECP */
  64. s->capsbase = 0x00;
  65. s->opregbase = 0x20;
  66. s->portscbase = 0x44;
  67. s->portnr = NB_PORTS;
  68. usb_ehci_init(s, DEVICE(obj));
  69. }
  70. static void usb_ehci_pci_write_config(PCIDevice *dev, uint32_t addr,
  71. uint32_t val, int l)
  72. {
  73. EHCIPCIState *i = PCI_EHCI(dev);
  74. bool busmaster;
  75. pci_default_write_config(dev, addr, val, l);
  76. if (!range_covers_byte(addr, l, PCI_COMMAND)) {
  77. return;
  78. }
  79. busmaster = pci_get_word(dev->config + PCI_COMMAND) & PCI_COMMAND_MASTER;
  80. i->ehci.as = busmaster ? pci_get_address_space(dev) : &address_space_memory;
  81. }
  82. static Property ehci_pci_properties[] = {
  83. DEFINE_PROP_UINT32("maxframes", EHCIPCIState, ehci.maxframes, 128),
  84. DEFINE_PROP_END_OF_LIST(),
  85. };
  86. static const VMStateDescription vmstate_ehci_pci = {
  87. .name = "ehci",
  88. .version_id = 2,
  89. .minimum_version_id = 1,
  90. .fields = (VMStateField[]) {
  91. VMSTATE_PCI_DEVICE(pcidev, EHCIPCIState),
  92. VMSTATE_STRUCT(ehci, EHCIPCIState, 2, vmstate_ehci, EHCIState),
  93. VMSTATE_END_OF_LIST()
  94. }
  95. };
  96. static void ehci_class_init(ObjectClass *klass, void *data)
  97. {
  98. DeviceClass *dc = DEVICE_CLASS(klass);
  99. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  100. k->init = usb_ehci_pci_initfn;
  101. k->class_id = PCI_CLASS_SERIAL_USB;
  102. k->config_write = usb_ehci_pci_write_config;
  103. dc->hotpluggable = false;
  104. dc->vmsd = &vmstate_ehci_pci;
  105. dc->props = ehci_pci_properties;
  106. }
  107. static const TypeInfo ehci_pci_type_info = {
  108. .name = TYPE_PCI_EHCI,
  109. .parent = TYPE_PCI_DEVICE,
  110. .instance_size = sizeof(EHCIPCIState),
  111. .instance_init = usb_ehci_pci_init,
  112. .abstract = true,
  113. .class_init = ehci_class_init,
  114. };
  115. static void ehci_data_class_init(ObjectClass *klass, void *data)
  116. {
  117. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  118. DeviceClass *dc = DEVICE_CLASS(klass);
  119. EHCIPCIInfo *i = data;
  120. k->vendor_id = i->vendor_id;
  121. k->device_id = i->device_id;
  122. k->revision = i->revision;
  123. set_bit(DEVICE_CATEGORY_USB, dc->categories);
  124. }
  125. static struct EHCIPCIInfo ehci_pci_info[] = {
  126. {
  127. .name = "usb-ehci",
  128. .vendor_id = PCI_VENDOR_ID_INTEL,
  129. .device_id = PCI_DEVICE_ID_INTEL_82801D, /* ich4 */
  130. .revision = 0x10,
  131. },{
  132. .name = "ich9-usb-ehci1", /* 00:1d.7 */
  133. .vendor_id = PCI_VENDOR_ID_INTEL,
  134. .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI1,
  135. .revision = 0x03,
  136. },{
  137. .name = "ich9-usb-ehci2", /* 00:1a.7 */
  138. .vendor_id = PCI_VENDOR_ID_INTEL,
  139. .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI2,
  140. .revision = 0x03,
  141. }
  142. };
  143. static void ehci_pci_register_types(void)
  144. {
  145. TypeInfo ehci_type_info = {
  146. .parent = TYPE_PCI_EHCI,
  147. .class_init = ehci_data_class_init,
  148. };
  149. int i;
  150. type_register_static(&ehci_pci_type_info);
  151. for (i = 0; i < ARRAY_SIZE(ehci_pci_info); i++) {
  152. ehci_type_info.name = ehci_pci_info[i].name;
  153. ehci_type_info.class_data = ehci_pci_info + i;
  154. type_register(&ehci_type_info);
  155. }
  156. }
  157. type_init(ehci_pci_register_types)
  158. struct ehci_companions {
  159. const char *name;
  160. int func;
  161. int port;
  162. };
  163. static const struct ehci_companions ich9_1d[] = {
  164. { .name = "ich9-usb-uhci1", .func = 0, .port = 0 },
  165. { .name = "ich9-usb-uhci2", .func = 1, .port = 2 },
  166. { .name = "ich9-usb-uhci3", .func = 2, .port = 4 },
  167. };
  168. static const struct ehci_companions ich9_1a[] = {
  169. { .name = "ich9-usb-uhci4", .func = 0, .port = 0 },
  170. { .name = "ich9-usb-uhci5", .func = 1, .port = 2 },
  171. { .name = "ich9-usb-uhci6", .func = 2, .port = 4 },
  172. };
  173. int ehci_create_ich9_with_companions(PCIBus *bus, int slot)
  174. {
  175. const struct ehci_companions *comp;
  176. PCIDevice *ehci, *uhci;
  177. BusState *usbbus;
  178. const char *name;
  179. int i;
  180. switch (slot) {
  181. case 0x1d:
  182. name = "ich9-usb-ehci1";
  183. comp = ich9_1d;
  184. break;
  185. case 0x1a:
  186. name = "ich9-usb-ehci2";
  187. comp = ich9_1a;
  188. break;
  189. default:
  190. return -1;
  191. }
  192. ehci = pci_create_multifunction(bus, PCI_DEVFN(slot, 7), true, name);
  193. qdev_init_nofail(&ehci->qdev);
  194. usbbus = QLIST_FIRST(&ehci->qdev.child_bus);
  195. for (i = 0; i < 3; i++) {
  196. uhci = pci_create_multifunction(bus, PCI_DEVFN(slot, comp[i].func),
  197. true, comp[i].name);
  198. qdev_prop_set_string(&uhci->qdev, "masterbus", usbbus->name);
  199. qdev_prop_set_uint32(&uhci->qdev, "firstport", comp[i].port);
  200. qdev_init_nofail(&uhci->qdev);
  201. }
  202. return 0;
  203. }