lpc32xx_hs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * High Speed Serial Ports on NXP LPC32xx SoC
  4. *
  5. * Authors: Kevin Wells <kevin.wells@nxp.com>
  6. * Roland Stigge <stigge@antcom.de>
  7. *
  8. * Copyright (C) 2010 NXP Semiconductors
  9. * Copyright (C) 2012 Roland Stigge
  10. */
  11. #include <linux/module.h>
  12. #include <linux/ioport.h>
  13. #include <linux/init.h>
  14. #include <linux/console.h>
  15. #include <linux/sysrq.h>
  16. #include <linux/tty.h>
  17. #include <linux/tty_flip.h>
  18. #include <linux/serial_core.h>
  19. #include <linux/serial.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/delay.h>
  22. #include <linux/nmi.h>
  23. #include <linux/io.h>
  24. #include <linux/irq.h>
  25. #include <linux/gpio.h>
  26. #include <linux/of.h>
  27. #include <linux/sizes.h>
  28. #include <linux/soc/nxp/lpc32xx-misc.h>
  29. /*
  30. * High Speed UART register offsets
  31. */
  32. #define LPC32XX_HSUART_FIFO(x) ((x) + 0x00)
  33. #define LPC32XX_HSUART_LEVEL(x) ((x) + 0x04)
  34. #define LPC32XX_HSUART_IIR(x) ((x) + 0x08)
  35. #define LPC32XX_HSUART_CTRL(x) ((x) + 0x0C)
  36. #define LPC32XX_HSUART_RATE(x) ((x) + 0x10)
  37. #define LPC32XX_HSU_BREAK_DATA (1 << 10)
  38. #define LPC32XX_HSU_ERROR_DATA (1 << 9)
  39. #define LPC32XX_HSU_RX_EMPTY (1 << 8)
  40. #define LPC32XX_HSU_TX_LEV(n) (((n) >> 8) & 0xFF)
  41. #define LPC32XX_HSU_RX_LEV(n) ((n) & 0xFF)
  42. #define LPC32XX_HSU_TX_INT_SET (1 << 6)
  43. #define LPC32XX_HSU_RX_OE_INT (1 << 5)
  44. #define LPC32XX_HSU_BRK_INT (1 << 4)
  45. #define LPC32XX_HSU_FE_INT (1 << 3)
  46. #define LPC32XX_HSU_RX_TIMEOUT_INT (1 << 2)
  47. #define LPC32XX_HSU_RX_TRIG_INT (1 << 1)
  48. #define LPC32XX_HSU_TX_INT (1 << 0)
  49. #define LPC32XX_HSU_HRTS_INV (1 << 21)
  50. #define LPC32XX_HSU_HRTS_TRIG_8B (0x0 << 19)
  51. #define LPC32XX_HSU_HRTS_TRIG_16B (0x1 << 19)
  52. #define LPC32XX_HSU_HRTS_TRIG_32B (0x2 << 19)
  53. #define LPC32XX_HSU_HRTS_TRIG_48B (0x3 << 19)
  54. #define LPC32XX_HSU_HRTS_EN (1 << 18)
  55. #define LPC32XX_HSU_TMO_DISABLED (0x0 << 16)
  56. #define LPC32XX_HSU_TMO_INACT_4B (0x1 << 16)
  57. #define LPC32XX_HSU_TMO_INACT_8B (0x2 << 16)
  58. #define LPC32XX_HSU_TMO_INACT_16B (0x3 << 16)
  59. #define LPC32XX_HSU_HCTS_INV (1 << 15)
  60. #define LPC32XX_HSU_HCTS_EN (1 << 14)
  61. #define LPC32XX_HSU_OFFSET(n) ((n) << 9)
  62. #define LPC32XX_HSU_BREAK (1 << 8)
  63. #define LPC32XX_HSU_ERR_INT_EN (1 << 7)
  64. #define LPC32XX_HSU_RX_INT_EN (1 << 6)
  65. #define LPC32XX_HSU_TX_INT_EN (1 << 5)
  66. #define LPC32XX_HSU_RX_TL1B (0x0 << 2)
  67. #define LPC32XX_HSU_RX_TL4B (0x1 << 2)
  68. #define LPC32XX_HSU_RX_TL8B (0x2 << 2)
  69. #define LPC32XX_HSU_RX_TL16B (0x3 << 2)
  70. #define LPC32XX_HSU_RX_TL32B (0x4 << 2)
  71. #define LPC32XX_HSU_RX_TL48B (0x5 << 2)
  72. #define LPC32XX_HSU_TX_TLEMPTY (0x0 << 0)
  73. #define LPC32XX_HSU_TX_TL0B (0x0 << 0)
  74. #define LPC32XX_HSU_TX_TL4B (0x1 << 0)
  75. #define LPC32XX_HSU_TX_TL8B (0x2 << 0)
  76. #define LPC32XX_HSU_TX_TL16B (0x3 << 0)
  77. #define LPC32XX_MAIN_OSC_FREQ 13000000
  78. #define MODNAME "lpc32xx_hsuart"
  79. struct lpc32xx_hsuart_port {
  80. struct uart_port port;
  81. };
  82. #define FIFO_READ_LIMIT 128
  83. #define MAX_PORTS 3
  84. #define LPC32XX_TTY_NAME "ttyTX"
  85. static struct lpc32xx_hsuart_port lpc32xx_hs_ports[MAX_PORTS];
  86. #ifdef CONFIG_SERIAL_HS_LPC32XX_CONSOLE
  87. static void wait_for_xmit_empty(struct uart_port *port)
  88. {
  89. unsigned int timeout = 10000;
  90. do {
  91. if (LPC32XX_HSU_TX_LEV(readl(LPC32XX_HSUART_LEVEL(
  92. port->membase))) == 0)
  93. break;
  94. if (--timeout == 0)
  95. break;
  96. udelay(1);
  97. } while (1);
  98. }
  99. static void wait_for_xmit_ready(struct uart_port *port)
  100. {
  101. unsigned int timeout = 10000;
  102. while (1) {
  103. if (LPC32XX_HSU_TX_LEV(readl(LPC32XX_HSUART_LEVEL(
  104. port->membase))) < 32)
  105. break;
  106. if (--timeout == 0)
  107. break;
  108. udelay(1);
  109. }
  110. }
  111. static void lpc32xx_hsuart_console_putchar(struct uart_port *port, int ch)
  112. {
  113. wait_for_xmit_ready(port);
  114. writel((u32)ch, LPC32XX_HSUART_FIFO(port->membase));
  115. }
  116. static void lpc32xx_hsuart_console_write(struct console *co, const char *s,
  117. unsigned int count)
  118. {
  119. struct lpc32xx_hsuart_port *up = &lpc32xx_hs_ports[co->index];
  120. unsigned long flags;
  121. int locked = 1;
  122. touch_nmi_watchdog();
  123. local_irq_save(flags);
  124. if (up->port.sysrq)
  125. locked = 0;
  126. else if (oops_in_progress)
  127. locked = spin_trylock(&up->port.lock);
  128. else
  129. spin_lock(&up->port.lock);
  130. uart_console_write(&up->port, s, count, lpc32xx_hsuart_console_putchar);
  131. wait_for_xmit_empty(&up->port);
  132. if (locked)
  133. spin_unlock(&up->port.lock);
  134. local_irq_restore(flags);
  135. }
  136. static int __init lpc32xx_hsuart_console_setup(struct console *co,
  137. char *options)
  138. {
  139. struct uart_port *port;
  140. int baud = 115200;
  141. int bits = 8;
  142. int parity = 'n';
  143. int flow = 'n';
  144. if (co->index >= MAX_PORTS)
  145. co->index = 0;
  146. port = &lpc32xx_hs_ports[co->index].port;
  147. if (!port->membase)
  148. return -ENODEV;
  149. if (options)
  150. uart_parse_options(options, &baud, &parity, &bits, &flow);
  151. lpc32xx_loopback_set(port->mapbase, 0); /* get out of loopback mode */
  152. return uart_set_options(port, co, baud, parity, bits, flow);
  153. }
  154. static struct uart_driver lpc32xx_hsuart_reg;
  155. static struct console lpc32xx_hsuart_console = {
  156. .name = LPC32XX_TTY_NAME,
  157. .write = lpc32xx_hsuart_console_write,
  158. .device = uart_console_device,
  159. .setup = lpc32xx_hsuart_console_setup,
  160. .flags = CON_PRINTBUFFER,
  161. .index = -1,
  162. .data = &lpc32xx_hsuart_reg,
  163. };
  164. static int __init lpc32xx_hsuart_console_init(void)
  165. {
  166. register_console(&lpc32xx_hsuart_console);
  167. return 0;
  168. }
  169. console_initcall(lpc32xx_hsuart_console_init);
  170. #define LPC32XX_HSUART_CONSOLE (&lpc32xx_hsuart_console)
  171. #else
  172. #define LPC32XX_HSUART_CONSOLE NULL
  173. #endif
  174. static struct uart_driver lpc32xx_hs_reg = {
  175. .owner = THIS_MODULE,
  176. .driver_name = MODNAME,
  177. .dev_name = LPC32XX_TTY_NAME,
  178. .nr = MAX_PORTS,
  179. .cons = LPC32XX_HSUART_CONSOLE,
  180. };
  181. static int uarts_registered;
  182. static unsigned int __serial_get_clock_div(unsigned long uartclk,
  183. unsigned long rate)
  184. {
  185. u32 div, goodrate, hsu_rate, l_hsu_rate, comprate;
  186. u32 rate_diff;
  187. /* Find the closest divider to get the desired clock rate */
  188. div = uartclk / rate;
  189. goodrate = hsu_rate = (div / 14) - 1;
  190. if (hsu_rate != 0)
  191. hsu_rate--;
  192. /* Tweak divider */
  193. l_hsu_rate = hsu_rate + 3;
  194. rate_diff = 0xFFFFFFFF;
  195. while (hsu_rate < l_hsu_rate) {
  196. comprate = uartclk / ((hsu_rate + 1) * 14);
  197. if (abs(comprate - rate) < rate_diff) {
  198. goodrate = hsu_rate;
  199. rate_diff = abs(comprate - rate);
  200. }
  201. hsu_rate++;
  202. }
  203. if (hsu_rate > 0xFF)
  204. hsu_rate = 0xFF;
  205. return goodrate;
  206. }
  207. static void __serial_uart_flush(struct uart_port *port)
  208. {
  209. u32 tmp;
  210. int cnt = 0;
  211. while ((readl(LPC32XX_HSUART_LEVEL(port->membase)) > 0) &&
  212. (cnt++ < FIFO_READ_LIMIT))
  213. tmp = readl(LPC32XX_HSUART_FIFO(port->membase));
  214. }
  215. static void __serial_lpc32xx_rx(struct uart_port *port)
  216. {
  217. struct tty_port *tport = &port->state->port;
  218. unsigned int tmp, flag;
  219. /* Read data from FIFO and push into terminal */
  220. tmp = readl(LPC32XX_HSUART_FIFO(port->membase));
  221. while (!(tmp & LPC32XX_HSU_RX_EMPTY)) {
  222. flag = TTY_NORMAL;
  223. port->icount.rx++;
  224. if (tmp & LPC32XX_HSU_ERROR_DATA) {
  225. /* Framing error */
  226. writel(LPC32XX_HSU_FE_INT,
  227. LPC32XX_HSUART_IIR(port->membase));
  228. port->icount.frame++;
  229. flag = TTY_FRAME;
  230. tty_insert_flip_char(tport, 0, TTY_FRAME);
  231. }
  232. tty_insert_flip_char(tport, (tmp & 0xFF), flag);
  233. tmp = readl(LPC32XX_HSUART_FIFO(port->membase));
  234. }
  235. spin_unlock(&port->lock);
  236. tty_flip_buffer_push(tport);
  237. spin_lock(&port->lock);
  238. }
  239. static void __serial_lpc32xx_tx(struct uart_port *port)
  240. {
  241. struct circ_buf *xmit = &port->state->xmit;
  242. unsigned int tmp;
  243. if (port->x_char) {
  244. writel((u32)port->x_char, LPC32XX_HSUART_FIFO(port->membase));
  245. port->icount.tx++;
  246. port->x_char = 0;
  247. return;
  248. }
  249. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  250. goto exit_tx;
  251. /* Transfer data */
  252. while (LPC32XX_HSU_TX_LEV(readl(
  253. LPC32XX_HSUART_LEVEL(port->membase))) < 64) {
  254. writel((u32) xmit->buf[xmit->tail],
  255. LPC32XX_HSUART_FIFO(port->membase));
  256. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  257. port->icount.tx++;
  258. if (uart_circ_empty(xmit))
  259. break;
  260. }
  261. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  262. uart_write_wakeup(port);
  263. exit_tx:
  264. if (uart_circ_empty(xmit)) {
  265. tmp = readl(LPC32XX_HSUART_CTRL(port->membase));
  266. tmp &= ~LPC32XX_HSU_TX_INT_EN;
  267. writel(tmp, LPC32XX_HSUART_CTRL(port->membase));
  268. }
  269. }
  270. static irqreturn_t serial_lpc32xx_interrupt(int irq, void *dev_id)
  271. {
  272. struct uart_port *port = dev_id;
  273. struct tty_port *tport = &port->state->port;
  274. u32 status;
  275. spin_lock(&port->lock);
  276. /* Read UART status and clear latched interrupts */
  277. status = readl(LPC32XX_HSUART_IIR(port->membase));
  278. if (status & LPC32XX_HSU_BRK_INT) {
  279. /* Break received */
  280. writel(LPC32XX_HSU_BRK_INT, LPC32XX_HSUART_IIR(port->membase));
  281. port->icount.brk++;
  282. uart_handle_break(port);
  283. }
  284. /* Framing error */
  285. if (status & LPC32XX_HSU_FE_INT)
  286. writel(LPC32XX_HSU_FE_INT, LPC32XX_HSUART_IIR(port->membase));
  287. if (status & LPC32XX_HSU_RX_OE_INT) {
  288. /* Receive FIFO overrun */
  289. writel(LPC32XX_HSU_RX_OE_INT,
  290. LPC32XX_HSUART_IIR(port->membase));
  291. port->icount.overrun++;
  292. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  293. tty_schedule_flip(tport);
  294. }
  295. /* Data received? */
  296. if (status & (LPC32XX_HSU_RX_TIMEOUT_INT | LPC32XX_HSU_RX_TRIG_INT))
  297. __serial_lpc32xx_rx(port);
  298. /* Transmit data request? */
  299. if ((status & LPC32XX_HSU_TX_INT) && (!uart_tx_stopped(port))) {
  300. writel(LPC32XX_HSU_TX_INT, LPC32XX_HSUART_IIR(port->membase));
  301. __serial_lpc32xx_tx(port);
  302. }
  303. spin_unlock(&port->lock);
  304. return IRQ_HANDLED;
  305. }
  306. /* port->lock is not held. */
  307. static unsigned int serial_lpc32xx_tx_empty(struct uart_port *port)
  308. {
  309. unsigned int ret = 0;
  310. if (LPC32XX_HSU_TX_LEV(readl(LPC32XX_HSUART_LEVEL(port->membase))) == 0)
  311. ret = TIOCSER_TEMT;
  312. return ret;
  313. }
  314. /* port->lock held by caller. */
  315. static void serial_lpc32xx_set_mctrl(struct uart_port *port,
  316. unsigned int mctrl)
  317. {
  318. /* No signals are supported on HS UARTs */
  319. }
  320. /* port->lock is held by caller and interrupts are disabled. */
  321. static unsigned int serial_lpc32xx_get_mctrl(struct uart_port *port)
  322. {
  323. /* No signals are supported on HS UARTs */
  324. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  325. }
  326. /* port->lock held by caller. */
  327. static void serial_lpc32xx_stop_tx(struct uart_port *port)
  328. {
  329. u32 tmp;
  330. tmp = readl(LPC32XX_HSUART_CTRL(port->membase));
  331. tmp &= ~LPC32XX_HSU_TX_INT_EN;
  332. writel(tmp, LPC32XX_HSUART_CTRL(port->membase));
  333. }
  334. /* port->lock held by caller. */
  335. static void serial_lpc32xx_start_tx(struct uart_port *port)
  336. {
  337. u32 tmp;
  338. __serial_lpc32xx_tx(port);
  339. tmp = readl(LPC32XX_HSUART_CTRL(port->membase));
  340. tmp |= LPC32XX_HSU_TX_INT_EN;
  341. writel(tmp, LPC32XX_HSUART_CTRL(port->membase));
  342. }
  343. /* port->lock held by caller. */
  344. static void serial_lpc32xx_stop_rx(struct uart_port *port)
  345. {
  346. u32 tmp;
  347. tmp = readl(LPC32XX_HSUART_CTRL(port->membase));
  348. tmp &= ~(LPC32XX_HSU_RX_INT_EN | LPC32XX_HSU_ERR_INT_EN);
  349. writel(tmp, LPC32XX_HSUART_CTRL(port->membase));
  350. writel((LPC32XX_HSU_BRK_INT | LPC32XX_HSU_RX_OE_INT |
  351. LPC32XX_HSU_FE_INT), LPC32XX_HSUART_IIR(port->membase));
  352. }
  353. /* port->lock is not held. */
  354. static void serial_lpc32xx_break_ctl(struct uart_port *port,
  355. int break_state)
  356. {
  357. unsigned long flags;
  358. u32 tmp;
  359. spin_lock_irqsave(&port->lock, flags);
  360. tmp = readl(LPC32XX_HSUART_CTRL(port->membase));
  361. if (break_state != 0)
  362. tmp |= LPC32XX_HSU_BREAK;
  363. else
  364. tmp &= ~LPC32XX_HSU_BREAK;
  365. writel(tmp, LPC32XX_HSUART_CTRL(port->membase));
  366. spin_unlock_irqrestore(&port->lock, flags);
  367. }
  368. /* port->lock is not held. */
  369. static int serial_lpc32xx_startup(struct uart_port *port)
  370. {
  371. int retval;
  372. unsigned long flags;
  373. u32 tmp;
  374. spin_lock_irqsave(&port->lock, flags);
  375. __serial_uart_flush(port);
  376. writel((LPC32XX_HSU_TX_INT | LPC32XX_HSU_FE_INT |
  377. LPC32XX_HSU_BRK_INT | LPC32XX_HSU_RX_OE_INT),
  378. LPC32XX_HSUART_IIR(port->membase));
  379. writel(0xFF, LPC32XX_HSUART_RATE(port->membase));
  380. /*
  381. * Set receiver timeout, HSU offset of 20, no break, no interrupts,
  382. * and default FIFO trigger levels
  383. */
  384. tmp = LPC32XX_HSU_TX_TL8B | LPC32XX_HSU_RX_TL32B |
  385. LPC32XX_HSU_OFFSET(20) | LPC32XX_HSU_TMO_INACT_4B;
  386. writel(tmp, LPC32XX_HSUART_CTRL(port->membase));
  387. lpc32xx_loopback_set(port->mapbase, 0); /* get out of loopback mode */
  388. spin_unlock_irqrestore(&port->lock, flags);
  389. retval = request_irq(port->irq, serial_lpc32xx_interrupt,
  390. 0, MODNAME, port);
  391. if (!retval)
  392. writel((tmp | LPC32XX_HSU_RX_INT_EN | LPC32XX_HSU_ERR_INT_EN),
  393. LPC32XX_HSUART_CTRL(port->membase));
  394. return retval;
  395. }
  396. /* port->lock is not held. */
  397. static void serial_lpc32xx_shutdown(struct uart_port *port)
  398. {
  399. u32 tmp;
  400. unsigned long flags;
  401. spin_lock_irqsave(&port->lock, flags);
  402. tmp = LPC32XX_HSU_TX_TL8B | LPC32XX_HSU_RX_TL32B |
  403. LPC32XX_HSU_OFFSET(20) | LPC32XX_HSU_TMO_INACT_4B;
  404. writel(tmp, LPC32XX_HSUART_CTRL(port->membase));
  405. lpc32xx_loopback_set(port->mapbase, 1); /* go to loopback mode */
  406. spin_unlock_irqrestore(&port->lock, flags);
  407. free_irq(port->irq, port);
  408. }
  409. /* port->lock is not held. */
  410. static void serial_lpc32xx_set_termios(struct uart_port *port,
  411. struct ktermios *termios,
  412. struct ktermios *old)
  413. {
  414. unsigned long flags;
  415. unsigned int baud, quot;
  416. u32 tmp;
  417. /* Always 8-bit, no parity, 1 stop bit */
  418. termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
  419. termios->c_cflag |= CS8;
  420. termios->c_cflag &= ~(HUPCL | CMSPAR | CLOCAL | CRTSCTS);
  421. baud = uart_get_baud_rate(port, termios, old, 0,
  422. port->uartclk / 14);
  423. quot = __serial_get_clock_div(port->uartclk, baud);
  424. spin_lock_irqsave(&port->lock, flags);
  425. /* Ignore characters? */
  426. tmp = readl(LPC32XX_HSUART_CTRL(port->membase));
  427. if ((termios->c_cflag & CREAD) == 0)
  428. tmp &= ~(LPC32XX_HSU_RX_INT_EN | LPC32XX_HSU_ERR_INT_EN);
  429. else
  430. tmp |= LPC32XX_HSU_RX_INT_EN | LPC32XX_HSU_ERR_INT_EN;
  431. writel(tmp, LPC32XX_HSUART_CTRL(port->membase));
  432. writel(quot, LPC32XX_HSUART_RATE(port->membase));
  433. uart_update_timeout(port, termios->c_cflag, baud);
  434. spin_unlock_irqrestore(&port->lock, flags);
  435. /* Don't rewrite B0 */
  436. if (tty_termios_baud_rate(termios))
  437. tty_termios_encode_baud_rate(termios, baud, baud);
  438. }
  439. static const char *serial_lpc32xx_type(struct uart_port *port)
  440. {
  441. return MODNAME;
  442. }
  443. static void serial_lpc32xx_release_port(struct uart_port *port)
  444. {
  445. if ((port->iotype == UPIO_MEM32) && (port->mapbase)) {
  446. if (port->flags & UPF_IOREMAP) {
  447. iounmap(port->membase);
  448. port->membase = NULL;
  449. }
  450. release_mem_region(port->mapbase, SZ_4K);
  451. }
  452. }
  453. static int serial_lpc32xx_request_port(struct uart_port *port)
  454. {
  455. int ret = -ENODEV;
  456. if ((port->iotype == UPIO_MEM32) && (port->mapbase)) {
  457. ret = 0;
  458. if (!request_mem_region(port->mapbase, SZ_4K, MODNAME))
  459. ret = -EBUSY;
  460. else if (port->flags & UPF_IOREMAP) {
  461. port->membase = ioremap(port->mapbase, SZ_4K);
  462. if (!port->membase) {
  463. release_mem_region(port->mapbase, SZ_4K);
  464. ret = -ENOMEM;
  465. }
  466. }
  467. }
  468. return ret;
  469. }
  470. static void serial_lpc32xx_config_port(struct uart_port *port, int uflags)
  471. {
  472. int ret;
  473. ret = serial_lpc32xx_request_port(port);
  474. if (ret < 0)
  475. return;
  476. port->type = PORT_UART00;
  477. port->fifosize = 64;
  478. __serial_uart_flush(port);
  479. writel((LPC32XX_HSU_TX_INT | LPC32XX_HSU_FE_INT |
  480. LPC32XX_HSU_BRK_INT | LPC32XX_HSU_RX_OE_INT),
  481. LPC32XX_HSUART_IIR(port->membase));
  482. writel(0xFF, LPC32XX_HSUART_RATE(port->membase));
  483. /* Set receiver timeout, HSU offset of 20, no break, no interrupts,
  484. and default FIFO trigger levels */
  485. writel(LPC32XX_HSU_TX_TL8B | LPC32XX_HSU_RX_TL32B |
  486. LPC32XX_HSU_OFFSET(20) | LPC32XX_HSU_TMO_INACT_4B,
  487. LPC32XX_HSUART_CTRL(port->membase));
  488. }
  489. static int serial_lpc32xx_verify_port(struct uart_port *port,
  490. struct serial_struct *ser)
  491. {
  492. int ret = 0;
  493. if (ser->type != PORT_UART00)
  494. ret = -EINVAL;
  495. return ret;
  496. }
  497. static const struct uart_ops serial_lpc32xx_pops = {
  498. .tx_empty = serial_lpc32xx_tx_empty,
  499. .set_mctrl = serial_lpc32xx_set_mctrl,
  500. .get_mctrl = serial_lpc32xx_get_mctrl,
  501. .stop_tx = serial_lpc32xx_stop_tx,
  502. .start_tx = serial_lpc32xx_start_tx,
  503. .stop_rx = serial_lpc32xx_stop_rx,
  504. .break_ctl = serial_lpc32xx_break_ctl,
  505. .startup = serial_lpc32xx_startup,
  506. .shutdown = serial_lpc32xx_shutdown,
  507. .set_termios = serial_lpc32xx_set_termios,
  508. .type = serial_lpc32xx_type,
  509. .release_port = serial_lpc32xx_release_port,
  510. .request_port = serial_lpc32xx_request_port,
  511. .config_port = serial_lpc32xx_config_port,
  512. .verify_port = serial_lpc32xx_verify_port,
  513. };
  514. /*
  515. * Register a set of serial devices attached to a platform device
  516. */
  517. static int serial_hs_lpc32xx_probe(struct platform_device *pdev)
  518. {
  519. struct lpc32xx_hsuart_port *p = &lpc32xx_hs_ports[uarts_registered];
  520. int ret = 0;
  521. struct resource *res;
  522. if (uarts_registered >= MAX_PORTS) {
  523. dev_err(&pdev->dev,
  524. "Error: Number of possible ports exceeded (%d)!\n",
  525. uarts_registered + 1);
  526. return -ENXIO;
  527. }
  528. memset(p, 0, sizeof(*p));
  529. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  530. if (!res) {
  531. dev_err(&pdev->dev,
  532. "Error getting mem resource for HS UART port %d\n",
  533. uarts_registered);
  534. return -ENXIO;
  535. }
  536. p->port.mapbase = res->start;
  537. p->port.membase = NULL;
  538. ret = platform_get_irq(pdev, 0);
  539. if (ret < 0)
  540. return ret;
  541. p->port.irq = ret;
  542. p->port.iotype = UPIO_MEM32;
  543. p->port.uartclk = LPC32XX_MAIN_OSC_FREQ;
  544. p->port.regshift = 2;
  545. p->port.flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT | UPF_IOREMAP;
  546. p->port.dev = &pdev->dev;
  547. p->port.ops = &serial_lpc32xx_pops;
  548. p->port.line = uarts_registered++;
  549. spin_lock_init(&p->port.lock);
  550. /* send port to loopback mode by default */
  551. lpc32xx_loopback_set(p->port.mapbase, 1);
  552. ret = uart_add_one_port(&lpc32xx_hs_reg, &p->port);
  553. platform_set_drvdata(pdev, p);
  554. return ret;
  555. }
  556. /*
  557. * Remove serial ports registered against a platform device.
  558. */
  559. static int serial_hs_lpc32xx_remove(struct platform_device *pdev)
  560. {
  561. struct lpc32xx_hsuart_port *p = platform_get_drvdata(pdev);
  562. uart_remove_one_port(&lpc32xx_hs_reg, &p->port);
  563. return 0;
  564. }
  565. #ifdef CONFIG_PM
  566. static int serial_hs_lpc32xx_suspend(struct platform_device *pdev,
  567. pm_message_t state)
  568. {
  569. struct lpc32xx_hsuart_port *p = platform_get_drvdata(pdev);
  570. uart_suspend_port(&lpc32xx_hs_reg, &p->port);
  571. return 0;
  572. }
  573. static int serial_hs_lpc32xx_resume(struct platform_device *pdev)
  574. {
  575. struct lpc32xx_hsuart_port *p = platform_get_drvdata(pdev);
  576. uart_resume_port(&lpc32xx_hs_reg, &p->port);
  577. return 0;
  578. }
  579. #else
  580. #define serial_hs_lpc32xx_suspend NULL
  581. #define serial_hs_lpc32xx_resume NULL
  582. #endif
  583. static const struct of_device_id serial_hs_lpc32xx_dt_ids[] = {
  584. { .compatible = "nxp,lpc3220-hsuart" },
  585. { /* sentinel */ }
  586. };
  587. MODULE_DEVICE_TABLE(of, serial_hs_lpc32xx_dt_ids);
  588. static struct platform_driver serial_hs_lpc32xx_driver = {
  589. .probe = serial_hs_lpc32xx_probe,
  590. .remove = serial_hs_lpc32xx_remove,
  591. .suspend = serial_hs_lpc32xx_suspend,
  592. .resume = serial_hs_lpc32xx_resume,
  593. .driver = {
  594. .name = MODNAME,
  595. .of_match_table = serial_hs_lpc32xx_dt_ids,
  596. },
  597. };
  598. static int __init lpc32xx_hsuart_init(void)
  599. {
  600. int ret;
  601. ret = uart_register_driver(&lpc32xx_hs_reg);
  602. if (ret)
  603. return ret;
  604. ret = platform_driver_register(&serial_hs_lpc32xx_driver);
  605. if (ret)
  606. uart_unregister_driver(&lpc32xx_hs_reg);
  607. return ret;
  608. }
  609. static void __exit lpc32xx_hsuart_exit(void)
  610. {
  611. platform_driver_unregister(&serial_hs_lpc32xx_driver);
  612. uart_unregister_driver(&lpc32xx_hs_reg);
  613. }
  614. module_init(lpc32xx_hsuart_init);
  615. module_exit(lpc32xx_hsuart_exit);
  616. MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
  617. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  618. MODULE_DESCRIPTION("NXP LPC32XX High Speed UART driver");
  619. MODULE_LICENSE("GPL");