Isp1761UsbDxe.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /** @file
  2. Copyright (c) 2013-2015, ARM Ltd. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include <Library/DebugLib.h>
  6. #include <Library/UefiBootServicesTableLib.h>
  7. #include <Library/UefiDriverEntryPoint.h>
  8. #include <Library/IoLib.h>
  9. #include <Library/MemoryAllocationLib.h>
  10. #include <IndustryStandard/Usb.h>
  11. #include <Protocol/UsbDevice.h>
  12. #include "Isp1761UsbDxe.h"
  13. /*
  14. Driver for using the NXP ISP1761 as a USB Peripheral controller.
  15. Doesn't use USB OTG - just sets it in Pure Peripheral mode.
  16. The ISP1582 datasheet has a little more info on the Peripheral controller
  17. registers than the ISP1761 datasheet
  18. We don't do string descriptors. They're optional.
  19. We currently assume the device has one configuration, one interface, one IN
  20. endpoint, and one OUT endpoint (plus the default control endpoint).
  21. In fact, this driver is the minimum required to implement fastboot.
  22. */
  23. // TODO Make sure the controller isn't sending empty packets when it shouldn't
  24. // (check behaviour in cases when Buffer Length isn't explcitly set)
  25. // ISP1582 Datasheet:
  26. // "Data transfers preceding the status stage must first be fully
  27. // completed before the STATUS bit can be set."
  28. // This variable stores whether some control data has been pended in the EP0TX
  29. // Tx buffer, so that when an EP0TX interrupt is received we can set the STATUS
  30. // bit to go to the Status stage of the control transfer.
  31. STATIC BOOLEAN mControlTxPending = FALSE;
  32. STATIC USB_DEVICE_DESCRIPTOR *mDeviceDescriptor;
  33. // The config descriptor, interface descriptor, and endpoint descriptors in a
  34. // buffer (in that order)
  35. STATIC VOID *mDescriptors;
  36. // Convenience pointers to those descriptors inside the buffer:
  37. STATIC USB_INTERFACE_DESCRIPTOR *mInterfaceDescriptor;
  38. STATIC USB_CONFIG_DESCRIPTOR *mConfigDescriptor;
  39. STATIC USB_ENDPOINT_DESCRIPTOR *mEndpointDescriptors;
  40. STATIC USB_DEVICE_RX_CALLBACK mDataReceivedCallback;
  41. STATIC USB_DEVICE_TX_CALLBACK mDataSentCallback;
  42. // The time between interrupt polls, in units of 100 nanoseconds
  43. // 10 Microseconds
  44. #define ISP1761_INTERRUPT_POLL_PERIOD 10000
  45. STATIC
  46. VOID
  47. SelectEndpoint (
  48. IN UINT8 Endpoint
  49. )
  50. {
  51. // The DMA Endpoint Index must not point to the same as the
  52. // Endpoint Index Register.
  53. WRITE_REG32 (ISP1761_DMA_ENDPOINT_INDEX, ((Endpoint + 2) % ISP1761_NUM_ENDPOINTS));
  54. WRITE_REG32 (ISP1761_ENDPOINT_INDEX, Endpoint);
  55. }
  56. // Enable going to the Data stage of a control transfer
  57. STATIC
  58. VOID
  59. DataStageEnable (
  60. IN UINT8 Endpoint
  61. )
  62. {
  63. SelectEndpoint (Endpoint);
  64. WRITE_REG32 (ISP1761_CTRL_FUNCTION, ISP1761_CTRL_FUNCTION_DSEN);
  65. }
  66. // Go to the Status stage of a successful control transfer
  67. STATIC
  68. VOID
  69. StatusAcknowledge (
  70. IN UINT8 Endpoint
  71. )
  72. {
  73. SelectEndpoint (Endpoint);
  74. WRITE_REG32 (ISP1761_CTRL_FUNCTION, ISP1761_CTRL_FUNCTION_STATUS);
  75. }
  76. // Read the FIFO for the endpoint indexed by Endpoint, into the buffer pointed
  77. // at by Buffer, whose size is *Size bytes.
  78. //
  79. // If *Size is less than the number of bytes in the FIFO, return EFI_BUFFER_TOO_SMALL
  80. //
  81. // Update *Size with the number of bytes of data in the FIFO.
  82. STATIC
  83. EFI_STATUS
  84. ReadEndpointBuffer (
  85. IN UINT8 Endpoint,
  86. IN OUT UINTN *Size,
  87. IN OUT VOID *Buffer
  88. )
  89. {
  90. UINT16 NumBytesAvailable;
  91. UINT32 Val32;
  92. UINTN Index;
  93. UINTN NumBytesRead;
  94. SelectEndpoint (Endpoint);
  95. NumBytesAvailable = READ_REG16 (ISP1761_BUFFER_LENGTH);
  96. if (NumBytesAvailable > *Size) {
  97. *Size = NumBytesAvailable;
  98. return EFI_BUFFER_TOO_SMALL;
  99. }
  100. *Size = NumBytesAvailable;
  101. /* -- NB! --
  102. The datasheet says the Data Port is 16 bits but it actually appears to
  103. be 32 bits.
  104. */
  105. // Read 32-bit chunks
  106. for (Index = 0; Index < NumBytesAvailable / 4; Index++) {
  107. ((UINT32 *) Buffer)[Index] = READ_REG32 (ISP1761_DATA_PORT);
  108. }
  109. // Read remaining bytes
  110. // Round NumBytesAvailable down to nearest power of 4
  111. NumBytesRead = NumBytesAvailable & (~0x3);
  112. if (NumBytesRead != NumBytesAvailable) {
  113. Val32 = READ_REG32 (ISP1761_DATA_PORT);
  114. // Copy each required byte of 32-bit word into buffer
  115. for (Index = 0; Index < NumBytesAvailable % 4; Index++) {
  116. ((UINT8 *) Buffer)[NumBytesRead + Index] = Val32 >> (Index * 8);
  117. }
  118. }
  119. return EFI_SUCCESS;
  120. }
  121. /*
  122. Write an endpoint buffer. Parameters:
  123. Endpoint Endpoint index (see Endpoint Index Register in datasheet)
  124. MaxPacketSize The MaxPacketSize this endpoint is configured for
  125. Size The size of the Buffer
  126. Buffer The data
  127. Assumes MaxPacketSize is a multiple of 4.
  128. (It seems that all valid values for MaxPacketSize _are_ multiples of 4)
  129. */
  130. STATIC
  131. EFI_STATUS
  132. WriteEndpointBuffer (
  133. IN UINT8 Endpoint,
  134. IN UINTN MaxPacketSize,
  135. IN UINTN Size,
  136. IN CONST VOID *Buffer
  137. )
  138. {
  139. UINTN Index;
  140. UINT32 *DwordBuffer;
  141. DwordBuffer = (UINT32 *) Buffer;
  142. SelectEndpoint (Endpoint);
  143. /* -- NB! --
  144. The datasheet says the Data Port is 16 bits but it actually appears to
  145. be 32 bits.
  146. */
  147. // Write packets of size MaxPacketSize
  148. while (Size > MaxPacketSize) {
  149. for (Index = 0; Index < MaxPacketSize / 4; Index++) {
  150. WRITE_REG32 (ISP1761_DATA_PORT, DwordBuffer[Index]);
  151. }
  152. Size -= MaxPacketSize;
  153. DwordBuffer += (MaxPacketSize / sizeof (UINT32));
  154. }
  155. // Write remaining data
  156. if (Size > 0) {
  157. WRITE_REG32 (ISP1761_BUFFER_LENGTH, Size);
  158. while (Size > 4) {
  159. WRITE_REG32 (ISP1761_DATA_PORT, DwordBuffer[0]);
  160. Size -= 4;
  161. DwordBuffer++;
  162. }
  163. if (Size > 0) {
  164. WRITE_REG32 (ISP1761_DATA_PORT, DwordBuffer[0]);
  165. }
  166. }
  167. return EFI_SUCCESS;
  168. }
  169. STATIC
  170. EFI_STATUS
  171. HandleGetDescriptor (
  172. IN USB_DEVICE_REQUEST *Request
  173. )
  174. {
  175. EFI_STATUS Status;
  176. UINT8 DescriptorType;
  177. UINTN ResponseSize;
  178. VOID *ResponseData;
  179. ResponseSize = 0;
  180. ResponseData = NULL;
  181. Status = EFI_SUCCESS;
  182. // Pretty confused if bmRequestType is anything but this:
  183. ASSERT (Request->RequestType == USB_DEV_GET_DESCRIPTOR_REQ_TYPE);
  184. // Choose the response
  185. DescriptorType = Request->Value >> 8;
  186. switch (DescriptorType) {
  187. case USB_DESC_TYPE_DEVICE:
  188. DEBUG ((EFI_D_INFO, "USB: Got a request for device descriptor\n"));
  189. ResponseSize = sizeof (USB_DEVICE_DESCRIPTOR);
  190. ResponseData = mDeviceDescriptor;
  191. break;
  192. case USB_DESC_TYPE_CONFIG:
  193. DEBUG ((EFI_D_INFO, "USB: Got a request for config descriptor\n"));
  194. ResponseSize = mConfigDescriptor->TotalLength;
  195. ResponseData = mDescriptors;
  196. break;
  197. case USB_DESC_TYPE_STRING:
  198. DEBUG ((EFI_D_INFO, "USB: Got a request for String descriptor %d\n", Request->Value & 0xFF));
  199. break;
  200. default:
  201. DEBUG ((EFI_D_INFO, "USB: Didn't understand request for descriptor 0x%04x\n", Request->Value));
  202. Status = EFI_NOT_FOUND;
  203. break;
  204. }
  205. // Send the response
  206. if (ResponseData) {
  207. ASSERT (ResponseSize != 0);
  208. if (Request->Length < ResponseSize) {
  209. // Truncate response
  210. ResponseSize = Request->Length;
  211. } else if (Request->Length > ResponseSize) {
  212. DEBUG ((EFI_D_INFO, "USB: Info: ResponseSize < wLength\n"));
  213. }
  214. DataStageEnable (ISP1761_EP0TX);
  215. Status = WriteEndpointBuffer (
  216. ISP1761_EP0TX,
  217. MAX_PACKET_SIZE_CONTROL,
  218. ResponseSize,
  219. ResponseData
  220. );
  221. if (!EFI_ERROR (Status)) {
  222. // Setting this value should cause us to go to the Status stage on the
  223. // next EP0TX interrupt
  224. mControlTxPending = TRUE;
  225. }
  226. }
  227. return EFI_SUCCESS;
  228. }
  229. STATIC
  230. EFI_STATUS
  231. HandleSetAddress (
  232. IN USB_DEVICE_REQUEST *Request
  233. )
  234. {
  235. // Pretty confused if bmRequestType is anything but this:
  236. ASSERT (Request->RequestType == USB_DEV_SET_ADDRESS_REQ_TYPE);
  237. // USB Spec: "The USB device does not change its device address until after
  238. // the Status stage of this request is completed successfully."
  239. // ISP1582 datasheet: "The new device address is activated when the
  240. // device receives an acknowledgment from the host for the empty packet
  241. // token". (StatusAcknowledge causes an empty packet to be sent).
  242. // So, we write the Address register _before_ acking the SET_ADDRESS.
  243. DEBUG ((EFI_D_INFO, "USB: Setting address to %d\n", Request->Value));
  244. WRITE_REG32 (ISP1761_ADDRESS, Request->Value | ISP1761_ADDRESS_DEVEN);
  245. StatusAcknowledge (ISP1761_EP0TX);
  246. return EFI_SUCCESS;
  247. }
  248. // Move the device to the Configured state.
  249. // (This code only supports one configuration for a device, so the configuration
  250. // index is ignored)
  251. STATIC
  252. EFI_STATUS
  253. HandleSetConfiguration (
  254. IN USB_DEVICE_REQUEST *Request
  255. )
  256. {
  257. USB_ENDPOINT_DESCRIPTOR *EPDesc;
  258. UINTN Index;
  259. UINT8 EndpointIndex;
  260. ASSERT (Request->RequestType == USB_DEV_SET_CONFIGURATION_REQ_TYPE);
  261. DEBUG ((EFI_D_INFO, "USB: Setting configuration.\n"));
  262. // Configure endpoints
  263. for (Index = 0; Index < mInterfaceDescriptor->NumEndpoints; Index++) {
  264. EPDesc = &mEndpointDescriptors[Index];
  265. // To simplify for now, assume endpoints aren't "sparse", and are in order.
  266. ASSERT ((EPDesc->EndpointAddress & 0xF) == ((Index / 2) + 1));
  267. // Convert from USB endpoint index to ISP1761 endpoint Index
  268. // USB: Endpoint number is bits [3:0], IN/OUT is bit [7]
  269. // ISP1761: Endpoint number is bits [4:1], IN/OUT is bit [0]
  270. EndpointIndex = ((EPDesc->EndpointAddress & 0xF) << 1) |
  271. ((EPDesc->EndpointAddress & BIT7) >> 7);
  272. SelectEndpoint (EndpointIndex);
  273. // Set endpoint type (Bulk/Isochronous/Interrupt)
  274. WRITE_REG32 (ISP1761_ENDPOINT_MAX_PACKET_SIZE, EPDesc->MaxPacketSize);
  275. // Hardware foible (bug?): Although the datasheet seems to suggest it should
  276. // automatically be set to MaxPacketSize, the Buffer Length register appears
  277. // to be reset to 0, which causes an empty packet to be sent in response to
  278. // the first IN token of the session. The NOEMPKT field of the Endpoint Type
  279. // register sounds like it might fix this problem, but it doesn't
  280. // (it's "applicable only in the DMA mode").
  281. WRITE_REG32 (ISP1761_BUFFER_LENGTH, EPDesc->MaxPacketSize);
  282. WRITE_REG32 (ISP1761_ENDPOINT_TYPE, (EPDesc->Attributes & 0x3) |
  283. ISP1761_ENDPOINT_TYPE_ENABLE);
  284. }
  285. StatusAcknowledge (ISP1761_EP0TX);
  286. return EFI_SUCCESS;
  287. }
  288. STATIC
  289. EFI_STATUS
  290. HandleDeviceRequest (
  291. IN USB_DEVICE_REQUEST *Request
  292. )
  293. {
  294. EFI_STATUS Status;
  295. Status = EFI_SUCCESS;
  296. switch (Request->Request) {
  297. case USB_DEV_GET_DESCRIPTOR:
  298. Status = HandleGetDescriptor (Request);
  299. break;
  300. case USB_DEV_SET_ADDRESS:
  301. Status = HandleSetAddress (Request);
  302. break;
  303. case USB_DEV_SET_CONFIGURATION:
  304. Status = HandleSetConfiguration (Request);
  305. break;
  306. default:
  307. DEBUG ((EFI_D_ERROR,
  308. "Didn't understand RequestType 0x%x Request 0x%x\n",
  309. Request->RequestType, Request->Request));
  310. Status = EFI_INVALID_PARAMETER;
  311. break;
  312. }
  313. return Status;
  314. }
  315. // Instead of actually registering interrupt handlers, we poll the controller's
  316. // interrupt source register in this function.
  317. STATIC
  318. VOID
  319. CheckInterrupts (
  320. IN EFI_EVENT Event,
  321. IN VOID *Context
  322. )
  323. {
  324. UINT32 DcInterrupts;
  325. UINTN NumBytes;
  326. UINTN MoreBytes;
  327. UINT8 Packet[512];
  328. VOID *DataPacket;
  329. UINT32 HandledInterrupts;
  330. UINT32 UnhandledInterrupts;
  331. EFI_STATUS Status;
  332. // Set bits in HandledInterrupts to mark the interrupt source handled.
  333. HandledInterrupts = 0;
  334. WRITE_REG32 (ISP1761_DEVICE_UNLOCK, ISP1761_DEVICE_UNLOCK_MAGIC);
  335. DcInterrupts = READ_REG32 (ISP1761_DC_INTERRUPT);
  336. if (DcInterrupts & ISP1761_DC_INTERRUPT_SUSP) {
  337. DEBUG ((EFI_D_INFO, "USB: Suspend\n"));
  338. HandledInterrupts |= ISP1761_DC_INTERRUPT_SUSP;
  339. }
  340. if (DcInterrupts & ISP1761_DC_INTERRUPT_RESUME) {
  341. DEBUG ((EFI_D_INFO, "USB: Resume\n"));
  342. HandledInterrupts |= ISP1761_DC_INTERRUPT_RESUME;
  343. }
  344. if (DcInterrupts & ISP1761_DC_INTERRUPT_EP0SETUP) {
  345. NumBytes = 512;
  346. ReadEndpointBuffer (0x20, &NumBytes, &Packet);
  347. ASSERT (NumBytes == 8);
  348. HandleDeviceRequest ((USB_DEVICE_REQUEST *) Packet);
  349. HandledInterrupts |= ISP1761_DC_INTERRUPT_EP0SETUP;
  350. }
  351. if (DcInterrupts & ISP1761_DC_INTERRUPT_EP0RX) {
  352. HandledInterrupts |= ISP1761_DC_INTERRUPT_EP0RX;
  353. }
  354. if (DcInterrupts & ISP1761_DC_INTERRUPT_EP0TX) {
  355. if (mControlTxPending) {
  356. // We previously put some data in the Control Endpoint's IN (Tx) FIFO.
  357. // We assume that that data has now been sent in response to the IN token
  358. // that triggered this interrupt. We can therefore go to the Status stage
  359. // of the control transfer.
  360. StatusAcknowledge (ISP1761_EP0TX);
  361. mControlTxPending = FALSE;
  362. }
  363. HandledInterrupts |= ISP1761_DC_INTERRUPT_EP0TX;
  364. }
  365. if (DcInterrupts & ISP1761_DC_INTERRUPT_EP1RX) {
  366. NumBytes = 512;
  367. DataPacket = AllocatePool (NumBytes);
  368. Status = ReadEndpointBuffer (ISP1761_EP1RX, &NumBytes, DataPacket);
  369. if (EFI_ERROR (Status) || NumBytes == 0) {
  370. if (EFI_ERROR (Status)) {
  371. DEBUG ((EFI_D_ERROR, "Couldn't read EP1RX data: %r\n", Status));
  372. }
  373. FreePool (DataPacket);
  374. } else {
  375. // Signal this event again so we poll again ASAP
  376. gBS->SignalEvent (Event);
  377. mDataReceivedCallback (NumBytes, DataPacket);
  378. }
  379. HandledInterrupts |= ISP1761_DC_INTERRUPT_EP1RX;
  380. }
  381. if (DcInterrupts & ISP1761_DC_INTERRUPT_EP1TX) {
  382. mDataSentCallback (1);
  383. HandledInterrupts |= ISP1761_DC_INTERRUPT_EP1TX;
  384. }
  385. if (DcInterrupts & (ISP1761_DC_INTERRUPT_SOF | ISP1761_DC_INTERRUPT_PSOF)) {
  386. // Don't care about SOFs or pseudo-SOFs
  387. HandledInterrupts |= (ISP1761_DC_INTERRUPT_SOF | ISP1761_DC_INTERRUPT_PSOF);
  388. }
  389. if (ISP1761_DC_INTERRUPT_BRESET) {
  390. HandledInterrupts |= ISP1761_DC_INTERRUPT_BRESET;
  391. }
  392. if (ISP1761_DC_INTERRUPT_HS_STAT) {
  393. HandledInterrupts |= ISP1761_DC_INTERRUPT_HS_STAT;
  394. }
  395. if (ISP1761_DC_INTERRUPT_VBUS) {
  396. HandledInterrupts |= ISP1761_DC_INTERRUPT_VBUS;
  397. }
  398. UnhandledInterrupts = DcInterrupts & (~HandledInterrupts) & ISP1761_DC_INTERRUPT_MASK;
  399. if (UnhandledInterrupts) {
  400. DEBUG ((EFI_D_ERROR, "USB: Unhandled DC Interrupts: 0x%08x\n",
  401. UnhandledInterrupts));
  402. }
  403. // Check if we received any more data while we were handling the interrupt.
  404. SelectEndpoint (ISP1761_EP1RX);
  405. MoreBytes = READ_REG16 (ISP1761_BUFFER_LENGTH);
  406. if (MoreBytes) {
  407. HandledInterrupts &= ~ISP1761_DC_INTERRUPT_EP1RX;
  408. }
  409. WRITE_REG32 (ISP1761_DC_INTERRUPT, HandledInterrupts);
  410. }
  411. EFI_STATUS
  412. Isp1761PeriphSend (
  413. IN UINT8 EndpointIndex,
  414. IN UINTN Size,
  415. IN CONST VOID *Buffer
  416. )
  417. {
  418. return WriteEndpointBuffer (
  419. (EndpointIndex << 1) | 0x1, //Convert to ISP1761 endpoint index, Tx
  420. MAX_PACKET_SIZE_BULK,
  421. Size,
  422. Buffer
  423. );
  424. }
  425. EFI_STATUS
  426. EFIAPI
  427. Isp1761PeriphStart (
  428. IN USB_DEVICE_DESCRIPTOR *DeviceDescriptor,
  429. IN VOID **Descriptors,
  430. IN USB_DEVICE_RX_CALLBACK RxCallback,
  431. IN USB_DEVICE_TX_CALLBACK TxCallback
  432. )
  433. {
  434. UINT16 OtgStatus;
  435. UINT8 *Ptr;
  436. EFI_STATUS Status;
  437. EFI_EVENT TimerEvent;
  438. ASSERT (DeviceDescriptor != NULL);
  439. ASSERT (Descriptors[0] != NULL);
  440. ASSERT (RxCallback != NULL);
  441. ASSERT (TxCallback != NULL);
  442. WRITE_REG32 (ISP1761_DEVICE_UNLOCK, ISP1761_DEVICE_UNLOCK_MAGIC);
  443. WRITE_REG32 (ISP1761_SW_RESET_REG, ISP1761_SW_RESET_ALL);
  444. while (READ_REG32 (ISP1761_SW_RESET_REG) & ISP1761_SW_RESET_ALL) {
  445. //busy wait
  446. }
  447. WRITE_REG32 (ISP1761_MODE, ISP1761_MODE_SFRESET);
  448. while (READ_REG32 (ISP1761_MODE) & ISP1761_MODE_SFRESET) {
  449. //busy wait
  450. }
  451. DEBUG ((EFI_D_INFO, "USB: Software reset done\n"));
  452. WRITE_REG32 (ISP1761_DC_INTERRUPT_ENABLE, 0x03FFFFFF);
  453. WRITE_REG32 (ISP1761_OTG_INTERRUPT_ENABLE_RISE, 0x07FF);
  454. WRITE_REG8 (ISP1761_ADDRESS, ISP1761_ADDRESS_DEVEN);
  455. WRITE_REG8 (ISP1761_MODE, ISP1761_MODE_WKUPCS | ISP1761_MODE_CLKAON);
  456. // Use port 1 as peripheral controller (magic - disagrees with datasheet)
  457. WRITE_REG32 (ISP1761_OTG_CTRL_SET, 0xffff0000);
  458. WRITE_REG32 (ISP1761_OTG_CTRL_SET, 0x000014d1);
  459. OtgStatus = READ_REG16 (ISP1761_OTG_STATUS);
  460. if ((OtgStatus & ISP1761_OTG_STATUS_B_SESS_END) != 0) {
  461. DEBUG ((EFI_D_ERROR, "USB: Vbus not powered.\n"));
  462. }
  463. if ((OtgStatus & ISP1761_OTG_STATUS_A_B_SESS_VLD) == 0) {
  464. DEBUG ((EFI_D_ERROR, "USB: Session not valid.\n"));
  465. }
  466. // Configure Control endpoints
  467. SelectEndpoint (0x20);
  468. WRITE_REG32 (ISP1761_ENDPOINT_MAX_PACKET_SIZE, MAX_PACKET_SIZE_CONTROL);
  469. WRITE_REG32 (ISP1761_ENDPOINT_TYPE, ISP1761_ENDPOINT_TYPE_ENABLE);
  470. SelectEndpoint (0x0);
  471. WRITE_REG32 (ISP1761_ENDPOINT_MAX_PACKET_SIZE, MAX_PACKET_SIZE_CONTROL);
  472. WRITE_REG32 (ISP1761_ENDPOINT_TYPE, ISP1761_ENDPOINT_TYPE_ENABLE);
  473. SelectEndpoint (0x1);
  474. WRITE_REG32 (ISP1761_ENDPOINT_MAX_PACKET_SIZE, MAX_PACKET_SIZE_CONTROL);
  475. WRITE_REG32 (ISP1761_ENDPOINT_TYPE, ISP1761_ENDPOINT_TYPE_ENABLE);
  476. // Interrupt on all ACK and NAK
  477. WRITE_REG32 (ISP1761_INTERRUPT_CONFIG, ISP1761_INTERRUPT_CONFIG_ACK_ONLY);
  478. mDeviceDescriptor = DeviceDescriptor;
  479. mDescriptors = Descriptors[0];
  480. // Right now we just support one configuration
  481. ASSERT (mDeviceDescriptor->NumConfigurations == 1);
  482. // ... and one interface
  483. mConfigDescriptor = (USB_CONFIG_DESCRIPTOR *)mDescriptors;
  484. ASSERT (mConfigDescriptor->NumInterfaces == 1);
  485. Ptr = ((UINT8 *) mDescriptors) + sizeof (USB_CONFIG_DESCRIPTOR);
  486. mInterfaceDescriptor = (USB_INTERFACE_DESCRIPTOR *) Ptr;
  487. Ptr += sizeof (USB_INTERFACE_DESCRIPTOR);
  488. mEndpointDescriptors = (USB_ENDPOINT_DESCRIPTOR *) Ptr;
  489. mDataReceivedCallback = RxCallback;
  490. mDataSentCallback = TxCallback;
  491. // Register a timer event so CheckInterupts gets called periodically
  492. Status = gBS->CreateEvent (
  493. EVT_TIMER | EVT_NOTIFY_SIGNAL,
  494. TPL_CALLBACK,
  495. CheckInterrupts,
  496. NULL,
  497. &TimerEvent
  498. );
  499. ASSERT_EFI_ERROR (Status);
  500. if (EFI_ERROR (Status)) {
  501. return Status;
  502. }
  503. Status = gBS->SetTimer (
  504. TimerEvent,
  505. TimerPeriodic,
  506. ISP1761_INTERRUPT_POLL_PERIOD
  507. );
  508. ASSERT_EFI_ERROR (Status);
  509. return Status;
  510. }
  511. USB_DEVICE_PROTOCOL mUsbDevice = {
  512. Isp1761PeriphStart,
  513. Isp1761PeriphSend
  514. };
  515. EFI_STATUS
  516. EFIAPI
  517. Isp1761PeriphEntryPoint (
  518. IN EFI_HANDLE ImageHandle,
  519. IN EFI_SYSTEM_TABLE *SystemTable
  520. )
  521. {
  522. UINT32 DeviceId;
  523. EFI_HANDLE Handle;
  524. DeviceId = READ_REG32 (ISP1761_DEVICE_ID);
  525. if (DeviceId != ISP1761_DEVICE_ID_VAL) {
  526. DEBUG ((EFI_D_ERROR,
  527. "ERROR: Read incorrect device ID for ISP1761: 0x%08x, expected 0x%08x\n",
  528. DeviceId , ISP1761_DEVICE_ID_VAL
  529. ));
  530. return EFI_DEVICE_ERROR;
  531. }
  532. Handle = NULL;
  533. return gBS->InstallProtocolInterface (
  534. &Handle,
  535. &gUsbDeviceProtocolGuid,
  536. EFI_NATIVE_INTERFACE,
  537. &mUsbDevice
  538. );
  539. }