serial_lpuart.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright 2013 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <watchdog.h>
  9. #include <asm/io.h>
  10. #include <serial.h>
  11. #include <linux/compiler.h>
  12. #include <asm/arch/imx-regs.h>
  13. #include <asm/arch/clock.h>
  14. #define US1_TDRE (1 << 7)
  15. #define US1_RDRF (1 << 5)
  16. #define US1_OR (1 << 3)
  17. #define UC2_TE (1 << 3)
  18. #define UC2_RE (1 << 2)
  19. #define CFIFO_TXFLUSH (1 << 7)
  20. #define CFIFO_RXFLUSH (1 << 6)
  21. #define SFIFO_RXOF (1 << 2)
  22. #define SFIFO_RXUF (1 << 0)
  23. #define STAT_LBKDIF (1 << 31)
  24. #define STAT_RXEDGIF (1 << 30)
  25. #define STAT_TDRE (1 << 23)
  26. #define STAT_RDRF (1 << 21)
  27. #define STAT_IDLE (1 << 20)
  28. #define STAT_OR (1 << 19)
  29. #define STAT_NF (1 << 18)
  30. #define STAT_FE (1 << 17)
  31. #define STAT_PF (1 << 16)
  32. #define STAT_MA1F (1 << 15)
  33. #define STAT_MA2F (1 << 14)
  34. #define STAT_FLAGS (STAT_LBKDIF | STAT_RXEDGIF | STAT_IDLE | STAT_OR | \
  35. STAT_NF | STAT_FE | STAT_PF | STAT_MA1F | STAT_MA2F)
  36. #define CTRL_TE (1 << 19)
  37. #define CTRL_RE (1 << 18)
  38. #define FIFO_TXFE 0x80
  39. #define FIFO_RXFE 0x40
  40. #define WATER_TXWATER_OFF 1
  41. #define WATER_RXWATER_OFF 16
  42. DECLARE_GLOBAL_DATA_PTR;
  43. struct lpuart_serial_platdata {
  44. struct lpuart_fsl *reg;
  45. };
  46. #ifndef CONFIG_LPUART_32B_REG
  47. static void _lpuart_serial_setbrg(struct lpuart_fsl *base, int baudrate)
  48. {
  49. u32 clk = mxc_get_clock(MXC_UART_CLK);
  50. u16 sbr;
  51. sbr = (u16)(clk / (16 * baudrate));
  52. /* place adjustment later - n/32 BRFA */
  53. __raw_writeb(sbr >> 8, &base->ubdh);
  54. __raw_writeb(sbr & 0xff, &base->ubdl);
  55. }
  56. static int _lpuart_serial_getc(struct lpuart_fsl *base)
  57. {
  58. while (!(__raw_readb(&base->us1) & (US1_RDRF | US1_OR)))
  59. WATCHDOG_RESET();
  60. barrier();
  61. return __raw_readb(&base->ud);
  62. }
  63. static void _lpuart_serial_putc(struct lpuart_fsl *base, const char c)
  64. {
  65. while (!(__raw_readb(&base->us1) & US1_TDRE))
  66. WATCHDOG_RESET();
  67. __raw_writeb(c, &base->ud);
  68. }
  69. /* Test whether a character is in the RX buffer */
  70. static int _lpuart_serial_tstc(struct lpuart_fsl *base)
  71. {
  72. if (__raw_readb(&base->urcfifo) == 0)
  73. return 0;
  74. return 1;
  75. }
  76. /*
  77. * Initialise the serial port with the given baudrate. The settings
  78. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  79. */
  80. static int _lpuart_serial_init(struct lpuart_fsl *base)
  81. {
  82. u8 ctrl;
  83. ctrl = __raw_readb(&base->uc2);
  84. ctrl &= ~UC2_RE;
  85. ctrl &= ~UC2_TE;
  86. __raw_writeb(ctrl, &base->uc2);
  87. __raw_writeb(0, &base->umodem);
  88. __raw_writeb(0, &base->uc1);
  89. /* Disable FIFO and flush buffer */
  90. __raw_writeb(0x0, &base->upfifo);
  91. __raw_writeb(0x0, &base->utwfifo);
  92. __raw_writeb(0x1, &base->urwfifo);
  93. __raw_writeb(CFIFO_TXFLUSH | CFIFO_RXFLUSH, &base->ucfifo);
  94. /* provide data bits, parity, stop bit, etc */
  95. _lpuart_serial_setbrg(base, gd->baudrate);
  96. __raw_writeb(UC2_RE | UC2_TE, &base->uc2);
  97. return 0;
  98. }
  99. static int lpuart_serial_setbrg(struct udevice *dev, int baudrate)
  100. {
  101. struct lpuart_serial_platdata *plat = dev->platdata;
  102. struct lpuart_fsl *reg = plat->reg;
  103. _lpuart_serial_setbrg(reg, baudrate);
  104. return 0;
  105. }
  106. static int lpuart_serial_getc(struct udevice *dev)
  107. {
  108. struct lpuart_serial_platdata *plat = dev->platdata;
  109. struct lpuart_fsl *reg = plat->reg;
  110. return _lpuart_serial_getc(reg);
  111. }
  112. static int lpuart_serial_putc(struct udevice *dev, const char c)
  113. {
  114. struct lpuart_serial_platdata *plat = dev->platdata;
  115. struct lpuart_fsl *reg = plat->reg;
  116. _lpuart_serial_putc(reg, c);
  117. return 0;
  118. }
  119. static int lpuart_serial_pending(struct udevice *dev, bool input)
  120. {
  121. struct lpuart_serial_platdata *plat = dev->platdata;
  122. struct lpuart_fsl *reg = plat->reg;
  123. if (input)
  124. return _lpuart_serial_tstc(reg);
  125. else
  126. return __raw_readb(&reg->us1) & US1_TDRE ? 0 : 1;
  127. }
  128. static int lpuart_serial_probe(struct udevice *dev)
  129. {
  130. struct lpuart_serial_platdata *plat = dev->platdata;
  131. struct lpuart_fsl *reg = plat->reg;
  132. return _lpuart_serial_init(reg);
  133. }
  134. #else
  135. u32 __weak get_lpuart_clk(void)
  136. {
  137. return CONFIG_SYS_CLK_FREQ;
  138. }
  139. static void _lpuart32_serial_setbrg(struct lpuart_fsl *base, int baudrate)
  140. {
  141. u32 clk = get_lpuart_clk();
  142. u32 sbr;
  143. sbr = (clk / (16 * baudrate));
  144. /* place adjustment later - n/32 BRFA */
  145. out_be32(&base->baud, sbr);
  146. }
  147. static int _lpuart32_serial_getc(struct lpuart_fsl *base)
  148. {
  149. u32 stat;
  150. while (((stat = in_be32(&base->stat)) & STAT_RDRF) == 0) {
  151. out_be32(&base->stat, STAT_FLAGS);
  152. WATCHDOG_RESET();
  153. }
  154. return in_be32(&base->data) & 0x3ff;
  155. }
  156. static void _lpuart32_serial_putc(struct lpuart_fsl *base, const char c)
  157. {
  158. while (!(in_be32(&base->stat) & STAT_TDRE))
  159. WATCHDOG_RESET();
  160. out_be32(&base->data, c);
  161. }
  162. /* Test whether a character is in the RX buffer */
  163. static int _lpuart32_serial_tstc(struct lpuart_fsl *base)
  164. {
  165. if ((in_be32(&base->water) >> 24) == 0)
  166. return 0;
  167. return 1;
  168. }
  169. /*
  170. * Initialise the serial port with the given baudrate. The settings
  171. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  172. */
  173. static int _lpuart32_serial_init(struct lpuart_fsl *base)
  174. {
  175. u8 ctrl;
  176. ctrl = in_be32(&base->ctrl);
  177. ctrl &= ~CTRL_RE;
  178. ctrl &= ~CTRL_TE;
  179. out_be32(&base->ctrl, ctrl);
  180. out_be32(&base->modir, 0);
  181. out_be32(&base->fifo, ~(FIFO_TXFE | FIFO_RXFE));
  182. out_be32(&base->match, 0);
  183. /* provide data bits, parity, stop bit, etc */
  184. _lpuart32_serial_setbrg(base, gd->baudrate);
  185. out_be32(&base->ctrl, CTRL_RE | CTRL_TE);
  186. return 0;
  187. }
  188. static int lpuart32_serial_setbrg(struct udevice *dev, int baudrate)
  189. {
  190. struct lpuart_serial_platdata *plat = dev->platdata;
  191. struct lpuart_fsl *reg = plat->reg;
  192. _lpuart32_serial_setbrg(reg, baudrate);
  193. return 0;
  194. }
  195. static int lpuart32_serial_getc(struct udevice *dev)
  196. {
  197. struct lpuart_serial_platdata *plat = dev->platdata;
  198. struct lpuart_fsl *reg = plat->reg;
  199. return _lpuart32_serial_getc(reg);
  200. }
  201. static int lpuart32_serial_putc(struct udevice *dev, const char c)
  202. {
  203. struct lpuart_serial_platdata *plat = dev->platdata;
  204. struct lpuart_fsl *reg = plat->reg;
  205. _lpuart32_serial_putc(reg, c);
  206. return 0;
  207. }
  208. static int lpuart32_serial_pending(struct udevice *dev, bool input)
  209. {
  210. struct lpuart_serial_platdata *plat = dev->platdata;
  211. struct lpuart_fsl *reg = plat->reg;
  212. if (input)
  213. return _lpuart32_serial_tstc(reg);
  214. else
  215. return in_be32(&reg->stat) & STAT_TDRE ? 0 : 1;
  216. }
  217. static int lpuart32_serial_probe(struct udevice *dev)
  218. {
  219. struct lpuart_serial_platdata *plat = dev->platdata;
  220. struct lpuart_fsl *reg = plat->reg;
  221. return _lpuart32_serial_init(reg);
  222. }
  223. #endif /* CONFIG_LPUART_32B_REG */
  224. static int lpuart_serial_ofdata_to_platdata(struct udevice *dev)
  225. {
  226. struct lpuart_serial_platdata *plat = dev->platdata;
  227. fdt_addr_t addr;
  228. addr = dev_get_addr(dev);
  229. if (addr == FDT_ADDR_T_NONE)
  230. return -EINVAL;
  231. plat->reg = (struct lpuart_fsl *)addr;
  232. return 0;
  233. }
  234. #ifndef CONFIG_LPUART_32B_REG
  235. static const struct dm_serial_ops lpuart_serial_ops = {
  236. .putc = lpuart_serial_putc,
  237. .pending = lpuart_serial_pending,
  238. .getc = lpuart_serial_getc,
  239. .setbrg = lpuart_serial_setbrg,
  240. };
  241. static const struct udevice_id lpuart_serial_ids[] = {
  242. { .compatible = "fsl,vf610-lpuart" },
  243. { }
  244. };
  245. U_BOOT_DRIVER(serial_lpuart) = {
  246. .name = "serial_lpuart",
  247. .id = UCLASS_SERIAL,
  248. .of_match = lpuart_serial_ids,
  249. .ofdata_to_platdata = lpuart_serial_ofdata_to_platdata,
  250. .platdata_auto_alloc_size = sizeof(struct lpuart_serial_platdata),
  251. .probe = lpuart_serial_probe,
  252. .ops = &lpuart_serial_ops,
  253. .flags = DM_FLAG_PRE_RELOC,
  254. };
  255. #else /* CONFIG_LPUART_32B_REG */
  256. static const struct dm_serial_ops lpuart32_serial_ops = {
  257. .putc = lpuart32_serial_putc,
  258. .pending = lpuart32_serial_pending,
  259. .getc = lpuart32_serial_getc,
  260. .setbrg = lpuart32_serial_setbrg,
  261. };
  262. static const struct udevice_id lpuart32_serial_ids[] = {
  263. { .compatible = "fsl,ls1021a-lpuart" },
  264. { }
  265. };
  266. U_BOOT_DRIVER(serial_lpuart32) = {
  267. .name = "serial_lpuart32",
  268. .id = UCLASS_SERIAL,
  269. .of_match = lpuart32_serial_ids,
  270. .ofdata_to_platdata = lpuart_serial_ofdata_to_platdata,
  271. .platdata_auto_alloc_size = sizeof(struct lpuart_serial_platdata),
  272. .probe = lpuart32_serial_probe,
  273. .ops = &lpuart32_serial_ops,
  274. .flags = DM_FLAG_PRE_RELOC,
  275. };
  276. #endif /* CONFIG_LPUART_32B_REG */