lantiq.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*
  2. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published
  6. * by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. *
  17. * Copyright (C) 2004 Infineon IFAP DC COM CPE
  18. * Copyright (C) 2007 Felix Fietkau <nbd@openwrt.org>
  19. * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
  20. * Copyright (C) 2010 Thomas Langer, <thomas.langer@lantiq.com>
  21. */
  22. #include <linux/slab.h>
  23. #include <linux/module.h>
  24. #include <linux/ioport.h>
  25. #include <linux/init.h>
  26. #include <linux/console.h>
  27. #include <linux/sysrq.h>
  28. #include <linux/device.h>
  29. #include <linux/tty.h>
  30. #include <linux/tty_flip.h>
  31. #include <linux/serial_core.h>
  32. #include <linux/serial.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/io.h>
  35. #include <linux/clk.h>
  36. #include <lantiq_soc.h>
  37. #define PORT_LTQ_ASC 111
  38. #define MAXPORTS 2
  39. #define UART_DUMMY_UER_RX 1
  40. #define DRVNAME "ltq_asc"
  41. #ifdef __BIG_ENDIAN
  42. #define LTQ_ASC_TBUF (0x0020 + 3)
  43. #define LTQ_ASC_RBUF (0x0024 + 3)
  44. #else
  45. #define LTQ_ASC_TBUF 0x0020
  46. #define LTQ_ASC_RBUF 0x0024
  47. #endif
  48. #define LTQ_ASC_FSTAT 0x0048
  49. #define LTQ_ASC_WHBSTATE 0x0018
  50. #define LTQ_ASC_STATE 0x0014
  51. #define LTQ_ASC_IRNCR 0x00F8
  52. #define LTQ_ASC_CLC 0x0000
  53. #define LTQ_ASC_ID 0x0008
  54. #define LTQ_ASC_PISEL 0x0004
  55. #define LTQ_ASC_TXFCON 0x0044
  56. #define LTQ_ASC_RXFCON 0x0040
  57. #define LTQ_ASC_CON 0x0010
  58. #define LTQ_ASC_BG 0x0050
  59. #define LTQ_ASC_IRNREN 0x00F4
  60. #define ASC_IRNREN_TX 0x1
  61. #define ASC_IRNREN_RX 0x2
  62. #define ASC_IRNREN_ERR 0x4
  63. #define ASC_IRNREN_TX_BUF 0x8
  64. #define ASC_IRNCR_TIR 0x1
  65. #define ASC_IRNCR_RIR 0x2
  66. #define ASC_IRNCR_EIR 0x4
  67. #define ASCOPT_CSIZE 0x3
  68. #define TXFIFO_FL 1
  69. #define RXFIFO_FL 1
  70. #define ASCCLC_DISS 0x2
  71. #define ASCCLC_RMCMASK 0x0000FF00
  72. #define ASCCLC_RMCOFFSET 8
  73. #define ASCCON_M_8ASYNC 0x0
  74. #define ASCCON_M_7ASYNC 0x2
  75. #define ASCCON_ODD 0x00000020
  76. #define ASCCON_STP 0x00000080
  77. #define ASCCON_BRS 0x00000100
  78. #define ASCCON_FDE 0x00000200
  79. #define ASCCON_R 0x00008000
  80. #define ASCCON_FEN 0x00020000
  81. #define ASCCON_ROEN 0x00080000
  82. #define ASCCON_TOEN 0x00100000
  83. #define ASCSTATE_PE 0x00010000
  84. #define ASCSTATE_FE 0x00020000
  85. #define ASCSTATE_ROE 0x00080000
  86. #define ASCSTATE_ANY (ASCSTATE_ROE|ASCSTATE_PE|ASCSTATE_FE)
  87. #define ASCWHBSTATE_CLRREN 0x00000001
  88. #define ASCWHBSTATE_SETREN 0x00000002
  89. #define ASCWHBSTATE_CLRPE 0x00000004
  90. #define ASCWHBSTATE_CLRFE 0x00000008
  91. #define ASCWHBSTATE_CLRROE 0x00000020
  92. #define ASCTXFCON_TXFEN 0x0001
  93. #define ASCTXFCON_TXFFLU 0x0002
  94. #define ASCTXFCON_TXFITLMASK 0x3F00
  95. #define ASCTXFCON_TXFITLOFF 8
  96. #define ASCRXFCON_RXFEN 0x0001
  97. #define ASCRXFCON_RXFFLU 0x0002
  98. #define ASCRXFCON_RXFITLMASK 0x3F00
  99. #define ASCRXFCON_RXFITLOFF 8
  100. #define ASCFSTAT_RXFFLMASK 0x003F
  101. #define ASCFSTAT_TXFFLMASK 0x3F00
  102. #define ASCFSTAT_TXFREEMASK 0x3F000000
  103. #define ASCFSTAT_TXFREEOFF 24
  104. static void lqasc_tx_chars(struct uart_port *port);
  105. static struct ltq_uart_port *lqasc_port[MAXPORTS];
  106. static struct uart_driver lqasc_reg;
  107. static DEFINE_SPINLOCK(ltq_asc_lock);
  108. struct ltq_uart_port {
  109. struct uart_port port;
  110. struct clk *clk;
  111. unsigned int tx_irq;
  112. unsigned int rx_irq;
  113. unsigned int err_irq;
  114. };
  115. static inline struct
  116. ltq_uart_port *to_ltq_uart_port(struct uart_port *port)
  117. {
  118. return container_of(port, struct ltq_uart_port, port);
  119. }
  120. static void
  121. lqasc_stop_tx(struct uart_port *port)
  122. {
  123. return;
  124. }
  125. static void
  126. lqasc_start_tx(struct uart_port *port)
  127. {
  128. unsigned long flags;
  129. spin_lock_irqsave(&ltq_asc_lock, flags);
  130. lqasc_tx_chars(port);
  131. spin_unlock_irqrestore(&ltq_asc_lock, flags);
  132. return;
  133. }
  134. static void
  135. lqasc_stop_rx(struct uart_port *port)
  136. {
  137. ltq_w32(ASCWHBSTATE_CLRREN, port->membase + LTQ_ASC_WHBSTATE);
  138. }
  139. static void
  140. lqasc_enable_ms(struct uart_port *port)
  141. {
  142. }
  143. static int
  144. lqasc_rx_chars(struct uart_port *port)
  145. {
  146. struct tty_struct *tty = tty_port_tty_get(&port->state->port);
  147. unsigned int ch = 0, rsr = 0, fifocnt;
  148. if (!tty) {
  149. dev_dbg(port->dev, "%s:tty is busy now", __func__);
  150. return -EBUSY;
  151. }
  152. fifocnt =
  153. ltq_r32(port->membase + LTQ_ASC_FSTAT) & ASCFSTAT_RXFFLMASK;
  154. while (fifocnt--) {
  155. u8 flag = TTY_NORMAL;
  156. ch = ltq_r8(port->membase + LTQ_ASC_RBUF);
  157. rsr = (ltq_r32(port->membase + LTQ_ASC_STATE)
  158. & ASCSTATE_ANY) | UART_DUMMY_UER_RX;
  159. tty_flip_buffer_push(tty);
  160. port->icount.rx++;
  161. /*
  162. * Note that the error handling code is
  163. * out of the main execution path
  164. */
  165. if (rsr & ASCSTATE_ANY) {
  166. if (rsr & ASCSTATE_PE) {
  167. port->icount.parity++;
  168. ltq_w32_mask(0, ASCWHBSTATE_CLRPE,
  169. port->membase + LTQ_ASC_WHBSTATE);
  170. } else if (rsr & ASCSTATE_FE) {
  171. port->icount.frame++;
  172. ltq_w32_mask(0, ASCWHBSTATE_CLRFE,
  173. port->membase + LTQ_ASC_WHBSTATE);
  174. }
  175. if (rsr & ASCSTATE_ROE) {
  176. port->icount.overrun++;
  177. ltq_w32_mask(0, ASCWHBSTATE_CLRROE,
  178. port->membase + LTQ_ASC_WHBSTATE);
  179. }
  180. rsr &= port->read_status_mask;
  181. if (rsr & ASCSTATE_PE)
  182. flag = TTY_PARITY;
  183. else if (rsr & ASCSTATE_FE)
  184. flag = TTY_FRAME;
  185. }
  186. if ((rsr & port->ignore_status_mask) == 0)
  187. tty_insert_flip_char(tty, ch, flag);
  188. if (rsr & ASCSTATE_ROE)
  189. /*
  190. * Overrun is special, since it's reported
  191. * immediately, and doesn't affect the current
  192. * character
  193. */
  194. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  195. }
  196. if (ch != 0)
  197. tty_flip_buffer_push(tty);
  198. tty_kref_put(tty);
  199. return 0;
  200. }
  201. static void
  202. lqasc_tx_chars(struct uart_port *port)
  203. {
  204. struct circ_buf *xmit = &port->state->xmit;
  205. if (uart_tx_stopped(port)) {
  206. lqasc_stop_tx(port);
  207. return;
  208. }
  209. while (((ltq_r32(port->membase + LTQ_ASC_FSTAT) &
  210. ASCFSTAT_TXFREEMASK) >> ASCFSTAT_TXFREEOFF) != 0) {
  211. if (port->x_char) {
  212. ltq_w8(port->x_char, port->membase + LTQ_ASC_TBUF);
  213. port->icount.tx++;
  214. port->x_char = 0;
  215. continue;
  216. }
  217. if (uart_circ_empty(xmit))
  218. break;
  219. ltq_w8(port->state->xmit.buf[port->state->xmit.tail],
  220. port->membase + LTQ_ASC_TBUF);
  221. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  222. port->icount.tx++;
  223. }
  224. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  225. uart_write_wakeup(port);
  226. }
  227. static irqreturn_t
  228. lqasc_tx_int(int irq, void *_port)
  229. {
  230. unsigned long flags;
  231. struct uart_port *port = (struct uart_port *)_port;
  232. spin_lock_irqsave(&ltq_asc_lock, flags);
  233. ltq_w32(ASC_IRNCR_TIR, port->membase + LTQ_ASC_IRNCR);
  234. spin_unlock_irqrestore(&ltq_asc_lock, flags);
  235. lqasc_start_tx(port);
  236. return IRQ_HANDLED;
  237. }
  238. static irqreturn_t
  239. lqasc_err_int(int irq, void *_port)
  240. {
  241. unsigned long flags;
  242. struct uart_port *port = (struct uart_port *)_port;
  243. spin_lock_irqsave(&ltq_asc_lock, flags);
  244. /* clear any pending interrupts */
  245. ltq_w32_mask(0, ASCWHBSTATE_CLRPE | ASCWHBSTATE_CLRFE |
  246. ASCWHBSTATE_CLRROE, port->membase + LTQ_ASC_WHBSTATE);
  247. spin_unlock_irqrestore(&ltq_asc_lock, flags);
  248. return IRQ_HANDLED;
  249. }
  250. static irqreturn_t
  251. lqasc_rx_int(int irq, void *_port)
  252. {
  253. unsigned long flags;
  254. struct uart_port *port = (struct uart_port *)_port;
  255. spin_lock_irqsave(&ltq_asc_lock, flags);
  256. ltq_w32(ASC_IRNCR_RIR, port->membase + LTQ_ASC_IRNCR);
  257. lqasc_rx_chars(port);
  258. spin_unlock_irqrestore(&ltq_asc_lock, flags);
  259. return IRQ_HANDLED;
  260. }
  261. static unsigned int
  262. lqasc_tx_empty(struct uart_port *port)
  263. {
  264. int status;
  265. status = ltq_r32(port->membase + LTQ_ASC_FSTAT) & ASCFSTAT_TXFFLMASK;
  266. return status ? 0 : TIOCSER_TEMT;
  267. }
  268. static unsigned int
  269. lqasc_get_mctrl(struct uart_port *port)
  270. {
  271. return TIOCM_CTS | TIOCM_CAR | TIOCM_DSR;
  272. }
  273. static void
  274. lqasc_set_mctrl(struct uart_port *port, u_int mctrl)
  275. {
  276. }
  277. static void
  278. lqasc_break_ctl(struct uart_port *port, int break_state)
  279. {
  280. }
  281. static int
  282. lqasc_startup(struct uart_port *port)
  283. {
  284. struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
  285. int retval;
  286. port->uartclk = clk_get_rate(ltq_port->clk);
  287. ltq_w32_mask(ASCCLC_DISS | ASCCLC_RMCMASK, (1 << ASCCLC_RMCOFFSET),
  288. port->membase + LTQ_ASC_CLC);
  289. ltq_w32(0, port->membase + LTQ_ASC_PISEL);
  290. ltq_w32(
  291. ((TXFIFO_FL << ASCTXFCON_TXFITLOFF) & ASCTXFCON_TXFITLMASK) |
  292. ASCTXFCON_TXFEN | ASCTXFCON_TXFFLU,
  293. port->membase + LTQ_ASC_TXFCON);
  294. ltq_w32(
  295. ((RXFIFO_FL << ASCRXFCON_RXFITLOFF) & ASCRXFCON_RXFITLMASK)
  296. | ASCRXFCON_RXFEN | ASCRXFCON_RXFFLU,
  297. port->membase + LTQ_ASC_RXFCON);
  298. /* make sure other settings are written to hardware before
  299. * setting enable bits
  300. */
  301. wmb();
  302. ltq_w32_mask(0, ASCCON_M_8ASYNC | ASCCON_FEN | ASCCON_TOEN |
  303. ASCCON_ROEN, port->membase + LTQ_ASC_CON);
  304. retval = request_irq(ltq_port->tx_irq, lqasc_tx_int,
  305. IRQF_DISABLED, "asc_tx", port);
  306. if (retval) {
  307. pr_err("failed to request lqasc_tx_int\n");
  308. return retval;
  309. }
  310. retval = request_irq(ltq_port->rx_irq, lqasc_rx_int,
  311. IRQF_DISABLED, "asc_rx", port);
  312. if (retval) {
  313. pr_err("failed to request lqasc_rx_int\n");
  314. goto err1;
  315. }
  316. retval = request_irq(ltq_port->err_irq, lqasc_err_int,
  317. IRQF_DISABLED, "asc_err", port);
  318. if (retval) {
  319. pr_err("failed to request lqasc_err_int\n");
  320. goto err2;
  321. }
  322. ltq_w32(ASC_IRNREN_RX | ASC_IRNREN_ERR | ASC_IRNREN_TX,
  323. port->membase + LTQ_ASC_IRNREN);
  324. return 0;
  325. err2:
  326. free_irq(ltq_port->rx_irq, port);
  327. err1:
  328. free_irq(ltq_port->tx_irq, port);
  329. return retval;
  330. }
  331. static void
  332. lqasc_shutdown(struct uart_port *port)
  333. {
  334. struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
  335. free_irq(ltq_port->tx_irq, port);
  336. free_irq(ltq_port->rx_irq, port);
  337. free_irq(ltq_port->err_irq, port);
  338. ltq_w32(0, port->membase + LTQ_ASC_CON);
  339. ltq_w32_mask(ASCRXFCON_RXFEN, ASCRXFCON_RXFFLU,
  340. port->membase + LTQ_ASC_RXFCON);
  341. ltq_w32_mask(ASCTXFCON_TXFEN, ASCTXFCON_TXFFLU,
  342. port->membase + LTQ_ASC_TXFCON);
  343. }
  344. static void
  345. lqasc_set_termios(struct uart_port *port,
  346. struct ktermios *new, struct ktermios *old)
  347. {
  348. unsigned int cflag;
  349. unsigned int iflag;
  350. unsigned int divisor;
  351. unsigned int baud;
  352. unsigned int con = 0;
  353. unsigned long flags;
  354. cflag = new->c_cflag;
  355. iflag = new->c_iflag;
  356. switch (cflag & CSIZE) {
  357. case CS7:
  358. con = ASCCON_M_7ASYNC;
  359. break;
  360. case CS5:
  361. case CS6:
  362. default:
  363. new->c_cflag &= ~ CSIZE;
  364. new->c_cflag |= CS8;
  365. con = ASCCON_M_8ASYNC;
  366. break;
  367. }
  368. cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
  369. if (cflag & CSTOPB)
  370. con |= ASCCON_STP;
  371. if (cflag & PARENB) {
  372. if (!(cflag & PARODD))
  373. con &= ~ASCCON_ODD;
  374. else
  375. con |= ASCCON_ODD;
  376. }
  377. port->read_status_mask = ASCSTATE_ROE;
  378. if (iflag & INPCK)
  379. port->read_status_mask |= ASCSTATE_FE | ASCSTATE_PE;
  380. port->ignore_status_mask = 0;
  381. if (iflag & IGNPAR)
  382. port->ignore_status_mask |= ASCSTATE_FE | ASCSTATE_PE;
  383. if (iflag & IGNBRK) {
  384. /*
  385. * If we're ignoring parity and break indicators,
  386. * ignore overruns too (for real raw support).
  387. */
  388. if (iflag & IGNPAR)
  389. port->ignore_status_mask |= ASCSTATE_ROE;
  390. }
  391. if ((cflag & CREAD) == 0)
  392. port->ignore_status_mask |= UART_DUMMY_UER_RX;
  393. /* set error signals - framing, parity and overrun, enable receiver */
  394. con |= ASCCON_FEN | ASCCON_TOEN | ASCCON_ROEN;
  395. spin_lock_irqsave(&ltq_asc_lock, flags);
  396. /* set up CON */
  397. ltq_w32_mask(0, con, port->membase + LTQ_ASC_CON);
  398. /* Set baud rate - take a divider of 2 into account */
  399. baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
  400. divisor = uart_get_divisor(port, baud);
  401. divisor = divisor / 2 - 1;
  402. /* disable the baudrate generator */
  403. ltq_w32_mask(ASCCON_R, 0, port->membase + LTQ_ASC_CON);
  404. /* make sure the fractional divider is off */
  405. ltq_w32_mask(ASCCON_FDE, 0, port->membase + LTQ_ASC_CON);
  406. /* set up to use divisor of 2 */
  407. ltq_w32_mask(ASCCON_BRS, 0, port->membase + LTQ_ASC_CON);
  408. /* now we can write the new baudrate into the register */
  409. ltq_w32(divisor, port->membase + LTQ_ASC_BG);
  410. /* turn the baudrate generator back on */
  411. ltq_w32_mask(0, ASCCON_R, port->membase + LTQ_ASC_CON);
  412. /* enable rx */
  413. ltq_w32(ASCWHBSTATE_SETREN, port->membase + LTQ_ASC_WHBSTATE);
  414. spin_unlock_irqrestore(&ltq_asc_lock, flags);
  415. /* Don't rewrite B0 */
  416. if (tty_termios_baud_rate(new))
  417. tty_termios_encode_baud_rate(new, baud, baud);
  418. }
  419. static const char*
  420. lqasc_type(struct uart_port *port)
  421. {
  422. if (port->type == PORT_LTQ_ASC)
  423. return DRVNAME;
  424. else
  425. return NULL;
  426. }
  427. static void
  428. lqasc_release_port(struct uart_port *port)
  429. {
  430. if (port->flags & UPF_IOREMAP) {
  431. iounmap(port->membase);
  432. port->membase = NULL;
  433. }
  434. }
  435. static int
  436. lqasc_request_port(struct uart_port *port)
  437. {
  438. struct platform_device *pdev = to_platform_device(port->dev);
  439. struct resource *res;
  440. int size;
  441. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  442. if (!res) {
  443. dev_err(&pdev->dev, "cannot obtain I/O memory region");
  444. return -ENODEV;
  445. }
  446. size = resource_size(res);
  447. res = devm_request_mem_region(&pdev->dev, res->start,
  448. size, dev_name(&pdev->dev));
  449. if (!res) {
  450. dev_err(&pdev->dev, "cannot request I/O memory region");
  451. return -EBUSY;
  452. }
  453. if (port->flags & UPF_IOREMAP) {
  454. port->membase = devm_ioremap_nocache(&pdev->dev,
  455. port->mapbase, size);
  456. if (port->membase == NULL)
  457. return -ENOMEM;
  458. }
  459. return 0;
  460. }
  461. static void
  462. lqasc_config_port(struct uart_port *port, int flags)
  463. {
  464. if (flags & UART_CONFIG_TYPE) {
  465. port->type = PORT_LTQ_ASC;
  466. lqasc_request_port(port);
  467. }
  468. }
  469. static int
  470. lqasc_verify_port(struct uart_port *port,
  471. struct serial_struct *ser)
  472. {
  473. int ret = 0;
  474. if (ser->type != PORT_UNKNOWN && ser->type != PORT_LTQ_ASC)
  475. ret = -EINVAL;
  476. if (ser->irq < 0 || ser->irq >= NR_IRQS)
  477. ret = -EINVAL;
  478. if (ser->baud_base < 9600)
  479. ret = -EINVAL;
  480. return ret;
  481. }
  482. static struct uart_ops lqasc_pops = {
  483. .tx_empty = lqasc_tx_empty,
  484. .set_mctrl = lqasc_set_mctrl,
  485. .get_mctrl = lqasc_get_mctrl,
  486. .stop_tx = lqasc_stop_tx,
  487. .start_tx = lqasc_start_tx,
  488. .stop_rx = lqasc_stop_rx,
  489. .enable_ms = lqasc_enable_ms,
  490. .break_ctl = lqasc_break_ctl,
  491. .startup = lqasc_startup,
  492. .shutdown = lqasc_shutdown,
  493. .set_termios = lqasc_set_termios,
  494. .type = lqasc_type,
  495. .release_port = lqasc_release_port,
  496. .request_port = lqasc_request_port,
  497. .config_port = lqasc_config_port,
  498. .verify_port = lqasc_verify_port,
  499. };
  500. static void
  501. lqasc_console_putchar(struct uart_port *port, int ch)
  502. {
  503. int fifofree;
  504. if (!port->membase)
  505. return;
  506. do {
  507. fifofree = (ltq_r32(port->membase + LTQ_ASC_FSTAT)
  508. & ASCFSTAT_TXFREEMASK) >> ASCFSTAT_TXFREEOFF;
  509. } while (fifofree == 0);
  510. ltq_w8(ch, port->membase + LTQ_ASC_TBUF);
  511. }
  512. static void
  513. lqasc_console_write(struct console *co, const char *s, u_int count)
  514. {
  515. struct ltq_uart_port *ltq_port;
  516. struct uart_port *port;
  517. unsigned long flags;
  518. if (co->index >= MAXPORTS)
  519. return;
  520. ltq_port = lqasc_port[co->index];
  521. if (!ltq_port)
  522. return;
  523. port = &ltq_port->port;
  524. spin_lock_irqsave(&ltq_asc_lock, flags);
  525. uart_console_write(port, s, count, lqasc_console_putchar);
  526. spin_unlock_irqrestore(&ltq_asc_lock, flags);
  527. }
  528. static int __init
  529. lqasc_console_setup(struct console *co, char *options)
  530. {
  531. struct ltq_uart_port *ltq_port;
  532. struct uart_port *port;
  533. int baud = 115200;
  534. int bits = 8;
  535. int parity = 'n';
  536. int flow = 'n';
  537. if (co->index >= MAXPORTS)
  538. return -ENODEV;
  539. ltq_port = lqasc_port[co->index];
  540. if (!ltq_port)
  541. return -ENODEV;
  542. port = &ltq_port->port;
  543. port->uartclk = clk_get_rate(ltq_port->clk);
  544. if (options)
  545. uart_parse_options(options, &baud, &parity, &bits, &flow);
  546. return uart_set_options(port, co, baud, parity, bits, flow);
  547. }
  548. static struct console lqasc_console = {
  549. .name = "ttyLTQ",
  550. .write = lqasc_console_write,
  551. .device = uart_console_device,
  552. .setup = lqasc_console_setup,
  553. .flags = CON_PRINTBUFFER,
  554. .index = -1,
  555. .data = &lqasc_reg,
  556. };
  557. static int __init
  558. lqasc_console_init(void)
  559. {
  560. register_console(&lqasc_console);
  561. return 0;
  562. }
  563. console_initcall(lqasc_console_init);
  564. static struct uart_driver lqasc_reg = {
  565. .owner = THIS_MODULE,
  566. .driver_name = DRVNAME,
  567. .dev_name = "ttyLTQ",
  568. .major = 0,
  569. .minor = 0,
  570. .nr = MAXPORTS,
  571. .cons = &lqasc_console,
  572. };
  573. static int __init
  574. lqasc_probe(struct platform_device *pdev)
  575. {
  576. struct ltq_uart_port *ltq_port;
  577. struct uart_port *port;
  578. struct resource *mmres, *irqres;
  579. int tx_irq, rx_irq, err_irq;
  580. struct clk *clk;
  581. int ret;
  582. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  583. irqres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  584. if (!mmres || !irqres)
  585. return -ENODEV;
  586. if (pdev->id >= MAXPORTS)
  587. return -EBUSY;
  588. if (lqasc_port[pdev->id] != NULL)
  589. return -EBUSY;
  590. clk = clk_get(&pdev->dev, "fpi");
  591. if (IS_ERR(clk)) {
  592. pr_err("failed to get fpi clk\n");
  593. return -ENOENT;
  594. }
  595. tx_irq = platform_get_irq_byname(pdev, "tx");
  596. rx_irq = platform_get_irq_byname(pdev, "rx");
  597. err_irq = platform_get_irq_byname(pdev, "err");
  598. if ((tx_irq < 0) | (rx_irq < 0) | (err_irq < 0))
  599. return -ENODEV;
  600. ltq_port = kzalloc(sizeof(struct ltq_uart_port), GFP_KERNEL);
  601. if (!ltq_port)
  602. return -ENOMEM;
  603. port = &ltq_port->port;
  604. port->iotype = SERIAL_IO_MEM;
  605. port->flags = ASYNC_BOOT_AUTOCONF | UPF_IOREMAP;
  606. port->ops = &lqasc_pops;
  607. port->fifosize = 16;
  608. port->type = PORT_LTQ_ASC,
  609. port->line = pdev->id;
  610. port->dev = &pdev->dev;
  611. port->irq = tx_irq; /* unused, just to be backward-compatibe */
  612. port->mapbase = mmres->start;
  613. ltq_port->clk = clk;
  614. ltq_port->tx_irq = tx_irq;
  615. ltq_port->rx_irq = rx_irq;
  616. ltq_port->err_irq = err_irq;
  617. lqasc_port[pdev->id] = ltq_port;
  618. platform_set_drvdata(pdev, ltq_port);
  619. ret = uart_add_one_port(&lqasc_reg, port);
  620. return ret;
  621. }
  622. static struct platform_driver lqasc_driver = {
  623. .driver = {
  624. .name = DRVNAME,
  625. .owner = THIS_MODULE,
  626. },
  627. };
  628. int __init
  629. init_lqasc(void)
  630. {
  631. int ret;
  632. ret = uart_register_driver(&lqasc_reg);
  633. if (ret != 0)
  634. return ret;
  635. ret = platform_driver_probe(&lqasc_driver, lqasc_probe);
  636. if (ret != 0)
  637. uart_unregister_driver(&lqasc_reg);
  638. return ret;
  639. }
  640. module_init(init_lqasc);
  641. MODULE_DESCRIPTION("Lantiq serial port driver");
  642. MODULE_LICENSE("GPL");