serial_pl01x.c 8.7 KB

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