spcr.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012, Intel Corporation
  4. * Copyright (c) 2015, Red Hat, Inc.
  5. * Copyright (c) 2015, 2016 Linaro Ltd.
  6. */
  7. #define pr_fmt(fmt) "ACPI: SPCR: " fmt
  8. #include <linux/acpi.h>
  9. #include <linux/console.h>
  10. #include <linux/kernel.h>
  11. #include <linux/serial_core.h>
  12. /*
  13. * Erratum 44 for QDF2432v1 and QDF2400v1 SoCs describes the BUSY bit as
  14. * occasionally getting stuck as 1. To avoid the potential for a hang, check
  15. * TXFE == 0 instead of BUSY == 1. This may not be suitable for all UART
  16. * implementations, so only do so if an affected platform is detected in
  17. * acpi_parse_spcr().
  18. */
  19. bool qdf2400_e44_present;
  20. EXPORT_SYMBOL(qdf2400_e44_present);
  21. /*
  22. * Some Qualcomm Datacenter Technologies SoCs have a defective UART BUSY bit.
  23. * Detect them by examining the OEM fields in the SPCR header, similar to PCI
  24. * quirk detection in pci_mcfg.c.
  25. */
  26. static bool qdf2400_erratum_44_present(struct acpi_table_header *h)
  27. {
  28. if (memcmp(h->oem_id, "QCOM ", ACPI_OEM_ID_SIZE))
  29. return false;
  30. if (!memcmp(h->oem_table_id, "QDF2432 ", ACPI_OEM_TABLE_ID_SIZE))
  31. return true;
  32. if (!memcmp(h->oem_table_id, "QDF2400 ", ACPI_OEM_TABLE_ID_SIZE) &&
  33. h->oem_revision == 1)
  34. return true;
  35. return false;
  36. }
  37. /*
  38. * APM X-Gene v1 and v2 UART hardware is an 16550 like device but has its
  39. * register aligned to 32-bit. In addition, the BIOS also encoded the
  40. * access width to be 8 bits. This function detects this errata condition.
  41. */
  42. static bool xgene_8250_erratum_present(struct acpi_table_spcr *tb)
  43. {
  44. bool xgene_8250 = false;
  45. if (tb->interface_type != ACPI_DBG2_16550_COMPATIBLE)
  46. return false;
  47. if (memcmp(tb->header.oem_id, "APMC0D", ACPI_OEM_ID_SIZE) &&
  48. memcmp(tb->header.oem_id, "HPE ", ACPI_OEM_ID_SIZE))
  49. return false;
  50. if (!memcmp(tb->header.oem_table_id, "XGENESPC",
  51. ACPI_OEM_TABLE_ID_SIZE) && tb->header.oem_revision == 0)
  52. xgene_8250 = true;
  53. if (!memcmp(tb->header.oem_table_id, "ProLiant",
  54. ACPI_OEM_TABLE_ID_SIZE) && tb->header.oem_revision == 1)
  55. xgene_8250 = true;
  56. return xgene_8250;
  57. }
  58. /**
  59. * acpi_parse_spcr() - parse ACPI SPCR table and add preferred console
  60. *
  61. * @enable_earlycon: set up earlycon for the console specified by the table
  62. * @enable_console: setup the console specified by the table.
  63. *
  64. * For the architectures with support for ACPI, CONFIG_ACPI_SPCR_TABLE may be
  65. * defined to parse ACPI SPCR table. As a result of the parsing preferred
  66. * console is registered and if @enable_earlycon is true, earlycon is set up.
  67. * If @enable_console is true the system console is also configured.
  68. *
  69. * When CONFIG_ACPI_SPCR_TABLE is defined, this function should be called
  70. * from arch initialization code as soon as the DT/ACPI decision is made.
  71. *
  72. */
  73. int __init acpi_parse_spcr(bool enable_earlycon, bool enable_console)
  74. {
  75. static char opts[64];
  76. struct acpi_table_spcr *table;
  77. acpi_status status;
  78. char *uart;
  79. char *iotype;
  80. int baud_rate;
  81. int err;
  82. if (acpi_disabled)
  83. return -ENODEV;
  84. status = acpi_get_table(ACPI_SIG_SPCR, 0,
  85. (struct acpi_table_header **)&table);
  86. if (ACPI_FAILURE(status))
  87. return -ENOENT;
  88. if (table->header.revision < 2)
  89. pr_info("SPCR table version %d\n", table->header.revision);
  90. if (table->serial_port.space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  91. u32 bit_width = table->serial_port.access_width;
  92. if (bit_width > ACPI_ACCESS_BIT_MAX) {
  93. pr_err("Unacceptable wide SPCR Access Width. Defaulting to byte size\n");
  94. bit_width = ACPI_ACCESS_BIT_DEFAULT;
  95. }
  96. switch (ACPI_ACCESS_BIT_WIDTH((bit_width))) {
  97. default:
  98. pr_err("Unexpected SPCR Access Width. Defaulting to byte size\n");
  99. fallthrough;
  100. case 8:
  101. iotype = "mmio";
  102. break;
  103. case 16:
  104. iotype = "mmio16";
  105. break;
  106. case 32:
  107. iotype = "mmio32";
  108. break;
  109. }
  110. } else
  111. iotype = "io";
  112. switch (table->interface_type) {
  113. case ACPI_DBG2_ARM_SBSA_32BIT:
  114. iotype = "mmio32";
  115. fallthrough;
  116. case ACPI_DBG2_ARM_PL011:
  117. case ACPI_DBG2_ARM_SBSA_GENERIC:
  118. case ACPI_DBG2_BCM2835:
  119. uart = "pl011";
  120. break;
  121. case ACPI_DBG2_16550_COMPATIBLE:
  122. case ACPI_DBG2_16550_SUBSET:
  123. case ACPI_DBG2_16550_WITH_GAS:
  124. case ACPI_DBG2_16550_NVIDIA:
  125. uart = "uart";
  126. break;
  127. default:
  128. err = -ENOENT;
  129. goto done;
  130. }
  131. switch (table->baud_rate) {
  132. case 0:
  133. /*
  134. * SPCR 1.04 defines 0 as a preconfigured state of UART.
  135. * Assume firmware or bootloader configures console correctly.
  136. */
  137. baud_rate = 0;
  138. break;
  139. case 3:
  140. baud_rate = 9600;
  141. break;
  142. case 4:
  143. baud_rate = 19200;
  144. break;
  145. case 6:
  146. baud_rate = 57600;
  147. break;
  148. case 7:
  149. baud_rate = 115200;
  150. break;
  151. default:
  152. err = -ENOENT;
  153. goto done;
  154. }
  155. /*
  156. * If the E44 erratum is required, then we need to tell the pl011
  157. * driver to implement the work-around.
  158. *
  159. * The global variable is used by the probe function when it
  160. * creates the UARTs, whether or not they're used as a console.
  161. *
  162. * If the user specifies "traditional" earlycon, the qdf2400_e44
  163. * console name matches the EARLYCON_DECLARE() statement, and
  164. * SPCR is not used. Parameter "earlycon" is false.
  165. *
  166. * If the user specifies "SPCR" earlycon, then we need to update
  167. * the console name so that it also says "qdf2400_e44". Parameter
  168. * "earlycon" is true.
  169. *
  170. * For consistency, if we change the console name, then we do it
  171. * for everyone, not just earlycon.
  172. */
  173. if (qdf2400_erratum_44_present(&table->header)) {
  174. qdf2400_e44_present = true;
  175. if (enable_earlycon)
  176. uart = "qdf2400_e44";
  177. }
  178. if (xgene_8250_erratum_present(table)) {
  179. iotype = "mmio32";
  180. /* for xgene v1 and v2 we don't know the clock rate of the
  181. * UART so don't attempt to change to the baud rate state
  182. * in the table because driver cannot calculate the dividers
  183. */
  184. baud_rate = 0;
  185. }
  186. if (!baud_rate) {
  187. snprintf(opts, sizeof(opts), "%s,%s,0x%llx", uart, iotype,
  188. table->serial_port.address);
  189. } else {
  190. snprintf(opts, sizeof(opts), "%s,%s,0x%llx,%d", uart, iotype,
  191. table->serial_port.address, baud_rate);
  192. }
  193. pr_info("console: %s\n", opts);
  194. if (enable_earlycon)
  195. setup_earlycon(opts);
  196. if (enable_console)
  197. err = add_preferred_console(uart, 0, opts + strlen(uart) + 1);
  198. else
  199. err = 0;
  200. done:
  201. acpi_put_table((struct acpi_table_header *)table);
  202. return err;
  203. }