serial.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. serial.c
  5. Abstract:
  6. This module implements support for the serial device on the TI PandaBoard.
  7. Author:
  8. Evan Green 8-Apr-2014
  9. Environment:
  10. Firmware
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include <uefifw.h>
  16. #include <minoca/uefi/protocol/serio.h>
  17. #include "pandafw.h"
  18. #include "dev/omapuart.h"
  19. //
  20. // --------------------------------------------------------------------- Macros
  21. //
  22. //
  23. // This macro returns a pointer to the disk I/O data given a pointer to the
  24. // block I/O protocol instance.
  25. //
  26. #define EFI_PANDA_SERIAL_FROM_THIS(_SerialIo) \
  27. (EFI_PANDA_SERIAL_CONTEXT *)((VOID *)(_SerialIo) - \
  28. ((VOID *)(&(((EFI_PANDA_SERIAL_CONTEXT *)0)->SerialIo))))
  29. //
  30. // ---------------------------------------------------------------- Definitions
  31. //
  32. #define EFI_PANDA_SERIAL_MAGIC 0x72655350 // 'reSP'
  33. #define EFI_PANDA_DEFAULT_SERIAL_BAUD_RATE 115200
  34. //
  35. // ------------------------------------------------------ Data Type Definitions
  36. //
  37. /*++
  38. Structure Description:
  39. This structure describes the PandaBoard Serial I/O device context.
  40. Members:
  41. Magic - Stores the magic constand EFI_PANDA_SERIAL_MAGIC.
  42. Handle - Stores the handle to the device.
  43. DevicePath - Stores a pointer to the device path.
  44. Uart - Stores the UART context.
  45. SerialIo - Stores the Serial I/O protocol.
  46. Mode - Stores the mode information.
  47. --*/
  48. typedef struct _EFI_PANDA_SERIAL_CONTEXT {
  49. UINT32 Magic;
  50. EFI_HANDLE Handle;
  51. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  52. OMAP_UART_CONTEXT Uart;
  53. EFI_SERIAL_IO_PROTOCOL SerialIo;
  54. EFI_SERIAL_IO_MODE Mode;
  55. } EFI_PANDA_SERIAL_CONTEXT, *PEFI_PANDA_SERIAL_CONTEXT;
  56. /*++
  57. Structure Description:
  58. This structure defines the PandaBoard Serial I/O device path node.
  59. Members:
  60. DevicePath - Stores the standard vendor-specific device path.
  61. ControllerBase - Stores the controller base address.
  62. --*/
  63. typedef struct _EFI_PANDA_SERIAL_IO_DEVICE_PATH_NODE {
  64. VENDOR_DEVICE_PATH DevicePath;
  65. UINT32 ControllerBase;
  66. } EFI_PANDA_SERIAL_IO_DEVICE_PATH_NODE, *PEFI_PANDA_SERIAL_IO_DEVICE_PATH_NODE;
  67. /*++
  68. Structure Description:
  69. This structure defines the PandaBoard Serial I/O device path form.
  70. Members:
  71. Device - Stores the serial port device path node.
  72. End - Stores the end device path node.
  73. --*/
  74. typedef struct _EFI_PANDA_SERIAL_IO_DEVICE_PATH {
  75. EFI_PANDA_SERIAL_IO_DEVICE_PATH_NODE Device;
  76. EFI_DEVICE_PATH_PROTOCOL End;
  77. } PACKED EFI_PANDA_SERIAL_IO_DEVICE_PATH, *PEFI_PANDA_SERIAL_IO_DEVICE_PATH;
  78. //
  79. // ----------------------------------------------- Internal Function Prototypes
  80. //
  81. EFIAPI
  82. EFI_STATUS
  83. EfipPandaSerialReset (
  84. EFI_SERIAL_IO_PROTOCOL *This
  85. );
  86. EFIAPI
  87. EFI_STATUS
  88. EfipPandaSerialSetAttributes (
  89. EFI_SERIAL_IO_PROTOCOL *This,
  90. UINT64 BaudRate,
  91. UINT32 ReceiveFifoDepth,
  92. UINT32 Timeout,
  93. EFI_PARITY_TYPE Parity,
  94. UINT8 DataBits,
  95. EFI_STOP_BITS_TYPE StopBits
  96. );
  97. EFIAPI
  98. EFI_STATUS
  99. EfipPandaSerialSetControlBits (
  100. EFI_SERIAL_IO_PROTOCOL *This,
  101. UINT32 Control
  102. );
  103. EFIAPI
  104. EFI_STATUS
  105. EfipPandaSerialGetControlBits (
  106. EFI_SERIAL_IO_PROTOCOL *This,
  107. UINT32 *Control
  108. );
  109. EFIAPI
  110. EFI_STATUS
  111. EfipPandaSerialWrite (
  112. EFI_SERIAL_IO_PROTOCOL *This,
  113. UINTN *BufferSize,
  114. VOID *Buffer
  115. );
  116. EFIAPI
  117. EFI_STATUS
  118. EfipPandaSerialRead (
  119. EFI_SERIAL_IO_PROTOCOL *This,
  120. UINTN *BufferSize,
  121. VOID *Buffer
  122. );
  123. //
  124. // -------------------------------------------------------------------- Globals
  125. //
  126. //
  127. // Define the device path template.
  128. //
  129. EFI_PANDA_SERIAL_IO_DEVICE_PATH EfiPandaSerialIoDevicePathTemplate = {
  130. {
  131. {
  132. {
  133. HARDWARE_DEVICE_PATH,
  134. HW_VENDOR_DP,
  135. sizeof(EFI_PANDA_SERIAL_IO_DEVICE_PATH_NODE)
  136. },
  137. EFI_SERIAL_IO_PROTOCOL_GUID,
  138. },
  139. 0xFFFFFFFF
  140. },
  141. {
  142. END_DEVICE_PATH_TYPE,
  143. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  144. END_DEVICE_PATH_LENGTH
  145. }
  146. };
  147. EFI_PANDA_SERIAL_CONTEXT EfiPandaSerialTemplate = {
  148. EFI_PANDA_SERIAL_MAGIC,
  149. NULL,
  150. NULL,
  151. {0},
  152. {
  153. EFI_SERIAL_IO_PROTOCOL_REVISION,
  154. EfipPandaSerialReset,
  155. EfipPandaSerialSetAttributes,
  156. EfipPandaSerialSetControlBits,
  157. EfipPandaSerialGetControlBits,
  158. EfipPandaSerialWrite,
  159. EfipPandaSerialRead,
  160. NULL
  161. },
  162. {
  163. EFI_SERIAL_INPUT_BUFFER_EMPTY,
  164. 0,
  165. 0,
  166. 0,
  167. 8,
  168. DefaultParity,
  169. DefaultStopBits
  170. }
  171. };
  172. EFI_GUID EfiSerialIoProtocolGuid = EFI_SERIAL_IO_PROTOCOL_GUID;
  173. //
  174. // ------------------------------------------------------------------ Functions
  175. //
  176. EFI_STATUS
  177. EfipPandaEnumerateSerial (
  178. VOID
  179. )
  180. /*++
  181. Routine Description:
  182. This routine enumerates the serial port on the PandaBoard.
  183. Arguments:
  184. None.
  185. Return Value:
  186. EFI status code.
  187. --*/
  188. {
  189. VOID *ControllerBase;
  190. PEFI_PANDA_SERIAL_CONTEXT Device;
  191. PEFI_PANDA_SERIAL_IO_DEVICE_PATH DevicePath;
  192. EFI_STATUS Status;
  193. ControllerBase = (VOID *)OMAP4430_UART3_BASE;
  194. //
  195. // Allocate and initialize the context structure.
  196. //
  197. Status = EfiAllocatePool(EfiBootServicesData,
  198. sizeof(EFI_PANDA_SERIAL_CONTEXT),
  199. (VOID **)&Device);
  200. if (EFI_ERROR(Status)) {
  201. return Status;
  202. }
  203. EfiCopyMem(Device,
  204. &EfiPandaSerialTemplate,
  205. sizeof(EFI_PANDA_SERIAL_CONTEXT));
  206. Device->Handle = NULL;
  207. Device->SerialIo.Mode = &(Device->Mode);
  208. Device->Uart.UartBase = ControllerBase;
  209. //
  210. // Create the device path.
  211. //
  212. Status = EfiAllocatePool(EfiBootServicesData,
  213. sizeof(EFI_PANDA_SERIAL_IO_DEVICE_PATH),
  214. (VOID **)&DevicePath);
  215. if (EFI_ERROR(Status)) {
  216. goto PandaEnumerateSerialEnd;
  217. }
  218. EfiCopyMem(DevicePath,
  219. &EfiPandaSerialIoDevicePathTemplate,
  220. sizeof(EFI_PANDA_SERIAL_IO_DEVICE_PATH));
  221. DevicePath->Device.ControllerBase = (UINT32)ControllerBase;
  222. Device->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)DevicePath;
  223. Device->Mode.BaudRate = EFI_PANDA_DEFAULT_SERIAL_BAUD_RATE;
  224. Status = EfiInstallMultipleProtocolInterfaces(&(Device->Handle),
  225. &EfiDevicePathProtocolGuid,
  226. Device->DevicePath,
  227. &EfiSerialIoProtocolGuid,
  228. &(Device->SerialIo),
  229. NULL);
  230. PandaEnumerateSerialEnd:
  231. if (EFI_ERROR(Status)) {
  232. if (Device != NULL) {
  233. if (Device->DevicePath != NULL) {
  234. EfiFreePool(DevicePath);
  235. }
  236. EfiFreePool(Device);
  237. }
  238. }
  239. return Status;
  240. }
  241. //
  242. // --------------------------------------------------------- Internal Functions
  243. //
  244. EFIAPI
  245. EFI_STATUS
  246. EfipPandaSerialReset (
  247. EFI_SERIAL_IO_PROTOCOL *This
  248. )
  249. /*++
  250. Routine Description:
  251. This routine resets the serial device.
  252. Arguments:
  253. This - Supplies a pointer to the protocol instance.
  254. Return Value:
  255. EFI_SUCCESS if the device was reset.
  256. EFI_DEVICE_ERROR if the device could not be reset.
  257. --*/
  258. {
  259. PEFI_PANDA_SERIAL_CONTEXT Device;
  260. EFI_STATUS Status;
  261. Device = EFI_PANDA_SERIAL_FROM_THIS(This);
  262. Status = EfipUartOmapComputeDivisor(Device->Mode.BaudRate,
  263. &(Device->Uart.BaudRateRegister));
  264. if (EFI_ERROR(Status)) {
  265. return Status;
  266. }
  267. Status = EfipUartOmapInitialize(&(Device->Uart));
  268. return Status;
  269. }
  270. EFIAPI
  271. EFI_STATUS
  272. EfipPandaSerialSetAttributes (
  273. EFI_SERIAL_IO_PROTOCOL *This,
  274. UINT64 BaudRate,
  275. UINT32 ReceiveFifoDepth,
  276. UINT32 Timeout,
  277. EFI_PARITY_TYPE Parity,
  278. UINT8 DataBits,
  279. EFI_STOP_BITS_TYPE StopBits
  280. )
  281. /*++
  282. Routine Description:
  283. This routine sets the baud rate, receive FIFO depth, transmit/receive
  284. timeout, parity, data bits, and stop bits on a serial device.
  285. Arguments:
  286. This - Supplies a pointer to the protocol instance.
  287. BaudRate - Supplies the desired baud rate. A value of zero will use the
  288. default interface speed.
  289. ReceiveFifoDepth - Supplies the requested depth of the receive FIFO. A
  290. value of zero uses the default FIFO size.
  291. Timeout - Supplies the timeout in microseconds for attempting to receive
  292. a single character. A timeout of zero uses the default timeout.
  293. Parity - Supplies the type of parity to use on the device.
  294. DataBits - Supplies the number of bits per byte on the serial device. A
  295. value of zero uses a default value.
  296. StopBits - Supplies the number of stop bits to use on the serial device.
  297. A value of zero uses a default value.
  298. Return Value:
  299. EFI_SUCCESS if the device was reset.
  300. EFI_DEVICE_ERROR if the device could not be reset.
  301. --*/
  302. {
  303. PEFI_PANDA_SERIAL_CONTEXT Device;
  304. Device = EFI_PANDA_SERIAL_FROM_THIS(This);
  305. if (BaudRate == 0) {
  306. BaudRate = EFI_PANDA_DEFAULT_SERIAL_BAUD_RATE;
  307. }
  308. if ((ReceiveFifoDepth != 0) ||
  309. (Timeout != 0) ||
  310. ((Parity != DefaultParity) && (Parity != NoParity)) ||
  311. ((DataBits != 0) && (DataBits != 8)) ||
  312. ((StopBits != DefaultStopBits) && (StopBits != OneStopBit))) {
  313. return EFI_UNSUPPORTED;
  314. }
  315. Device->Mode.BaudRate = BaudRate;
  316. return This->Reset(This);
  317. }
  318. EFIAPI
  319. EFI_STATUS
  320. EfipPandaSerialSetControlBits (
  321. EFI_SERIAL_IO_PROTOCOL *This,
  322. UINT32 Control
  323. )
  324. /*++
  325. Routine Description:
  326. This routine sets the control bits on a serial device.
  327. Arguments:
  328. This - Supplies a pointer to the protocol instance.
  329. Control - Supplies the control bits to set.
  330. Return Value:
  331. EFI_SUCCESS if the new control bits were set.
  332. EFI_UNSUPPORTED if the serial device does not support this operation.
  333. EFI_DEVICE_ERROR if the device is not functioning properly.
  334. --*/
  335. {
  336. return EFI_UNSUPPORTED;
  337. }
  338. EFIAPI
  339. EFI_STATUS
  340. EfipPandaSerialGetControlBits (
  341. EFI_SERIAL_IO_PROTOCOL *This,
  342. UINT32 *Control
  343. )
  344. /*++
  345. Routine Description:
  346. This routine gets the control bits on a serial device.
  347. Arguments:
  348. This - Supplies a pointer to the protocol instance.
  349. Control - Supplies a pointer where the current control bits will be
  350. returned.
  351. Return Value:
  352. EFI_SUCCESS if the new control bits were set.
  353. EFI_DEVICE_ERROR if the device is not functioning properly.
  354. --*/
  355. {
  356. PEFI_PANDA_SERIAL_CONTEXT Device;
  357. BOOLEAN ReceiveDataAvailable;
  358. EFI_STATUS Status;
  359. Device = EFI_PANDA_SERIAL_FROM_THIS(This);
  360. if (Device->Uart.BaudRateRegister == 0) {
  361. Status = This->Reset(This);
  362. if (EFI_ERROR(Status)) {
  363. return Status;
  364. }
  365. }
  366. Status = EfipUartOmapGetStatus(&(Device->Uart), &ReceiveDataAvailable);
  367. if (EFI_ERROR(Status)) {
  368. return Status;
  369. }
  370. *Control = 0;
  371. if (ReceiveDataAvailable == FALSE) {
  372. *Control |= EFI_SERIAL_INPUT_BUFFER_EMPTY;
  373. }
  374. return EFI_SUCCESS;
  375. }
  376. EFIAPI
  377. EFI_STATUS
  378. EfipPandaSerialWrite (
  379. EFI_SERIAL_IO_PROTOCOL *This,
  380. UINTN *BufferSize,
  381. VOID *Buffer
  382. )
  383. /*++
  384. Routine Description:
  385. This routine writes data to a serial device.
  386. Arguments:
  387. This - Supplies a pointer to the protocol instance.
  388. BufferSize - Supplies a pointer that on input contains the size of the
  389. buffer. On output, the number of bytes successfully written will be
  390. returned.
  391. Buffer - Supplies a pointer to the data to write.
  392. Return Value:
  393. EFI_SUCCESS if the data was written
  394. EFI_DEVICE_ERROR if the device is not functioning properly.
  395. EFI_TIMEOUT if the operation timed out before the data could be written.
  396. --*/
  397. {
  398. PEFI_PANDA_SERIAL_CONTEXT Device;
  399. UINTN Size;
  400. EFI_STATUS Status;
  401. Size = *BufferSize;
  402. *BufferSize = 0;
  403. Device = EFI_PANDA_SERIAL_FROM_THIS(This);
  404. if (Device->Uart.BaudRateRegister == 0) {
  405. Status = This->Reset(This);
  406. if (EFI_ERROR(Status)) {
  407. return Status;
  408. }
  409. }
  410. Status = EfipUartOmapTransmit(&(Device->Uart), Buffer, Size);
  411. if (EFI_ERROR(Status)) {
  412. return Status;
  413. }
  414. *BufferSize = Size;
  415. return EFI_SUCCESS;
  416. }
  417. EFIAPI
  418. EFI_STATUS
  419. EfipPandaSerialRead (
  420. EFI_SERIAL_IO_PROTOCOL *This,
  421. UINTN *BufferSize,
  422. VOID *Buffer
  423. )
  424. /*++
  425. Routine Description:
  426. This routine reads data from a serial device.
  427. Arguments:
  428. This - Supplies a pointer to the protocol instance.
  429. BufferSize - Supplies a pointer that on input contains the size of the
  430. buffer. On output, the number of bytes successfully read will be
  431. returned.
  432. Buffer - Supplies a pointer where the read data will be returned on success.
  433. Return Value:
  434. EFI_SUCCESS if the data was read.
  435. EFI_DEVICE_ERROR if the device is not functioning properly.
  436. EFI_TIMEOUT if the operation timed out before the data could be read.
  437. --*/
  438. {
  439. PEFI_PANDA_SERIAL_CONTEXT Device;
  440. EFI_STATUS Status;
  441. Device = EFI_PANDA_SERIAL_FROM_THIS(This);
  442. if (Device->Uart.BaudRateRegister == 0) {
  443. Status = This->Reset(This);
  444. if (EFI_ERROR(Status)) {
  445. return Status;
  446. }
  447. }
  448. Status = EfipUartOmapReceive(&(Device->Uart), Buffer, BufferSize);
  449. if (Status == EFI_NOT_READY) {
  450. Status = EFI_TIMEOUT;
  451. }
  452. if (EFI_ERROR(Status)) {
  453. return Status;
  454. }
  455. return EFI_SUCCESS;
  456. }