lantiq.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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/of_platform.h>
  34. #include <linux/of_address.h>
  35. #include <linux/of_irq.h>
  36. #include <linux/io.h>
  37. #include <linux/clk.h>
  38. #include <linux/gpio.h>
  39. #include <lantiq_soc.h>
  40. #define PORT_LTQ_ASC 111
  41. #define MAXPORTS 2
  42. #define UART_DUMMY_UER_RX 1
  43. #define DRVNAME "lantiq,asc"
  44. #ifdef __BIG_ENDIAN
  45. #define LTQ_ASC_TBUF (0x0020 + 3)
  46. #define LTQ_ASC_RBUF (0x0024 + 3)
  47. #else
  48. #define LTQ_ASC_TBUF 0x0020
  49. #define LTQ_ASC_RBUF 0x0024
  50. #endif
  51. #define LTQ_ASC_FSTAT 0x0048
  52. #define LTQ_ASC_WHBSTATE 0x0018
  53. #define LTQ_ASC_STATE 0x0014
  54. #define LTQ_ASC_IRNCR 0x00F8
  55. #define LTQ_ASC_CLC 0x0000
  56. #define LTQ_ASC_ID 0x0008
  57. #define LTQ_ASC_PISEL 0x0004
  58. #define LTQ_ASC_TXFCON 0x0044
  59. #define LTQ_ASC_RXFCON 0x0040
  60. #define LTQ_ASC_CON 0x0010
  61. #define LTQ_ASC_BG 0x0050
  62. #define LTQ_ASC_IRNREN 0x00F4
  63. #define ASC_IRNREN_TX 0x1
  64. #define ASC_IRNREN_RX 0x2
  65. #define ASC_IRNREN_ERR 0x4
  66. #define ASC_IRNREN_TX_BUF 0x8
  67. #define ASC_IRNCR_TIR 0x1
  68. #define ASC_IRNCR_RIR 0x2
  69. #define ASC_IRNCR_EIR 0x4
  70. #define ASCOPT_CSIZE 0x3
  71. #define TXFIFO_FL 1
  72. #define RXFIFO_FL 1
  73. #define ASCCLC_DISS 0x2
  74. #define ASCCLC_RMCMASK 0x0000FF00
  75. #define ASCCLC_RMCOFFSET 8
  76. #define ASCCON_M_8ASYNC 0x0
  77. #define ASCCON_M_7ASYNC 0x2
  78. #define ASCCON_ODD 0x00000020
  79. #define ASCCON_STP 0x00000080
  80. #define ASCCON_BRS 0x00000100
  81. #define ASCCON_FDE 0x00000200
  82. #define ASCCON_R 0x00008000
  83. #define ASCCON_FEN 0x00020000
  84. #define ASCCON_ROEN 0x00080000
  85. #define ASCCON_TOEN 0x00100000
  86. #define ASCSTATE_PE 0x00010000
  87. #define ASCSTATE_FE 0x00020000
  88. #define ASCSTATE_ROE 0x00080000
  89. #define ASCSTATE_ANY (ASCSTATE_ROE|ASCSTATE_PE|ASCSTATE_FE)
  90. #define ASCWHBSTATE_CLRREN 0x00000001
  91. #define ASCWHBSTATE_SETREN 0x00000002
  92. #define ASCWHBSTATE_CLRPE 0x00000004
  93. #define ASCWHBSTATE_CLRFE 0x00000008
  94. #define ASCWHBSTATE_CLRROE 0x00000020
  95. #define ASCTXFCON_TXFEN 0x0001
  96. #define ASCTXFCON_TXFFLU 0x0002
  97. #define ASCTXFCON_TXFITLMASK 0x3F00
  98. #define ASCTXFCON_TXFITLOFF 8
  99. #define ASCRXFCON_RXFEN 0x0001
  100. #define ASCRXFCON_RXFFLU 0x0002
  101. #define ASCRXFCON_RXFITLMASK 0x3F00
  102. #define ASCRXFCON_RXFITLOFF 8
  103. #define ASCFSTAT_RXFFLMASK 0x003F
  104. #define ASCFSTAT_TXFFLMASK 0x3F00
  105. #define ASCFSTAT_TXFREEMASK 0x3F000000
  106. #define ASCFSTAT_TXFREEOFF 24
  107. static void lqasc_tx_chars(struct uart_port *port);
  108. static struct ltq_uart_port *lqasc_port[MAXPORTS];
  109. static struct uart_driver lqasc_reg;
  110. static DEFINE_SPINLOCK(ltq_asc_lock);
  111. struct ltq_uart_port {
  112. struct uart_port port;
  113. /* clock used to derive divider */
  114. struct clk *fpiclk;
  115. /* clock gating of the ASC core */
  116. struct clk *clk;
  117. unsigned int tx_irq;
  118. unsigned int rx_irq;
  119. unsigned int err_irq;
  120. };
  121. static inline struct
  122. ltq_uart_port *to_ltq_uart_port(struct uart_port *port)
  123. {
  124. return container_of(port, struct ltq_uart_port, port);
  125. }
  126. static void
  127. lqasc_stop_tx(struct uart_port *port)
  128. {
  129. return;
  130. }
  131. static void
  132. lqasc_start_tx(struct uart_port *port)
  133. {
  134. unsigned long flags;
  135. spin_lock_irqsave(&ltq_asc_lock, flags);
  136. lqasc_tx_chars(port);
  137. spin_unlock_irqrestore(&ltq_asc_lock, flags);
  138. return;
  139. }
  140. static void
  141. lqasc_stop_rx(struct uart_port *port)
  142. {
  143. ltq_w32(ASCWHBSTATE_CLRREN, port->membase + LTQ_ASC_WHBSTATE);
  144. }
  145. static int
  146. lqasc_rx_chars(struct uart_port *port)
  147. {
  148. struct tty_port *tport = &port->state->port;
  149. unsigned int ch = 0, rsr = 0, fifocnt;
  150. fifocnt = ltq_r32(port->membase + LTQ_ASC_FSTAT) & ASCFSTAT_RXFFLMASK;
  151. while (fifocnt--) {
  152. u8 flag = TTY_NORMAL;
  153. ch = ltq_r8(port->membase + LTQ_ASC_RBUF);
  154. rsr = (ltq_r32(port->membase + LTQ_ASC_STATE)
  155. & ASCSTATE_ANY) | UART_DUMMY_UER_RX;
  156. tty_flip_buffer_push(tport);
  157. port->icount.rx++;
  158. /*
  159. * Note that the error handling code is
  160. * out of the main execution path
  161. */
  162. if (rsr & ASCSTATE_ANY) {
  163. if (rsr & ASCSTATE_PE) {
  164. port->icount.parity++;
  165. ltq_w32_mask(0, ASCWHBSTATE_CLRPE,
  166. port->membase + LTQ_ASC_WHBSTATE);
  167. } else if (rsr & ASCSTATE_FE) {
  168. port->icount.frame++;
  169. ltq_w32_mask(0, ASCWHBSTATE_CLRFE,
  170. port->membase + LTQ_ASC_WHBSTATE);
  171. }
  172. if (rsr & ASCSTATE_ROE) {
  173. port->icount.overrun++;
  174. ltq_w32_mask(0, ASCWHBSTATE_CLRROE,
  175. port->membase + LTQ_ASC_WHBSTATE);
  176. }
  177. rsr &= port->read_status_mask;
  178. if (rsr & ASCSTATE_PE)
  179. flag = TTY_PARITY;
  180. else if (rsr & ASCSTATE_FE)
  181. flag = TTY_FRAME;
  182. }
  183. if ((rsr & port->ignore_status_mask) == 0)
  184. tty_insert_flip_char(tport, ch, flag);
  185. if (rsr & ASCSTATE_ROE)
  186. /*
  187. * Overrun is special, since it's reported
  188. * immediately, and doesn't affect the current
  189. * character
  190. */
  191. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  192. }
  193. if (ch != 0)
  194. tty_flip_buffer_push(tport);
  195. return 0;
  196. }
  197. static void
  198. lqasc_tx_chars(struct uart_port *port)
  199. {
  200. struct circ_buf *xmit = &port->state->xmit;
  201. if (uart_tx_stopped(port)) {
  202. lqasc_stop_tx(port);
  203. return;
  204. }
  205. while (((ltq_r32(port->membase + LTQ_ASC_FSTAT) &
  206. ASCFSTAT_TXFREEMASK) >> ASCFSTAT_TXFREEOFF) != 0) {
  207. if (port->x_char) {
  208. ltq_w8(port->x_char, port->membase + LTQ_ASC_TBUF);
  209. port->icount.tx++;
  210. port->x_char = 0;
  211. continue;
  212. }
  213. if (uart_circ_empty(xmit))
  214. break;
  215. ltq_w8(port->state->xmit.buf[port->state->xmit.tail],
  216. port->membase + LTQ_ASC_TBUF);
  217. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  218. port->icount.tx++;
  219. }
  220. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  221. uart_write_wakeup(port);
  222. }
  223. static irqreturn_t
  224. lqasc_tx_int(int irq, void *_port)
  225. {
  226. unsigned long flags;
  227. struct uart_port *port = (struct uart_port *)_port;
  228. spin_lock_irqsave(&ltq_asc_lock, flags);
  229. ltq_w32(ASC_IRNCR_TIR, port->membase + LTQ_ASC_IRNCR);
  230. spin_unlock_irqrestore(&ltq_asc_lock, flags);
  231. lqasc_start_tx(port);
  232. return IRQ_HANDLED;
  233. }
  234. static irqreturn_t
  235. lqasc_err_int(int irq, void *_port)
  236. {
  237. unsigned long flags;
  238. struct uart_port *port = (struct uart_port *)_port;
  239. spin_lock_irqsave(&ltq_asc_lock, flags);
  240. /* clear any pending interrupts */
  241. ltq_w32_mask(0, ASCWHBSTATE_CLRPE | ASCWHBSTATE_CLRFE |
  242. ASCWHBSTATE_CLRROE, port->membase + LTQ_ASC_WHBSTATE);
  243. spin_unlock_irqrestore(&ltq_asc_lock, flags);
  244. return IRQ_HANDLED;
  245. }
  246. static irqreturn_t
  247. lqasc_rx_int(int irq, void *_port)
  248. {
  249. unsigned long flags;
  250. struct uart_port *port = (struct uart_port *)_port;
  251. spin_lock_irqsave(&ltq_asc_lock, flags);
  252. ltq_w32(ASC_IRNCR_RIR, port->membase + LTQ_ASC_IRNCR);
  253. lqasc_rx_chars(port);
  254. spin_unlock_irqrestore(&ltq_asc_lock, flags);
  255. return IRQ_HANDLED;
  256. }
  257. static unsigned int
  258. lqasc_tx_empty(struct uart_port *port)
  259. {
  260. int status;
  261. status = ltq_r32(port->membase + LTQ_ASC_FSTAT) & ASCFSTAT_TXFFLMASK;
  262. return status ? 0 : TIOCSER_TEMT;
  263. }
  264. static unsigned int
  265. lqasc_get_mctrl(struct uart_port *port)
  266. {
  267. return TIOCM_CTS | TIOCM_CAR | TIOCM_DSR;
  268. }
  269. static void
  270. lqasc_set_mctrl(struct uart_port *port, u_int mctrl)
  271. {
  272. }
  273. static void
  274. lqasc_break_ctl(struct uart_port *port, int break_state)
  275. {
  276. }
  277. static int
  278. lqasc_startup(struct uart_port *port)
  279. {
  280. struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
  281. int retval;
  282. if (!IS_ERR(ltq_port->clk))
  283. clk_enable(ltq_port->clk);
  284. port->uartclk = clk_get_rate(ltq_port->fpiclk);
  285. ltq_w32_mask(ASCCLC_DISS | ASCCLC_RMCMASK, (1 << ASCCLC_RMCOFFSET),
  286. port->membase + LTQ_ASC_CLC);
  287. ltq_w32(0, port->membase + LTQ_ASC_PISEL);
  288. ltq_w32(
  289. ((TXFIFO_FL << ASCTXFCON_TXFITLOFF) & ASCTXFCON_TXFITLMASK) |
  290. ASCTXFCON_TXFEN | ASCTXFCON_TXFFLU,
  291. port->membase + LTQ_ASC_TXFCON);
  292. ltq_w32(
  293. ((RXFIFO_FL << ASCRXFCON_RXFITLOFF) & ASCRXFCON_RXFITLMASK)
  294. | ASCRXFCON_RXFEN | ASCRXFCON_RXFFLU,
  295. port->membase + LTQ_ASC_RXFCON);
  296. /* make sure other settings are written to hardware before
  297. * setting enable bits
  298. */
  299. wmb();
  300. ltq_w32_mask(0, ASCCON_M_8ASYNC | ASCCON_FEN | ASCCON_TOEN |
  301. ASCCON_ROEN, port->membase + LTQ_ASC_CON);
  302. retval = request_irq(ltq_port->tx_irq, lqasc_tx_int,
  303. 0, "asc_tx", port);
  304. if (retval) {
  305. pr_err("failed to request lqasc_tx_int\n");
  306. return retval;
  307. }
  308. retval = request_irq(ltq_port->rx_irq, lqasc_rx_int,
  309. 0, "asc_rx", port);
  310. if (retval) {
  311. pr_err("failed to request lqasc_rx_int\n");
  312. goto err1;
  313. }
  314. retval = request_irq(ltq_port->err_irq, lqasc_err_int,
  315. 0, "asc_err", port);
  316. if (retval) {
  317. pr_err("failed to request lqasc_err_int\n");
  318. goto err2;
  319. }
  320. ltq_w32(ASC_IRNREN_RX | ASC_IRNREN_ERR | ASC_IRNREN_TX,
  321. port->membase + LTQ_ASC_IRNREN);
  322. return 0;
  323. err2:
  324. free_irq(ltq_port->rx_irq, port);
  325. err1:
  326. free_irq(ltq_port->tx_irq, port);
  327. return retval;
  328. }
  329. static void
  330. lqasc_shutdown(struct uart_port *port)
  331. {
  332. struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
  333. free_irq(ltq_port->tx_irq, port);
  334. free_irq(ltq_port->rx_irq, port);
  335. free_irq(ltq_port->err_irq, port);
  336. ltq_w32(0, port->membase + LTQ_ASC_CON);
  337. ltq_w32_mask(ASCRXFCON_RXFEN, ASCRXFCON_RXFFLU,
  338. port->membase + LTQ_ASC_RXFCON);
  339. ltq_w32_mask(ASCTXFCON_TXFEN, ASCTXFCON_TXFFLU,
  340. port->membase + LTQ_ASC_TXFCON);
  341. if (!IS_ERR(ltq_port->clk))
  342. clk_disable(ltq_port->clk);
  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. uart_update_timeout(port, cflag, baud);
  419. }
  420. static const char*
  421. lqasc_type(struct uart_port *port)
  422. {
  423. if (port->type == PORT_LTQ_ASC)
  424. return DRVNAME;
  425. else
  426. return NULL;
  427. }
  428. static void
  429. lqasc_release_port(struct uart_port *port)
  430. {
  431. struct platform_device *pdev = to_platform_device(port->dev);
  432. if (port->flags & UPF_IOREMAP) {
  433. devm_iounmap(&pdev->dev, port->membase);
  434. port->membase = NULL;
  435. }
  436. }
  437. static int
  438. lqasc_request_port(struct uart_port *port)
  439. {
  440. struct platform_device *pdev = to_platform_device(port->dev);
  441. struct resource *res;
  442. int size;
  443. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  444. if (!res) {
  445. dev_err(&pdev->dev, "cannot obtain I/O memory region");
  446. return -ENODEV;
  447. }
  448. size = resource_size(res);
  449. res = devm_request_mem_region(&pdev->dev, res->start,
  450. size, dev_name(&pdev->dev));
  451. if (!res) {
  452. dev_err(&pdev->dev, "cannot request I/O memory region");
  453. return -EBUSY;
  454. }
  455. if (port->flags & UPF_IOREMAP) {
  456. port->membase = devm_ioremap_nocache(&pdev->dev,
  457. port->mapbase, size);
  458. if (port->membase == NULL)
  459. return -ENOMEM;
  460. }
  461. return 0;
  462. }
  463. static void
  464. lqasc_config_port(struct uart_port *port, int flags)
  465. {
  466. if (flags & UART_CONFIG_TYPE) {
  467. port->type = PORT_LTQ_ASC;
  468. lqasc_request_port(port);
  469. }
  470. }
  471. static int
  472. lqasc_verify_port(struct uart_port *port,
  473. struct serial_struct *ser)
  474. {
  475. int ret = 0;
  476. if (ser->type != PORT_UNKNOWN && ser->type != PORT_LTQ_ASC)
  477. ret = -EINVAL;
  478. if (ser->irq < 0 || ser->irq >= NR_IRQS)
  479. ret = -EINVAL;
  480. if (ser->baud_base < 9600)
  481. ret = -EINVAL;
  482. return ret;
  483. }
  484. static struct uart_ops lqasc_pops = {
  485. .tx_empty = lqasc_tx_empty,
  486. .set_mctrl = lqasc_set_mctrl,
  487. .get_mctrl = lqasc_get_mctrl,
  488. .stop_tx = lqasc_stop_tx,
  489. .start_tx = lqasc_start_tx,
  490. .stop_rx = lqasc_stop_rx,
  491. .break_ctl = lqasc_break_ctl,
  492. .startup = lqasc_startup,
  493. .shutdown = lqasc_shutdown,
  494. .set_termios = lqasc_set_termios,
  495. .type = lqasc_type,
  496. .release_port = lqasc_release_port,
  497. .request_port = lqasc_request_port,
  498. .config_port = lqasc_config_port,
  499. .verify_port = lqasc_verify_port,
  500. };
  501. static void
  502. lqasc_console_putchar(struct uart_port *port, int ch)
  503. {
  504. int fifofree;
  505. if (!port->membase)
  506. return;
  507. do {
  508. fifofree = (ltq_r32(port->membase + LTQ_ASC_FSTAT)
  509. & ASCFSTAT_TXFREEMASK) >> ASCFSTAT_TXFREEOFF;
  510. } while (fifofree == 0);
  511. ltq_w8(ch, port->membase + LTQ_ASC_TBUF);
  512. }
  513. static void
  514. lqasc_console_write(struct console *co, const char *s, u_int count)
  515. {
  516. struct ltq_uart_port *ltq_port;
  517. struct uart_port *port;
  518. unsigned long flags;
  519. if (co->index >= MAXPORTS)
  520. return;
  521. ltq_port = lqasc_port[co->index];
  522. if (!ltq_port)
  523. return;
  524. port = &ltq_port->port;
  525. spin_lock_irqsave(&ltq_asc_lock, flags);
  526. uart_console_write(port, s, count, lqasc_console_putchar);
  527. spin_unlock_irqrestore(&ltq_asc_lock, flags);
  528. }
  529. static int __init
  530. lqasc_console_setup(struct console *co, char *options)
  531. {
  532. struct ltq_uart_port *ltq_port;
  533. struct uart_port *port;
  534. int baud = 115200;
  535. int bits = 8;
  536. int parity = 'n';
  537. int flow = 'n';
  538. if (co->index >= MAXPORTS)
  539. return -ENODEV;
  540. ltq_port = lqasc_port[co->index];
  541. if (!ltq_port)
  542. return -ENODEV;
  543. port = &ltq_port->port;
  544. if (!IS_ERR(ltq_port->clk))
  545. clk_enable(ltq_port->clk);
  546. port->uartclk = clk_get_rate(ltq_port->fpiclk);
  547. if (options)
  548. uart_parse_options(options, &baud, &parity, &bits, &flow);
  549. return uart_set_options(port, co, baud, parity, bits, flow);
  550. }
  551. static struct console lqasc_console = {
  552. .name = "ttyLTQ",
  553. .write = lqasc_console_write,
  554. .device = uart_console_device,
  555. .setup = lqasc_console_setup,
  556. .flags = CON_PRINTBUFFER,
  557. .index = -1,
  558. .data = &lqasc_reg,
  559. };
  560. static int __init
  561. lqasc_console_init(void)
  562. {
  563. register_console(&lqasc_console);
  564. return 0;
  565. }
  566. console_initcall(lqasc_console_init);
  567. static struct uart_driver lqasc_reg = {
  568. .owner = THIS_MODULE,
  569. .driver_name = DRVNAME,
  570. .dev_name = "ttyLTQ",
  571. .major = 0,
  572. .minor = 0,
  573. .nr = MAXPORTS,
  574. .cons = &lqasc_console,
  575. };
  576. static int __init
  577. lqasc_probe(struct platform_device *pdev)
  578. {
  579. struct device_node *node = pdev->dev.of_node;
  580. struct ltq_uart_port *ltq_port;
  581. struct uart_port *port;
  582. struct resource *mmres, irqres[3];
  583. int line = 0;
  584. int ret;
  585. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  586. ret = of_irq_to_resource_table(node, irqres, 3);
  587. if (!mmres || (ret != 3)) {
  588. dev_err(&pdev->dev,
  589. "failed to get memory/irq for serial port\n");
  590. return -ENODEV;
  591. }
  592. /* check if this is the console port */
  593. if (mmres->start != CPHYSADDR(LTQ_EARLY_ASC))
  594. line = 1;
  595. if (lqasc_port[line]) {
  596. dev_err(&pdev->dev, "port %d already allocated\n", line);
  597. return -EBUSY;
  598. }
  599. ltq_port = devm_kzalloc(&pdev->dev, sizeof(struct ltq_uart_port),
  600. GFP_KERNEL);
  601. if (!ltq_port)
  602. return -ENOMEM;
  603. port = &ltq_port->port;
  604. port->iotype = SERIAL_IO_MEM;
  605. port->flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
  606. port->ops = &lqasc_pops;
  607. port->fifosize = 16;
  608. port->type = PORT_LTQ_ASC,
  609. port->line = line;
  610. port->dev = &pdev->dev;
  611. /* unused, just to be backward-compatible */
  612. port->irq = irqres[0].start;
  613. port->mapbase = mmres->start;
  614. ltq_port->fpiclk = clk_get_fpi();
  615. if (IS_ERR(ltq_port->fpiclk)) {
  616. pr_err("failed to get fpi clk\n");
  617. return -ENOENT;
  618. }
  619. /* not all asc ports have clock gates, lets ignore the return code */
  620. ltq_port->clk = clk_get(&pdev->dev, NULL);
  621. ltq_port->tx_irq = irqres[0].start;
  622. ltq_port->rx_irq = irqres[1].start;
  623. ltq_port->err_irq = irqres[2].start;
  624. lqasc_port[line] = ltq_port;
  625. platform_set_drvdata(pdev, ltq_port);
  626. ret = uart_add_one_port(&lqasc_reg, port);
  627. return ret;
  628. }
  629. static const struct of_device_id ltq_asc_match[] = {
  630. { .compatible = DRVNAME },
  631. {},
  632. };
  633. MODULE_DEVICE_TABLE(of, ltq_asc_match);
  634. static struct platform_driver lqasc_driver = {
  635. .driver = {
  636. .name = DRVNAME,
  637. .of_match_table = ltq_asc_match,
  638. },
  639. };
  640. int __init
  641. init_lqasc(void)
  642. {
  643. int ret;
  644. ret = uart_register_driver(&lqasc_reg);
  645. if (ret != 0)
  646. return ret;
  647. ret = platform_driver_probe(&lqasc_driver, lqasc_probe);
  648. if (ret != 0)
  649. uart_unregister_driver(&lqasc_reg);
  650. return ret;
  651. }
  652. module_init(init_lqasc);
  653. MODULE_DESCRIPTION("Lantiq serial port driver");
  654. MODULE_LICENSE("GPL");