serial_lpuart.c 11 KB

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