romusb.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. romusb.c
  5. Abstract:
  6. This module implements support for the OMAP4 ROM USB interface.
  7. Author:
  8. Evan Green 1-Apr-2014
  9. Environment:
  10. Firmware
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include <uefifw.h>
  16. #include "init.h"
  17. //
  18. // ---------------------------------------------------------------- Definitions
  19. //
  20. //
  21. // ------------------------------------------------------ Data Type Definitions
  22. //
  23. //
  24. // ----------------------------------------------- Internal Function Prototypes
  25. //
  26. VOID
  27. EfipOmap4UsbQueueRead (
  28. PTI_ROM_USB_HANDLE Handle,
  29. VOID *Data,
  30. UINTN Length
  31. );
  32. INTN
  33. EfipOmap4UsbWaitForRead (
  34. PTI_ROM_USB_HANDLE Handle
  35. );
  36. INT32
  37. EfipOmap4UsbReadCallback (
  38. PTI_ROM_PER_HANDLE Handle
  39. );
  40. VOID
  41. EfipOmap4UsbQueueWrite (
  42. PTI_ROM_USB_HANDLE Handle,
  43. VOID *Data,
  44. UINTN Length
  45. );
  46. INTN
  47. EfipOmap4UsbWaitForWrite (
  48. PTI_ROM_USB_HANDLE Handle
  49. );
  50. INT32
  51. EfipOmap4UsbWriteCallback (
  52. PTI_ROM_PER_HANDLE Handle
  53. );
  54. //
  55. // -------------------------------------------------------------------- Globals
  56. //
  57. //
  58. // Store a pointer to the handle of the transfer that was just queued.
  59. //
  60. PTI_ROM_USB_HANDLE EfiOmap4ActiveUsbReadHandle;
  61. PTI_ROM_USB_HANDLE EfiOmap4ActiveUsbWriteHandle;
  62. //
  63. // ------------------------------------------------------------------ Functions
  64. //
  65. INTN
  66. EfipOmap4UsbOpen (
  67. PTI_ROM_USB_HANDLE Handle
  68. )
  69. /*++
  70. Routine Description:
  71. This routine opens a connection to the ROM API for the USB device.
  72. Arguments:
  73. Handle - Supplies a pointer where the connection state will be returned
  74. on success.
  75. Return Value:
  76. 0 on success.
  77. Non-zero on failure.
  78. --*/
  79. {
  80. UINT32 Base;
  81. PTI_ROM_PER_HANDLE BootHandle;
  82. PTI_ROM_GET_PER_DEVICE GetDevice;
  83. PTI_ROM_GET_PER_DRIVER GetDriver;
  84. INTN Result;
  85. EfipInitZeroMemory(Handle, sizeof(TI_ROM_USB_HANDLE));
  86. if (EfipOmap4GetRevision() >= Omap4460RevisionEs10) {
  87. Base = OMAP4460_PUBLIC_API_BASE;
  88. } else {
  89. Base = OMAP4430_PUBLIC_API_BASE;
  90. }
  91. GetDevice = TI_ROM_API(Base + PUBLIC_GET_DEVICE_PER_OFFSET);
  92. GetDriver = TI_ROM_API(Base + PUBLIC_GET_DRIVER_PER_OFFSET);
  93. BootHandle = NULL;
  94. Result = GetDevice(&BootHandle);
  95. if (Result != 0) {
  96. return Result;
  97. }
  98. if ((BootHandle->DeviceType != OMAP4_ROM_DEVICE_USB) &&
  99. (BootHandle->DeviceType != OMAP4_ROM_DEVICE_USBEXT)) {
  100. return -1;
  101. }
  102. Result = GetDriver(&(Handle->Driver), BootHandle->DeviceType);
  103. if (Result != 0) {
  104. return Result;
  105. }
  106. Handle->ReadHandle.TransferMode = BootHandle->TransferMode;
  107. Handle->ReadHandle.Options = BootHandle->Options;
  108. Handle->ReadHandle.DeviceType = BootHandle->DeviceType;
  109. Handle->WriteHandle.TransferMode = BootHandle->TransferMode;
  110. Handle->WriteHandle.Options = BootHandle->Options;
  111. Handle->WriteHandle.DeviceType = BootHandle->DeviceType;
  112. return 0;
  113. }
  114. INTN
  115. EfipOmap4UsbRead (
  116. PTI_ROM_USB_HANDLE Handle,
  117. VOID *Data,
  118. UINTN Length
  119. )
  120. /*++
  121. Routine Description:
  122. This routine reads from the USB device.
  123. Arguments:
  124. Handle - Supplies a pointer to the ROM handle.
  125. Data - Supplies a pointer where the data will be returned on success.
  126. Length - Supplies the length of the buffer in bytes.
  127. Return Value:
  128. 0 on success.
  129. Non-zero on failure.
  130. --*/
  131. {
  132. UINT8 *Buffer;
  133. INTN Result;
  134. UINT32 Transfer;
  135. Buffer = Data;
  136. while (Length > 0) {
  137. Transfer = TI_ROM_USB_MAX_IO_SIZE;
  138. if (Length < Transfer) {
  139. Transfer = Length;
  140. }
  141. EfipOmap4UsbQueueRead(Handle, Buffer, Transfer);
  142. Result = EfipOmap4UsbWaitForRead(Handle);
  143. if (Result != 0) {
  144. return Result;
  145. }
  146. Buffer += Transfer;
  147. Length -= Transfer;
  148. }
  149. return 0;
  150. }
  151. INTN
  152. EfipOmap4UsbWrite (
  153. PTI_ROM_USB_HANDLE Handle,
  154. VOID *Data,
  155. UINTN Length
  156. )
  157. /*++
  158. Routine Description:
  159. This routine writes to the USB device.
  160. Arguments:
  161. Handle - Supplies a pointer to the ROM handle.
  162. Data - Supplies a pointer to the data to write.
  163. Length - Supplies the length of the buffer in bytes.
  164. Return Value:
  165. 0 on success.
  166. Non-zero on failure.
  167. --*/
  168. {
  169. EfipOmap4UsbQueueWrite(Handle, Data, Length);
  170. return EfipOmap4UsbWaitForWrite(Handle);
  171. }
  172. VOID
  173. EfipOmap4UsbClose (
  174. PTI_ROM_USB_HANDLE Handle
  175. )
  176. /*++
  177. Routine Description:
  178. This routine closes an open handle to the USB device.
  179. Arguments:
  180. Handle - Supplies a pointer to the ROM handle.
  181. Return Value:
  182. None.
  183. --*/
  184. {
  185. Handle->Driver->Close(&(Handle->ReadHandle));
  186. return;
  187. }
  188. //
  189. // --------------------------------------------------------- Internal Functions
  190. //
  191. VOID
  192. EfipOmap4UsbQueueRead (
  193. PTI_ROM_USB_HANDLE Handle,
  194. VOID *Data,
  195. UINTN Length
  196. )
  197. /*++
  198. Routine Description:
  199. This routine queues a read request from the USB device.
  200. Arguments:
  201. Handle - Supplies a pointer to the ROM handle.
  202. Data - Supplies a pointer where the data will be returned on success.
  203. Length - Supplies the length of the buffer in bytes.
  204. Return Value:
  205. None.
  206. --*/
  207. {
  208. INTN Result;
  209. Handle->ReadHandle.Data = Data;
  210. Handle->ReadHandle.Length = Length;
  211. Handle->ReadHandle.Status = -1;
  212. Handle->ReadHandle.TransferMode = TI_ROM_TRANSFER_MODE_DMA;
  213. Handle->ReadHandle.Callback = EfipOmap4UsbReadCallback;
  214. EfiOmap4ActiveUsbReadHandle = Handle;
  215. Result = Handle->Driver->Read(&(Handle->ReadHandle));
  216. if (Result != 0) {
  217. Handle->ReadHandle.Status = Result;
  218. }
  219. return;
  220. }
  221. INTN
  222. EfipOmap4UsbWaitForRead (
  223. PTI_ROM_USB_HANDLE Handle
  224. )
  225. /*++
  226. Routine Description:
  227. This routine waits for a USB transfer to complete and returns its status.
  228. Arguments:
  229. Handle - Supplies a pointer to the ROM handle.
  230. Return Value:
  231. 0 on success.
  232. Non-zero on failure.
  233. --*/
  234. {
  235. while (TRUE) {
  236. if ((Handle->ReadHandle.Status != -1) &&
  237. (Handle->ReadHandle.Status != TI_ROM_STATUS_WAITING)) {
  238. return Handle->ReadHandle.Status;
  239. }
  240. }
  241. return -1;
  242. }
  243. INT32
  244. EfipOmap4UsbReadCallback (
  245. PTI_ROM_PER_HANDLE Handle
  246. )
  247. /*++
  248. Routine Description:
  249. This routine is called by the ROM when I/O completes.
  250. Arguments:
  251. Handle - Supplies the handle with the I/O.
  252. Return Value:
  253. 0 on success.
  254. Non-zero on failure.
  255. --*/
  256. {
  257. EfiOmap4ActiveUsbReadHandle->ReadHandle.Status = Handle->Status;
  258. return 0;
  259. }
  260. VOID
  261. EfipOmap4UsbQueueWrite (
  262. PTI_ROM_USB_HANDLE Handle,
  263. VOID *Data,
  264. UINTN Length
  265. )
  266. /*++
  267. Routine Description:
  268. This routine queues a write request to the USB device.
  269. Arguments:
  270. Handle - Supplies a pointer to the ROM handle.
  271. Data - Supplies a pointer where the data will be returned on success.
  272. Length - Supplies the length of the buffer in bytes.
  273. Return Value:
  274. None.
  275. --*/
  276. {
  277. INTN Result;
  278. Handle->WriteHandle.Data = Data;
  279. Handle->WriteHandle.Length = Length;
  280. Handle->WriteHandle.Status = -1;
  281. Handle->WriteHandle.TransferMode = TI_ROM_TRANSFER_MODE_DMA;
  282. Handle->WriteHandle.Callback = EfipOmap4UsbWriteCallback;
  283. EfiOmap4ActiveUsbWriteHandle = Handle;
  284. Result = Handle->Driver->Write(&(Handle->WriteHandle));
  285. if (Result != 0) {
  286. Handle->WriteHandle.Status = Result;
  287. }
  288. return;
  289. }
  290. INTN
  291. EfipOmap4UsbWaitForWrite (
  292. PTI_ROM_USB_HANDLE Handle
  293. )
  294. /*++
  295. Routine Description:
  296. This routine waits for a USB transfer to complete and returns its status.
  297. Arguments:
  298. Handle - Supplies a pointer to the ROM handle.
  299. Return Value:
  300. 0 on success.
  301. Non-zero on failure.
  302. --*/
  303. {
  304. while (TRUE) {
  305. if ((Handle->WriteHandle.Status != -1) &&
  306. (Handle->WriteHandle.Status != TI_ROM_STATUS_WAITING)) {
  307. return Handle->WriteHandle.Status;
  308. }
  309. }
  310. return -1;
  311. }
  312. INT32
  313. EfipOmap4UsbWriteCallback (
  314. PTI_ROM_PER_HANDLE Handle
  315. )
  316. /*++
  317. Routine Description:
  318. This routine is called by the ROM when I/O completes.
  319. Arguments:
  320. Handle - Supplies the handle with the I/O.
  321. Return Value:
  322. 0 on success.
  323. Non-zero on failure.
  324. --*/
  325. {
  326. EfiOmap4ActiveUsbWriteHandle->ReadHandle.Status = Handle->Status;
  327. return 0;
  328. }