serial.c 14 KB

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