lpc32xx_hs.c 20 KB

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