serial_lpuart.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. static void _lpuart32_serial_setbrg(struct lpuart_fsl *base, int baudrate)
  136. {
  137. u32 clk = CONFIG_SYS_CLK_FREQ;
  138. u32 sbr;
  139. sbr = (clk / (16 * baudrate));
  140. /* place adjustment later - n/32 BRFA */
  141. out_be32(&base->baud, sbr);
  142. }
  143. static int _lpuart32_serial_getc(struct lpuart_fsl *base)
  144. {
  145. u32 stat;
  146. while (((stat = in_be32(&base->stat)) & STAT_RDRF) == 0) {
  147. out_be32(&base->stat, STAT_FLAGS);
  148. WATCHDOG_RESET();
  149. }
  150. return in_be32(&base->data) & 0x3ff;
  151. }
  152. static void _lpuart32_serial_putc(struct lpuart_fsl *base, const char c)
  153. {
  154. while (!(in_be32(&base->stat) & STAT_TDRE))
  155. WATCHDOG_RESET();
  156. out_be32(&base->data, c);
  157. }
  158. /* Test whether a character is in the RX buffer */
  159. static int _lpuart32_serial_tstc(struct lpuart_fsl *base)
  160. {
  161. if ((in_be32(&base->water) >> 24) == 0)
  162. return 0;
  163. return 1;
  164. }
  165. /*
  166. * Initialise the serial port with the given baudrate. The settings
  167. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  168. */
  169. static int _lpuart32_serial_init(struct lpuart_fsl *base)
  170. {
  171. u8 ctrl;
  172. ctrl = in_be32(&base->ctrl);
  173. ctrl &= ~CTRL_RE;
  174. ctrl &= ~CTRL_TE;
  175. out_be32(&base->ctrl, ctrl);
  176. out_be32(&base->modir, 0);
  177. out_be32(&base->fifo, ~(FIFO_TXFE | FIFO_RXFE));
  178. out_be32(&base->match, 0);
  179. /* provide data bits, parity, stop bit, etc */
  180. _lpuart32_serial_setbrg(base, gd->baudrate);
  181. out_be32(&base->ctrl, CTRL_RE | CTRL_TE);
  182. return 0;
  183. }
  184. static int lpuart32_serial_setbrg(struct udevice *dev, int baudrate)
  185. {
  186. struct lpuart_serial_platdata *plat = dev->platdata;
  187. struct lpuart_fsl *reg = plat->reg;
  188. _lpuart32_serial_setbrg(reg, baudrate);
  189. return 0;
  190. }
  191. static int lpuart32_serial_getc(struct udevice *dev)
  192. {
  193. struct lpuart_serial_platdata *plat = dev->platdata;
  194. struct lpuart_fsl *reg = plat->reg;
  195. return _lpuart32_serial_getc(reg);
  196. }
  197. static int lpuart32_serial_putc(struct udevice *dev, const char c)
  198. {
  199. struct lpuart_serial_platdata *plat = dev->platdata;
  200. struct lpuart_fsl *reg = plat->reg;
  201. _lpuart32_serial_putc(reg, c);
  202. return 0;
  203. }
  204. static int lpuart32_serial_pending(struct udevice *dev, bool input)
  205. {
  206. struct lpuart_serial_platdata *plat = dev->platdata;
  207. struct lpuart_fsl *reg = plat->reg;
  208. if (input)
  209. return _lpuart32_serial_tstc(reg);
  210. else
  211. return in_be32(&reg->stat) & STAT_TDRE ? 0 : 1;
  212. }
  213. static int lpuart32_serial_probe(struct udevice *dev)
  214. {
  215. struct lpuart_serial_platdata *plat = dev->platdata;
  216. struct lpuart_fsl *reg = plat->reg;
  217. return _lpuart32_serial_init(reg);
  218. }
  219. #endif /* CONFIG_LPUART_32B_REG */
  220. static int lpuart_serial_ofdata_to_platdata(struct udevice *dev)
  221. {
  222. struct lpuart_serial_platdata *plat = dev->platdata;
  223. fdt_addr_t addr;
  224. addr = dev_get_addr(dev);
  225. if (addr == FDT_ADDR_T_NONE)
  226. return -EINVAL;
  227. plat->reg = (struct lpuart_fsl *)addr;
  228. return 0;
  229. }
  230. #ifndef CONFIG_LPUART_32B_REG
  231. static const struct dm_serial_ops lpuart_serial_ops = {
  232. .putc = lpuart_serial_putc,
  233. .pending = lpuart_serial_pending,
  234. .getc = lpuart_serial_getc,
  235. .setbrg = lpuart_serial_setbrg,
  236. };
  237. static const struct udevice_id lpuart_serial_ids[] = {
  238. { .compatible = "fsl,vf610-lpuart" },
  239. { }
  240. };
  241. U_BOOT_DRIVER(serial_lpuart) = {
  242. .name = "serial_lpuart",
  243. .id = UCLASS_SERIAL,
  244. .of_match = lpuart_serial_ids,
  245. .ofdata_to_platdata = lpuart_serial_ofdata_to_platdata,
  246. .platdata_auto_alloc_size = sizeof(struct lpuart_serial_platdata),
  247. .probe = lpuart_serial_probe,
  248. .ops = &lpuart_serial_ops,
  249. .flags = DM_FLAG_PRE_RELOC,
  250. };
  251. #else /* CONFIG_LPUART_32B_REG */
  252. static const struct dm_serial_ops lpuart32_serial_ops = {
  253. .putc = lpuart32_serial_putc,
  254. .pending = lpuart32_serial_pending,
  255. .getc = lpuart32_serial_getc,
  256. .setbrg = lpuart32_serial_setbrg,
  257. };
  258. static const struct udevice_id lpuart32_serial_ids[] = {
  259. { .compatible = "fsl,ls1021a-lpuart" },
  260. { }
  261. };
  262. U_BOOT_DRIVER(serial_lpuart32) = {
  263. .name = "serial_lpuart32",
  264. .id = UCLASS_SERIAL,
  265. .of_match = lpuart32_serial_ids,
  266. .ofdata_to_platdata = lpuart_serial_ofdata_to_platdata,
  267. .platdata_auto_alloc_size = sizeof(struct lpuart_serial_platdata),
  268. .probe = lpuart32_serial_probe,
  269. .ops = &lpuart32_serial_ops,
  270. .flags = DM_FLAG_PRE_RELOC,
  271. };
  272. #endif /* CONFIG_LPUART_32B_REG */