serial.c 15 KB

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