serial_lpuart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2013 Freescale Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <fsl_lpuart.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. #define LPUART_FLAG_REGMAP_32BIT_REG BIT(0)
  44. #define LPUART_FLAG_REGMAP_ENDIAN_BIG BIT(1)
  45. enum lpuart_devtype {
  46. DEV_VF610 = 1,
  47. DEV_LS1021A,
  48. DEV_MX7ULP
  49. };
  50. struct lpuart_serial_platdata {
  51. void *reg;
  52. enum lpuart_devtype devtype;
  53. ulong flags;
  54. };
  55. static void lpuart_read32(u32 flags, u32 *addr, u32 *val)
  56. {
  57. if (flags & LPUART_FLAG_REGMAP_32BIT_REG) {
  58. if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG)
  59. *(u32 *)val = in_be32(addr);
  60. else
  61. *(u32 *)val = in_le32(addr);
  62. }
  63. }
  64. static void lpuart_write32(u32 flags, u32 *addr, u32 val)
  65. {
  66. if (flags & LPUART_FLAG_REGMAP_32BIT_REG) {
  67. if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG)
  68. out_be32(addr, val);
  69. else
  70. out_le32(addr, val);
  71. }
  72. }
  73. #ifndef CONFIG_SYS_CLK_FREQ
  74. #define CONFIG_SYS_CLK_FREQ 0
  75. #endif
  76. u32 __weak get_lpuart_clk(void)
  77. {
  78. return CONFIG_SYS_CLK_FREQ;
  79. }
  80. static bool is_lpuart32(struct udevice *dev)
  81. {
  82. struct lpuart_serial_platdata *plat = dev->platdata;
  83. return plat->flags & LPUART_FLAG_REGMAP_32BIT_REG;
  84. }
  85. static void _lpuart_serial_setbrg(struct lpuart_serial_platdata *plat,
  86. int baudrate)
  87. {
  88. struct lpuart_fsl *base = plat->reg;
  89. u32 clk = get_lpuart_clk();
  90. u16 sbr;
  91. sbr = (u16)(clk / (16 * baudrate));
  92. /* place adjustment later - n/32 BRFA */
  93. __raw_writeb(sbr >> 8, &base->ubdh);
  94. __raw_writeb(sbr & 0xff, &base->ubdl);
  95. }
  96. static int _lpuart_serial_getc(struct lpuart_serial_platdata *plat)
  97. {
  98. struct lpuart_fsl *base = plat->reg;
  99. while (!(__raw_readb(&base->us1) & (US1_RDRF | US1_OR)))
  100. WATCHDOG_RESET();
  101. barrier();
  102. return __raw_readb(&base->ud);
  103. }
  104. static void _lpuart_serial_putc(struct lpuart_serial_platdata *plat,
  105. const char c)
  106. {
  107. struct lpuart_fsl *base = plat->reg;
  108. while (!(__raw_readb(&base->us1) & US1_TDRE))
  109. WATCHDOG_RESET();
  110. __raw_writeb(c, &base->ud);
  111. }
  112. /* Test whether a character is in the RX buffer */
  113. static int _lpuart_serial_tstc(struct lpuart_serial_platdata *plat)
  114. {
  115. struct lpuart_fsl *base = plat->reg;
  116. if (__raw_readb(&base->urcfifo) == 0)
  117. return 0;
  118. return 1;
  119. }
  120. /*
  121. * Initialise the serial port with the given baudrate. The settings
  122. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  123. */
  124. static int _lpuart_serial_init(struct lpuart_serial_platdata *plat)
  125. {
  126. struct lpuart_fsl *base = (struct lpuart_fsl *)plat->reg;
  127. u8 ctrl;
  128. ctrl = __raw_readb(&base->uc2);
  129. ctrl &= ~UC2_RE;
  130. ctrl &= ~UC2_TE;
  131. __raw_writeb(ctrl, &base->uc2);
  132. __raw_writeb(0, &base->umodem);
  133. __raw_writeb(0, &base->uc1);
  134. /* Disable FIFO and flush buffer */
  135. __raw_writeb(0x0, &base->upfifo);
  136. __raw_writeb(0x0, &base->utwfifo);
  137. __raw_writeb(0x1, &base->urwfifo);
  138. __raw_writeb(CFIFO_TXFLUSH | CFIFO_RXFLUSH, &base->ucfifo);
  139. /* provide data bits, parity, stop bit, etc */
  140. _lpuart_serial_setbrg(plat, gd->baudrate);
  141. __raw_writeb(UC2_RE | UC2_TE, &base->uc2);
  142. return 0;
  143. }
  144. static void _lpuart32_serial_setbrg_7ulp(struct lpuart_serial_platdata *plat,
  145. int baudrate)
  146. {
  147. struct lpuart_fsl_reg32 *base = plat->reg;
  148. u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
  149. u32 clk = get_lpuart_clk();
  150. baud_diff = baudrate;
  151. osr = 0;
  152. sbr = 0;
  153. for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
  154. tmp_sbr = (clk / (baudrate * tmp_osr));
  155. if (tmp_sbr == 0)
  156. tmp_sbr = 1;
  157. /*calculate difference in actual buad w/ current values */
  158. tmp_diff = (clk / (tmp_osr * tmp_sbr));
  159. tmp_diff = tmp_diff - baudrate;
  160. /* select best values between sbr and sbr+1 */
  161. if (tmp_diff > (baudrate - (clk / (tmp_osr * (tmp_sbr + 1))))) {
  162. tmp_diff = baudrate - (clk / (tmp_osr * (tmp_sbr + 1)));
  163. tmp_sbr++;
  164. }
  165. if (tmp_diff <= baud_diff) {
  166. baud_diff = tmp_diff;
  167. osr = tmp_osr;
  168. sbr = tmp_sbr;
  169. }
  170. }
  171. /*
  172. * TODO: handle buadrate outside acceptable rate
  173. * if (baudDiff > ((config->baudRate_Bps / 100) * 3))
  174. * {
  175. * Unacceptable baud rate difference of more than 3%
  176. * return kStatus_LPUART_BaudrateNotSupport;
  177. * }
  178. */
  179. tmp = in_le32(&base->baud);
  180. if ((osr > 3) && (osr < 8))
  181. tmp |= LPUART_BAUD_BOTHEDGE_MASK;
  182. tmp &= ~LPUART_BAUD_OSR_MASK;
  183. tmp |= LPUART_BAUD_OSR(osr-1);
  184. tmp &= ~LPUART_BAUD_SBR_MASK;
  185. tmp |= LPUART_BAUD_SBR(sbr);
  186. /* explicitly disable 10 bit mode & set 1 stop bit */
  187. tmp &= ~(LPUART_BAUD_M10_MASK | LPUART_BAUD_SBNS_MASK);
  188. out_le32(&base->baud, tmp);
  189. }
  190. static void _lpuart32_serial_setbrg(struct lpuart_serial_platdata *plat,
  191. int baudrate)
  192. {
  193. struct lpuart_fsl_reg32 *base = plat->reg;
  194. u32 clk = get_lpuart_clk();
  195. u32 sbr;
  196. sbr = (clk / (16 * baudrate));
  197. /* place adjustment later - n/32 BRFA */
  198. lpuart_write32(plat->flags, &base->baud, sbr);
  199. }
  200. static int _lpuart32_serial_getc(struct lpuart_serial_platdata *plat)
  201. {
  202. struct lpuart_fsl_reg32 *base = plat->reg;
  203. u32 stat, val;
  204. lpuart_read32(plat->flags, &base->stat, &stat);
  205. while ((stat & STAT_RDRF) == 0) {
  206. lpuart_write32(plat->flags, &base->stat, STAT_FLAGS);
  207. WATCHDOG_RESET();
  208. lpuart_read32(plat->flags, &base->stat, &stat);
  209. }
  210. lpuart_read32(plat->flags, &base->data, &val);
  211. lpuart_read32(plat->flags, &base->stat, &stat);
  212. if (stat & STAT_OR)
  213. lpuart_write32(plat->flags, &base->stat, STAT_OR);
  214. return val & 0x3ff;
  215. }
  216. static void _lpuart32_serial_putc(struct lpuart_serial_platdata *plat,
  217. const char c)
  218. {
  219. struct lpuart_fsl_reg32 *base = plat->reg;
  220. u32 stat;
  221. if (c == '\n')
  222. serial_putc('\r');
  223. while (true) {
  224. lpuart_read32(plat->flags, &base->stat, &stat);
  225. if ((stat & STAT_TDRE))
  226. break;
  227. WATCHDOG_RESET();
  228. }
  229. lpuart_write32(plat->flags, &base->data, c);
  230. }
  231. /* Test whether a character is in the RX buffer */
  232. static int _lpuart32_serial_tstc(struct lpuart_serial_platdata *plat)
  233. {
  234. struct lpuart_fsl_reg32 *base = plat->reg;
  235. u32 water;
  236. lpuart_read32(plat->flags, &base->water, &water);
  237. if ((water >> 24) == 0)
  238. return 0;
  239. return 1;
  240. }
  241. /*
  242. * Initialise the serial port with the given baudrate. The settings
  243. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  244. */
  245. static int _lpuart32_serial_init(struct lpuart_serial_platdata *plat)
  246. {
  247. struct lpuart_fsl_reg32 *base = (struct lpuart_fsl_reg32 *)plat->reg;
  248. u32 ctrl;
  249. lpuart_read32(plat->flags, &base->ctrl, &ctrl);
  250. ctrl &= ~CTRL_RE;
  251. ctrl &= ~CTRL_TE;
  252. lpuart_write32(plat->flags, &base->ctrl, ctrl);
  253. lpuart_write32(plat->flags, &base->modir, 0);
  254. lpuart_write32(plat->flags, &base->fifo, ~(FIFO_TXFE | FIFO_RXFE));
  255. lpuart_write32(plat->flags, &base->match, 0);
  256. if (plat->devtype == DEV_MX7ULP) {
  257. _lpuart32_serial_setbrg_7ulp(plat, gd->baudrate);
  258. } else {
  259. /* provide data bits, parity, stop bit, etc */
  260. _lpuart32_serial_setbrg(plat, gd->baudrate);
  261. }
  262. lpuart_write32(plat->flags, &base->ctrl, CTRL_RE | CTRL_TE);
  263. return 0;
  264. }
  265. static int lpuart_serial_setbrg(struct udevice *dev, int baudrate)
  266. {
  267. struct lpuart_serial_platdata *plat = dev->platdata;
  268. if (is_lpuart32(dev)) {
  269. if (plat->devtype == DEV_MX7ULP)
  270. _lpuart32_serial_setbrg_7ulp(plat, baudrate);
  271. else
  272. _lpuart32_serial_setbrg(plat, baudrate);
  273. } else {
  274. _lpuart_serial_setbrg(plat, baudrate);
  275. }
  276. return 0;
  277. }
  278. static int lpuart_serial_getc(struct udevice *dev)
  279. {
  280. struct lpuart_serial_platdata *plat = dev->platdata;
  281. if (is_lpuart32(dev))
  282. return _lpuart32_serial_getc(plat);
  283. return _lpuart_serial_getc(plat);
  284. }
  285. static int lpuart_serial_putc(struct udevice *dev, const char c)
  286. {
  287. struct lpuart_serial_platdata *plat = dev->platdata;
  288. if (is_lpuart32(dev))
  289. _lpuart32_serial_putc(plat, c);
  290. else
  291. _lpuart_serial_putc(plat, c);
  292. return 0;
  293. }
  294. static int lpuart_serial_pending(struct udevice *dev, bool input)
  295. {
  296. struct lpuart_serial_platdata *plat = dev->platdata;
  297. struct lpuart_fsl *reg = plat->reg;
  298. struct lpuart_fsl_reg32 *reg32 = plat->reg;
  299. u32 stat;
  300. if (is_lpuart32(dev)) {
  301. if (input) {
  302. return _lpuart32_serial_tstc(plat);
  303. } else {
  304. lpuart_read32(plat->flags, &reg32->stat, &stat);
  305. return stat & STAT_TDRE ? 0 : 1;
  306. }
  307. }
  308. if (input)
  309. return _lpuart_serial_tstc(plat);
  310. else
  311. return __raw_readb(&reg->us1) & US1_TDRE ? 0 : 1;
  312. }
  313. static int lpuart_serial_probe(struct udevice *dev)
  314. {
  315. struct lpuart_serial_platdata *plat = dev->platdata;
  316. if (is_lpuart32(dev))
  317. return _lpuart32_serial_init(plat);
  318. else
  319. return _lpuart_serial_init(plat);
  320. }
  321. static int lpuart_serial_ofdata_to_platdata(struct udevice *dev)
  322. {
  323. struct lpuart_serial_platdata *plat = dev->platdata;
  324. const void *blob = gd->fdt_blob;
  325. int node = dev_of_offset(dev);
  326. fdt_addr_t addr;
  327. addr = devfdt_get_addr(dev);
  328. if (addr == FDT_ADDR_T_NONE)
  329. return -EINVAL;
  330. plat->reg = (void *)addr;
  331. plat->flags = dev_get_driver_data(dev);
  332. if (!fdt_node_check_compatible(blob, node, "fsl,ls1021a-lpuart"))
  333. plat->devtype = DEV_LS1021A;
  334. else if (!fdt_node_check_compatible(blob, node, "fsl,imx7ulp-lpuart"))
  335. plat->devtype = DEV_MX7ULP;
  336. else if (!fdt_node_check_compatible(blob, node, "fsl,vf610-lpuart"))
  337. plat->devtype = DEV_VF610;
  338. return 0;
  339. }
  340. static const struct dm_serial_ops lpuart_serial_ops = {
  341. .putc = lpuart_serial_putc,
  342. .pending = lpuart_serial_pending,
  343. .getc = lpuart_serial_getc,
  344. .setbrg = lpuart_serial_setbrg,
  345. };
  346. static const struct udevice_id lpuart_serial_ids[] = {
  347. { .compatible = "fsl,ls1021a-lpuart", .data =
  348. LPUART_FLAG_REGMAP_32BIT_REG | LPUART_FLAG_REGMAP_ENDIAN_BIG },
  349. { .compatible = "fsl,imx7ulp-lpuart",
  350. .data = LPUART_FLAG_REGMAP_32BIT_REG },
  351. { .compatible = "fsl,vf610-lpuart"},
  352. { }
  353. };
  354. U_BOOT_DRIVER(serial_lpuart) = {
  355. .name = "serial_lpuart",
  356. .id = UCLASS_SERIAL,
  357. .of_match = lpuart_serial_ids,
  358. .ofdata_to_platdata = lpuart_serial_ofdata_to_platdata,
  359. .platdata_auto_alloc_size = sizeof(struct lpuart_serial_platdata),
  360. .probe = lpuart_serial_probe,
  361. .ops = &lpuart_serial_ops,
  362. .flags = DM_FLAG_PRE_RELOC,
  363. };