ns16550.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * COM1 NS16550 support
  3. * originally from linux source (arch/powerpc/boot/ns16550.c)
  4. * modified to use CONFIG_SYS_ISA_MEM and new defines
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <fdtdec.h>
  10. #include <ns16550.h>
  11. #include <serial.h>
  12. #include <watchdog.h>
  13. #include <linux/types.h>
  14. #include <asm/io.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. #define UART_LCRVAL UART_LCR_8N1 /* 8 data, 1 stop, no parity */
  17. #define UART_MCRVAL (UART_MCR_DTR | \
  18. UART_MCR_RTS) /* RTS/DTR */
  19. #define UART_FCRVAL (UART_FCR_FIFO_EN | \
  20. UART_FCR_RXSR | \
  21. UART_FCR_TXSR) /* Clear & enable FIFOs */
  22. #ifndef CONFIG_DM_SERIAL
  23. #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
  24. #define serial_out(x, y) outb(x, (ulong)y)
  25. #define serial_in(y) inb((ulong)y)
  26. #elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE > 0)
  27. #define serial_out(x, y) out_be32(y, x)
  28. #define serial_in(y) in_be32(y)
  29. #elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE < 0)
  30. #define serial_out(x, y) out_le32(y, x)
  31. #define serial_in(y) in_le32(y)
  32. #else
  33. #define serial_out(x, y) writeb(x, y)
  34. #define serial_in(y) readb(y)
  35. #endif
  36. #endif /* !CONFIG_DM_SERIAL */
  37. #if defined(CONFIG_SOC_KEYSTONE)
  38. #define UART_REG_VAL_PWREMU_MGMT_UART_DISABLE 0
  39. #define UART_REG_VAL_PWREMU_MGMT_UART_ENABLE ((1 << 14) | (1 << 13) | (1 << 0))
  40. #undef UART_MCRVAL
  41. #ifdef CONFIG_SERIAL_HW_FLOW_CONTROL
  42. #define UART_MCRVAL (UART_MCR_RTS | UART_MCR_AFE)
  43. #else
  44. #define UART_MCRVAL (UART_MCR_RTS)
  45. #endif
  46. #endif
  47. #ifndef CONFIG_SYS_NS16550_IER
  48. #define CONFIG_SYS_NS16550_IER 0x00
  49. #endif /* CONFIG_SYS_NS16550_IER */
  50. static inline void serial_out_shift(void *addr, int shift, int value)
  51. {
  52. #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
  53. outb(value, (ulong)addr);
  54. #elif defined(CONFIG_SYS_NS16550_MEM32) && !defined(CONFIG_SYS_BIG_ENDIAN)
  55. out_le32(addr, value);
  56. #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
  57. out_be32(addr, value);
  58. #elif defined(CONFIG_SYS_NS16550_MEM32)
  59. writel(value, addr);
  60. #elif defined(CONFIG_SYS_BIG_ENDIAN)
  61. writeb(value, addr + (1 << shift) - 1);
  62. #else
  63. writeb(value, addr);
  64. #endif
  65. }
  66. static inline int serial_in_shift(void *addr, int shift)
  67. {
  68. #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
  69. return inb((ulong)addr);
  70. #elif defined(CONFIG_SYS_NS16550_MEM32) && !defined(CONFIG_SYS_BIG_ENDIAN)
  71. return in_le32(addr);
  72. #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
  73. return in_be32(addr);
  74. #elif defined(CONFIG_SYS_NS16550_MEM32)
  75. return readl(addr);
  76. #elif defined(CONFIG_SYS_BIG_ENDIAN)
  77. return readb(addr + (1 << shift) - 1);
  78. #else
  79. return readb(addr);
  80. #endif
  81. }
  82. #ifdef CONFIG_DM_SERIAL
  83. #ifndef CONFIG_SYS_NS16550_CLK
  84. #define CONFIG_SYS_NS16550_CLK 0
  85. #endif
  86. static void ns16550_writeb(NS16550_t port, int offset, int value)
  87. {
  88. struct ns16550_platdata *plat = port->plat;
  89. unsigned char *addr;
  90. offset *= 1 << plat->reg_shift;
  91. addr = (unsigned char *)plat->base + offset;
  92. /*
  93. * As far as we know it doesn't make sense to support selection of
  94. * these options at run-time, so use the existing CONFIG options.
  95. */
  96. serial_out_shift(addr + plat->reg_offset, plat->reg_shift, value);
  97. }
  98. static int ns16550_readb(NS16550_t port, int offset)
  99. {
  100. struct ns16550_platdata *plat = port->plat;
  101. unsigned char *addr;
  102. offset *= 1 << plat->reg_shift;
  103. addr = (unsigned char *)plat->base + offset;
  104. return serial_in_shift(addr + plat->reg_offset, plat->reg_shift);
  105. }
  106. /* We can clean these up once everything is moved to driver model */
  107. #define serial_out(value, addr) \
  108. ns16550_writeb(com_port, \
  109. (unsigned char *)addr - (unsigned char *)com_port, value)
  110. #define serial_in(addr) \
  111. ns16550_readb(com_port, \
  112. (unsigned char *)addr - (unsigned char *)com_port)
  113. #endif
  114. int ns16550_calc_divisor(NS16550_t port, int clock, int baudrate)
  115. {
  116. const unsigned int mode_x_div = 16;
  117. return DIV_ROUND_CLOSEST(clock, mode_x_div * baudrate);
  118. }
  119. static void NS16550_setbrg(NS16550_t com_port, int baud_divisor)
  120. {
  121. serial_out(UART_LCR_BKSE | UART_LCRVAL, &com_port->lcr);
  122. serial_out(baud_divisor & 0xff, &com_port->dll);
  123. serial_out((baud_divisor >> 8) & 0xff, &com_port->dlm);
  124. serial_out(UART_LCRVAL, &com_port->lcr);
  125. }
  126. void NS16550_init(NS16550_t com_port, int baud_divisor)
  127. {
  128. #if (defined(CONFIG_SPL_BUILD) && \
  129. (defined(CONFIG_OMAP34XX) || defined(CONFIG_OMAP44XX)))
  130. /*
  131. * On some OMAP3/OMAP4 devices when UART3 is configured for boot mode
  132. * before SPL starts only THRE bit is set. We have to empty the
  133. * transmitter before initialization starts.
  134. */
  135. if ((serial_in(&com_port->lsr) & (UART_LSR_TEMT | UART_LSR_THRE))
  136. == UART_LSR_THRE) {
  137. if (baud_divisor != -1)
  138. NS16550_setbrg(com_port, baud_divisor);
  139. serial_out(0, &com_port->mdr1);
  140. }
  141. #endif
  142. while (!(serial_in(&com_port->lsr) & UART_LSR_TEMT))
  143. ;
  144. serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
  145. #if defined(CONFIG_OMAP) || defined(CONFIG_AM33XX) || \
  146. defined(CONFIG_TI81XX) || defined(CONFIG_AM43XX)
  147. serial_out(0x7, &com_port->mdr1); /* mode select reset TL16C750*/
  148. #endif
  149. serial_out(UART_MCRVAL, &com_port->mcr);
  150. serial_out(UART_FCRVAL, &com_port->fcr);
  151. if (baud_divisor != -1)
  152. NS16550_setbrg(com_port, baud_divisor);
  153. #if defined(CONFIG_OMAP) || \
  154. defined(CONFIG_AM33XX) || defined(CONFIG_SOC_DA8XX) || \
  155. defined(CONFIG_TI81XX) || defined(CONFIG_AM43XX)
  156. /* /16 is proper to hit 115200 with 48MHz */
  157. serial_out(0, &com_port->mdr1);
  158. #endif /* CONFIG_OMAP */
  159. #if defined(CONFIG_SOC_KEYSTONE)
  160. serial_out(UART_REG_VAL_PWREMU_MGMT_UART_ENABLE, &com_port->regC);
  161. #endif
  162. }
  163. #ifndef CONFIG_NS16550_MIN_FUNCTIONS
  164. void NS16550_reinit(NS16550_t com_port, int baud_divisor)
  165. {
  166. serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
  167. NS16550_setbrg(com_port, 0);
  168. serial_out(UART_MCRVAL, &com_port->mcr);
  169. serial_out(UART_FCRVAL, &com_port->fcr);
  170. NS16550_setbrg(com_port, baud_divisor);
  171. }
  172. #endif /* CONFIG_NS16550_MIN_FUNCTIONS */
  173. void NS16550_putc(NS16550_t com_port, char c)
  174. {
  175. while ((serial_in(&com_port->lsr) & UART_LSR_THRE) == 0)
  176. ;
  177. serial_out(c, &com_port->thr);
  178. /*
  179. * Call watchdog_reset() upon newline. This is done here in putc
  180. * since the environment code uses a single puts() to print the complete
  181. * environment upon "printenv". So we can't put this watchdog call
  182. * in puts().
  183. */
  184. if (c == '\n')
  185. WATCHDOG_RESET();
  186. }
  187. #ifndef CONFIG_NS16550_MIN_FUNCTIONS
  188. char NS16550_getc(NS16550_t com_port)
  189. {
  190. while ((serial_in(&com_port->lsr) & UART_LSR_DR) == 0) {
  191. #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_TTY)
  192. extern void usbtty_poll(void);
  193. usbtty_poll();
  194. #endif
  195. WATCHDOG_RESET();
  196. }
  197. return serial_in(&com_port->rbr);
  198. }
  199. int NS16550_tstc(NS16550_t com_port)
  200. {
  201. return (serial_in(&com_port->lsr) & UART_LSR_DR) != 0;
  202. }
  203. #endif /* CONFIG_NS16550_MIN_FUNCTIONS */
  204. #ifdef CONFIG_DEBUG_UART_NS16550
  205. #include <debug_uart.h>
  206. #define serial_dout(reg, value) \
  207. serial_out_shift((char *)com_port + \
  208. ((char *)reg - (char *)com_port) * \
  209. (1 << CONFIG_DEBUG_UART_SHIFT), \
  210. CONFIG_DEBUG_UART_SHIFT, value)
  211. #define serial_din(reg) \
  212. serial_in_shift((char *)com_port + \
  213. ((char *)reg - (char *)com_port) * \
  214. (1 << CONFIG_DEBUG_UART_SHIFT), \
  215. CONFIG_DEBUG_UART_SHIFT)
  216. static inline void _debug_uart_init(void)
  217. {
  218. struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
  219. int baud_divisor;
  220. /*
  221. * We copy the code from above because it is already horribly messy.
  222. * Trying to refactor to nicely remove the duplication doesn't seem
  223. * feasible. The better fix is to move all users of this driver to
  224. * driver model.
  225. */
  226. baud_divisor = ns16550_calc_divisor(com_port, CONFIG_DEBUG_UART_CLOCK,
  227. CONFIG_BAUDRATE);
  228. serial_dout(&com_port->ier, CONFIG_SYS_NS16550_IER);
  229. serial_dout(&com_port->mcr, UART_MCRVAL);
  230. serial_dout(&com_port->fcr, UART_FCRVAL);
  231. serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
  232. serial_dout(&com_port->dll, baud_divisor & 0xff);
  233. serial_dout(&com_port->dlm, (baud_divisor >> 8) & 0xff);
  234. serial_dout(&com_port->lcr, UART_LCRVAL);
  235. }
  236. static inline void _debug_uart_putc(int ch)
  237. {
  238. struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
  239. while (!(serial_din(&com_port->lsr) & UART_LSR_THRE))
  240. ;
  241. serial_dout(&com_port->thr, ch);
  242. }
  243. DEBUG_UART_FUNCS
  244. #endif
  245. #ifdef CONFIG_DM_SERIAL
  246. static int ns16550_serial_putc(struct udevice *dev, const char ch)
  247. {
  248. struct NS16550 *const com_port = dev_get_priv(dev);
  249. if (!(serial_in(&com_port->lsr) & UART_LSR_THRE))
  250. return -EAGAIN;
  251. serial_out(ch, &com_port->thr);
  252. /*
  253. * Call watchdog_reset() upon newline. This is done here in putc
  254. * since the environment code uses a single puts() to print the complete
  255. * environment upon "printenv". So we can't put this watchdog call
  256. * in puts().
  257. */
  258. if (ch == '\n')
  259. WATCHDOG_RESET();
  260. return 0;
  261. }
  262. static int ns16550_serial_pending(struct udevice *dev, bool input)
  263. {
  264. struct NS16550 *const com_port = dev_get_priv(dev);
  265. if (input)
  266. return serial_in(&com_port->lsr) & UART_LSR_DR ? 1 : 0;
  267. else
  268. return serial_in(&com_port->lsr) & UART_LSR_THRE ? 0 : 1;
  269. }
  270. static int ns16550_serial_getc(struct udevice *dev)
  271. {
  272. struct NS16550 *const com_port = dev_get_priv(dev);
  273. if (!(serial_in(&com_port->lsr) & UART_LSR_DR))
  274. return -EAGAIN;
  275. return serial_in(&com_port->rbr);
  276. }
  277. static int ns16550_serial_setbrg(struct udevice *dev, int baudrate)
  278. {
  279. struct NS16550 *const com_port = dev_get_priv(dev);
  280. struct ns16550_platdata *plat = com_port->plat;
  281. int clock_divisor;
  282. clock_divisor = ns16550_calc_divisor(com_port, plat->clock, baudrate);
  283. NS16550_setbrg(com_port, clock_divisor);
  284. return 0;
  285. }
  286. int ns16550_serial_probe(struct udevice *dev)
  287. {
  288. struct NS16550 *const com_port = dev_get_priv(dev);
  289. com_port->plat = dev_get_platdata(dev);
  290. NS16550_init(com_port, -1);
  291. return 0;
  292. }
  293. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  294. int ns16550_serial_ofdata_to_platdata(struct udevice *dev)
  295. {
  296. struct ns16550_platdata *plat = dev->platdata;
  297. fdt_addr_t addr;
  298. /* try Processor Local Bus device first */
  299. addr = dev_get_addr(dev);
  300. #if defined(CONFIG_PCI) && defined(CONFIG_DM_PCI)
  301. if (addr == FDT_ADDR_T_NONE) {
  302. /* then try pci device */
  303. struct fdt_pci_addr pci_addr;
  304. u32 bar;
  305. int ret;
  306. /* we prefer to use a memory-mapped register */
  307. ret = fdtdec_get_pci_addr(gd->fdt_blob, dev->of_offset,
  308. FDT_PCI_SPACE_MEM32, "reg",
  309. &pci_addr);
  310. if (ret) {
  311. /* try if there is any i/o-mapped register */
  312. ret = fdtdec_get_pci_addr(gd->fdt_blob,
  313. dev->of_offset,
  314. FDT_PCI_SPACE_IO,
  315. "reg", &pci_addr);
  316. if (ret)
  317. return ret;
  318. }
  319. ret = fdtdec_get_pci_bar32(dev, &pci_addr, &bar);
  320. if (ret)
  321. return ret;
  322. addr = bar;
  323. }
  324. #endif
  325. if (addr == FDT_ADDR_T_NONE)
  326. return -EINVAL;
  327. #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
  328. plat->base = addr;
  329. #else
  330. plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
  331. #endif
  332. plat->reg_offset = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  333. "reg-offset", 0);
  334. plat->reg_shift = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  335. "reg-shift", 0);
  336. plat->clock = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  337. "clock-frequency",
  338. CONFIG_SYS_NS16550_CLK);
  339. if (!plat->clock) {
  340. debug("ns16550 clock not defined\n");
  341. return -EINVAL;
  342. }
  343. return 0;
  344. }
  345. #endif
  346. const struct dm_serial_ops ns16550_serial_ops = {
  347. .putc = ns16550_serial_putc,
  348. .pending = ns16550_serial_pending,
  349. .getc = ns16550_serial_getc,
  350. .setbrg = ns16550_serial_setbrg,
  351. };
  352. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  353. #if CONFIG_IS_ENABLED(OF_CONTROL)
  354. /*
  355. * Please consider existing compatible strings before adding a new
  356. * one to keep this table compact. Or you may add a generic "ns16550"
  357. * compatible string to your dts.
  358. */
  359. static const struct udevice_id ns16550_serial_ids[] = {
  360. { .compatible = "ns16550" },
  361. { .compatible = "ns16550a" },
  362. { .compatible = "nvidia,tegra20-uart" },
  363. { .compatible = "snps,dw-apb-uart" },
  364. { .compatible = "ti,omap2-uart" },
  365. { .compatible = "ti,omap3-uart" },
  366. { .compatible = "ti,omap4-uart" },
  367. { .compatible = "ti,am3352-uart" },
  368. { .compatible = "ti,am4372-uart" },
  369. { .compatible = "ti,dra742-uart" },
  370. {}
  371. };
  372. #endif
  373. #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
  374. U_BOOT_DRIVER(ns16550_serial) = {
  375. .name = "ns16550_serial",
  376. .id = UCLASS_SERIAL,
  377. #if CONFIG_IS_ENABLED(OF_CONTROL)
  378. .of_match = ns16550_serial_ids,
  379. .ofdata_to_platdata = ns16550_serial_ofdata_to_platdata,
  380. .platdata_auto_alloc_size = sizeof(struct ns16550_platdata),
  381. #endif
  382. .priv_auto_alloc_size = sizeof(struct NS16550),
  383. .probe = ns16550_serial_probe,
  384. .ops = &ns16550_serial_ops,
  385. .flags = DM_FLAG_PRE_RELOC,
  386. };
  387. #endif
  388. #endif /* !OF_PLATDATA */
  389. #endif /* CONFIG_DM_SERIAL */