serial_pl01x.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000
  4. * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
  5. *
  6. * (C) Copyright 2004
  7. * ARM Ltd.
  8. * Philippe Robin, <philippe.robin@arm.com>
  9. */
  10. /* Simple U-Boot driver for the PrimeCell PL010/PL011 UARTs */
  11. #include <common.h>
  12. #include <asm/global_data.h>
  13. /* For get_bus_freq() */
  14. #include <clock_legacy.h>
  15. #include <dm.h>
  16. #include <clk.h>
  17. #include <errno.h>
  18. #include <watchdog.h>
  19. #include <asm/io.h>
  20. #include <serial.h>
  21. #include <dm/device_compat.h>
  22. #include <dm/platform_data/serial_pl01x.h>
  23. #include <linux/compiler.h>
  24. #include "serial_pl01x_internal.h"
  25. DECLARE_GLOBAL_DATA_PTR;
  26. #if !CONFIG_IS_ENABLED(DM_SERIAL)
  27. static volatile unsigned char *const port[] = CFG_PL01x_PORTS;
  28. static enum pl01x_type pl01x_type __section(".data");
  29. static struct pl01x_regs *base_regs __section(".data");
  30. #define NUM_PORTS (sizeof(port)/sizeof(port[0]))
  31. #endif
  32. static int pl01x_putc(struct pl01x_regs *regs, char c)
  33. {
  34. /* Wait until there is space in the FIFO */
  35. if (readl(&regs->fr) & UART_PL01x_FR_TXFF)
  36. return -EAGAIN;
  37. /* Send the character */
  38. writel(c, &regs->dr);
  39. return 0;
  40. }
  41. static int pl01x_getc(struct pl01x_regs *regs)
  42. {
  43. unsigned int data;
  44. /* Wait until there is data in the FIFO */
  45. if (readl(&regs->fr) & UART_PL01x_FR_RXFE)
  46. return -EAGAIN;
  47. data = readl(&regs->dr);
  48. /* Check for an error flag */
  49. if (data & 0xFFFFFF00) {
  50. /* Clear the error */
  51. writel(0xFFFFFFFF, &regs->ecr);
  52. return -1;
  53. }
  54. return (int) data;
  55. }
  56. static int pl01x_tstc(struct pl01x_regs *regs)
  57. {
  58. schedule();
  59. return !(readl(&regs->fr) & UART_PL01x_FR_RXFE);
  60. }
  61. static int pl01x_generic_serial_init(struct pl01x_regs *regs,
  62. enum pl01x_type type)
  63. {
  64. switch (type) {
  65. case TYPE_PL010:
  66. /* disable everything */
  67. writel(0, &regs->pl010_cr);
  68. break;
  69. case TYPE_PL011:
  70. /* disable everything */
  71. writel(0, &regs->pl011_cr);
  72. break;
  73. default:
  74. return -EINVAL;
  75. }
  76. return 0;
  77. }
  78. static int pl011_set_line_control(struct pl01x_regs *regs)
  79. {
  80. unsigned int lcr;
  81. /*
  82. * Internal update of baud rate register require line
  83. * control register write
  84. */
  85. lcr = UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN;
  86. writel(lcr, &regs->pl011_lcrh);
  87. return 0;
  88. }
  89. static int pl01x_generic_setbrg(struct pl01x_regs *regs, enum pl01x_type type,
  90. int clock, int baudrate)
  91. {
  92. switch (type) {
  93. case TYPE_PL010: {
  94. unsigned int divisor;
  95. /* disable everything */
  96. writel(0, &regs->pl010_cr);
  97. switch (baudrate) {
  98. case 9600:
  99. divisor = UART_PL010_BAUD_9600;
  100. break;
  101. case 19200:
  102. divisor = UART_PL010_BAUD_19200;
  103. break;
  104. case 38400:
  105. divisor = UART_PL010_BAUD_38400;
  106. break;
  107. case 57600:
  108. divisor = UART_PL010_BAUD_57600;
  109. break;
  110. case 115200:
  111. divisor = UART_PL010_BAUD_115200;
  112. break;
  113. default:
  114. divisor = UART_PL010_BAUD_38400;
  115. }
  116. writel((divisor & 0xf00) >> 8, &regs->pl010_lcrm);
  117. writel(divisor & 0xff, &regs->pl010_lcrl);
  118. /*
  119. * Set line control for the PL010 to be 8 bits, 1 stop bit,
  120. * no parity, fifo enabled
  121. */
  122. writel(UART_PL010_LCRH_WLEN_8 | UART_PL010_LCRH_FEN,
  123. &regs->pl010_lcrh);
  124. /* Finally, enable the UART */
  125. writel(UART_PL010_CR_UARTEN, &regs->pl010_cr);
  126. break;
  127. }
  128. case TYPE_PL011: {
  129. unsigned int temp;
  130. unsigned int divider;
  131. unsigned int remainder;
  132. unsigned int fraction;
  133. /* Without a valid clock rate we cannot set up the baudrate. */
  134. if (clock) {
  135. /*
  136. * Set baud rate
  137. *
  138. * IBRD = UART_CLK / (16 * BAUD_RATE)
  139. * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE)))
  140. * / (16 * BAUD_RATE))
  141. */
  142. temp = 16 * baudrate;
  143. divider = clock / temp;
  144. remainder = clock % temp;
  145. temp = (8 * remainder) / baudrate;
  146. fraction = (temp >> 1) + (temp & 1);
  147. writel(divider, &regs->pl011_ibrd);
  148. writel(fraction, &regs->pl011_fbrd);
  149. }
  150. pl011_set_line_control(regs);
  151. /* Finally, enable the UART */
  152. writel(UART_PL011_CR_UARTEN | UART_PL011_CR_TXE |
  153. UART_PL011_CR_RXE | UART_PL011_CR_RTS, &regs->pl011_cr);
  154. break;
  155. }
  156. default:
  157. return -EINVAL;
  158. }
  159. return 0;
  160. }
  161. #if !CONFIG_IS_ENABLED(DM_SERIAL)
  162. static void pl01x_serial_init_baud(int baudrate)
  163. {
  164. int clock = 0;
  165. #if defined(CONFIG_PL011_SERIAL)
  166. pl01x_type = TYPE_PL011;
  167. clock = CFG_PL011_CLOCK;
  168. #endif
  169. base_regs = (struct pl01x_regs *)port[CONFIG_CONS_INDEX];
  170. pl01x_generic_serial_init(base_regs, pl01x_type);
  171. pl01x_generic_setbrg(base_regs, pl01x_type, clock, baudrate);
  172. }
  173. /*
  174. * Integrator AP has two UARTs, we use the first one, at 38400-8-N-1
  175. * Integrator CP has two UARTs, use the first one, at 38400-8-N-1
  176. * Versatile PB has four UARTs.
  177. */
  178. int pl01x_serial_init(void)
  179. {
  180. pl01x_serial_init_baud(CONFIG_BAUDRATE);
  181. return 0;
  182. }
  183. static void pl01x_serial_putc(const char c)
  184. {
  185. if (c == '\n')
  186. while (pl01x_putc(base_regs, '\r') == -EAGAIN);
  187. while (pl01x_putc(base_regs, c) == -EAGAIN);
  188. }
  189. static int pl01x_serial_getc(void)
  190. {
  191. while (1) {
  192. int ch = pl01x_getc(base_regs);
  193. if (ch == -EAGAIN) {
  194. schedule();
  195. continue;
  196. }
  197. return ch;
  198. }
  199. }
  200. static int pl01x_serial_tstc(void)
  201. {
  202. return pl01x_tstc(base_regs);
  203. }
  204. static void pl01x_serial_setbrg(void)
  205. {
  206. /*
  207. * Flush FIFO and wait for non-busy before changing baudrate to avoid
  208. * crap in console
  209. */
  210. while (!(readl(&base_regs->fr) & UART_PL01x_FR_TXFE))
  211. schedule();
  212. while (readl(&base_regs->fr) & UART_PL01x_FR_BUSY)
  213. schedule();
  214. pl01x_serial_init_baud(gd->baudrate);
  215. }
  216. static struct serial_device pl01x_serial_drv = {
  217. .name = "pl01x_serial",
  218. .start = pl01x_serial_init,
  219. .stop = NULL,
  220. .setbrg = pl01x_serial_setbrg,
  221. .putc = pl01x_serial_putc,
  222. .puts = default_serial_puts,
  223. .getc = pl01x_serial_getc,
  224. .tstc = pl01x_serial_tstc,
  225. };
  226. void pl01x_serial_initialize(void)
  227. {
  228. serial_register(&pl01x_serial_drv);
  229. }
  230. __weak struct serial_device *default_serial_console(void)
  231. {
  232. return &pl01x_serial_drv;
  233. }
  234. #else
  235. int pl01x_serial_setbrg(struct udevice *dev, int baudrate)
  236. {
  237. struct pl01x_serial_plat *plat = dev_get_plat(dev);
  238. struct pl01x_priv *priv = dev_get_priv(dev);
  239. if (!plat->skip_init) {
  240. pl01x_generic_setbrg(priv->regs, priv->type, plat->clock,
  241. baudrate);
  242. }
  243. return 0;
  244. }
  245. int pl01x_serial_probe(struct udevice *dev)
  246. {
  247. struct pl01x_serial_plat *plat = dev_get_plat(dev);
  248. struct pl01x_priv *priv = dev_get_priv(dev);
  249. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  250. struct dtd_serial_pl01x *dtplat = &plat->dtplat;
  251. priv->regs = (struct pl01x_regs *)dtplat->reg[0];
  252. plat->type = dtplat->type;
  253. #else
  254. priv->regs = (struct pl01x_regs *)plat->base;
  255. #endif
  256. priv->type = plat->type;
  257. if (!plat->skip_init)
  258. return pl01x_generic_serial_init(priv->regs, priv->type);
  259. else
  260. return 0;
  261. }
  262. int pl01x_serial_getc(struct udevice *dev)
  263. {
  264. struct pl01x_priv *priv = dev_get_priv(dev);
  265. return pl01x_getc(priv->regs);
  266. }
  267. int pl01x_serial_putc(struct udevice *dev, const char ch)
  268. {
  269. struct pl01x_priv *priv = dev_get_priv(dev);
  270. return pl01x_putc(priv->regs, ch);
  271. }
  272. int pl01x_serial_pending(struct udevice *dev, bool input)
  273. {
  274. struct pl01x_priv *priv = dev_get_priv(dev);
  275. unsigned int fr = readl(&priv->regs->fr);
  276. if (input)
  277. return pl01x_tstc(priv->regs);
  278. else
  279. return fr & UART_PL01x_FR_TXFE ? 0 : 1;
  280. }
  281. static const struct dm_serial_ops pl01x_serial_ops = {
  282. .putc = pl01x_serial_putc,
  283. .pending = pl01x_serial_pending,
  284. .getc = pl01x_serial_getc,
  285. .setbrg = pl01x_serial_setbrg,
  286. };
  287. #if CONFIG_IS_ENABLED(OF_REAL)
  288. static const struct udevice_id pl01x_serial_id[] ={
  289. {.compatible = "arm,pl011", .data = TYPE_PL011},
  290. {.compatible = "arm,pl010", .data = TYPE_PL010},
  291. {}
  292. };
  293. #ifndef CFG_PL011_CLOCK
  294. #define CFG_PL011_CLOCK 0
  295. #endif
  296. int pl01x_serial_of_to_plat(struct udevice *dev)
  297. {
  298. struct pl01x_serial_plat *plat = dev_get_plat(dev);
  299. struct clk clk;
  300. fdt_addr_t addr;
  301. int ret;
  302. addr = dev_read_addr(dev);
  303. if (addr == FDT_ADDR_T_NONE)
  304. return -EINVAL;
  305. plat->base = addr;
  306. plat->clock = dev_read_u32_default(dev, "clock", CFG_PL011_CLOCK);
  307. ret = clk_get_by_index(dev, 0, &clk);
  308. if (!ret) {
  309. ret = clk_enable(&clk);
  310. if (ret && ret != -ENOSYS) {
  311. dev_err(dev, "failed to enable clock\n");
  312. return ret;
  313. }
  314. plat->clock = clk_get_rate(&clk);
  315. if (IS_ERR_VALUE(plat->clock)) {
  316. dev_err(dev, "failed to get rate\n");
  317. return plat->clock;
  318. }
  319. debug("%s: CLK %d\n", __func__, plat->clock);
  320. }
  321. plat->type = dev_get_driver_data(dev);
  322. plat->skip_init = dev_read_bool(dev, "skip-init");
  323. return 0;
  324. }
  325. #endif
  326. U_BOOT_DRIVER(serial_pl01x) = {
  327. .name = "serial_pl01x",
  328. .id = UCLASS_SERIAL,
  329. #if CONFIG_IS_ENABLED(OF_REAL)
  330. .of_match = of_match_ptr(pl01x_serial_id),
  331. .of_to_plat = of_match_ptr(pl01x_serial_of_to_plat),
  332. #endif
  333. .plat_auto = sizeof(struct pl01x_serial_plat),
  334. .probe = pl01x_serial_probe,
  335. .ops = &pl01x_serial_ops,
  336. .flags = DM_FLAG_PRE_RELOC,
  337. .priv_auto = sizeof(struct pl01x_priv),
  338. };
  339. DM_DRIVER_ALIAS(serial_pl01x, arm_pl011)
  340. DM_DRIVER_ALIAS(serial_pl01x, arm_pl010)
  341. #endif
  342. #if defined(CONFIG_DEBUG_UART_PL010) || defined(CONFIG_DEBUG_UART_PL011)
  343. #include <debug_uart.h>
  344. static void _debug_uart_init(void)
  345. {
  346. #ifndef CONFIG_DEBUG_UART_SKIP_INIT
  347. struct pl01x_regs *regs = (struct pl01x_regs *)CONFIG_VAL(DEBUG_UART_BASE);
  348. enum pl01x_type type;
  349. if (IS_ENABLED(CONFIG_DEBUG_UART_PL011))
  350. type = TYPE_PL011;
  351. else
  352. type = TYPE_PL010;
  353. pl01x_generic_serial_init(regs, type);
  354. pl01x_generic_setbrg(regs, type,
  355. CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
  356. #endif
  357. }
  358. static inline void _debug_uart_putc(int ch)
  359. {
  360. struct pl01x_regs *regs = (struct pl01x_regs *)CONFIG_VAL(DEBUG_UART_BASE);
  361. while (pl01x_putc(regs, ch) == -EAGAIN)
  362. ;
  363. }
  364. DEBUG_UART_FUNCS
  365. #endif