lantiq.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  4. *
  5. * Copyright (C) 2004 Infineon IFAP DC COM CPE
  6. * Copyright (C) 2007 Felix Fietkau <nbd@openwrt.org>
  7. * Copyright (C) 2007 John Crispin <john@phrozen.org>
  8. * Copyright (C) 2010 Thomas Langer, <thomas.langer@lantiq.com>
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/console.h>
  12. #include <linux/device.h>
  13. #include <linux/gpio.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/ioport.h>
  17. #include <linux/lantiq.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/serial.h>
  22. #include <linux/serial_core.h>
  23. #include <linux/slab.h>
  24. #include <linux/sysrq.h>
  25. #include <linux/tty.h>
  26. #include <linux/tty_flip.h>
  27. #define PORT_LTQ_ASC 111
  28. #define MAXPORTS 2
  29. #define UART_DUMMY_UER_RX 1
  30. #define DRVNAME "lantiq,asc"
  31. #ifdef __BIG_ENDIAN
  32. #define LTQ_ASC_TBUF (0x0020 + 3)
  33. #define LTQ_ASC_RBUF (0x0024 + 3)
  34. #else
  35. #define LTQ_ASC_TBUF 0x0020
  36. #define LTQ_ASC_RBUF 0x0024
  37. #endif
  38. #define LTQ_ASC_FSTAT 0x0048
  39. #define LTQ_ASC_WHBSTATE 0x0018
  40. #define LTQ_ASC_STATE 0x0014
  41. #define LTQ_ASC_IRNCR 0x00F8
  42. #define LTQ_ASC_CLC 0x0000
  43. #define LTQ_ASC_ID 0x0008
  44. #define LTQ_ASC_PISEL 0x0004
  45. #define LTQ_ASC_TXFCON 0x0044
  46. #define LTQ_ASC_RXFCON 0x0040
  47. #define LTQ_ASC_CON 0x0010
  48. #define LTQ_ASC_BG 0x0050
  49. #define LTQ_ASC_IRNREN 0x00F4
  50. #define ASC_IRNREN_TX 0x1
  51. #define ASC_IRNREN_RX 0x2
  52. #define ASC_IRNREN_ERR 0x4
  53. #define ASC_IRNREN_TX_BUF 0x8
  54. #define ASC_IRNCR_TIR 0x1
  55. #define ASC_IRNCR_RIR 0x2
  56. #define ASC_IRNCR_EIR 0x4
  57. #define ASC_IRNCR_MASK GENMASK(2, 0)
  58. #define ASCOPT_CSIZE 0x3
  59. #define TXFIFO_FL 1
  60. #define RXFIFO_FL 1
  61. #define ASCCLC_DISS 0x2
  62. #define ASCCLC_RMCMASK 0x0000FF00
  63. #define ASCCLC_RMCOFFSET 8
  64. #define ASCCON_M_8ASYNC 0x0
  65. #define ASCCON_M_7ASYNC 0x2
  66. #define ASCCON_ODD 0x00000020
  67. #define ASCCON_STP 0x00000080
  68. #define ASCCON_BRS 0x00000100
  69. #define ASCCON_FDE 0x00000200
  70. #define ASCCON_R 0x00008000
  71. #define ASCCON_FEN 0x00020000
  72. #define ASCCON_ROEN 0x00080000
  73. #define ASCCON_TOEN 0x00100000
  74. #define ASCSTATE_PE 0x00010000
  75. #define ASCSTATE_FE 0x00020000
  76. #define ASCSTATE_ROE 0x00080000
  77. #define ASCSTATE_ANY (ASCSTATE_ROE|ASCSTATE_PE|ASCSTATE_FE)
  78. #define ASCWHBSTATE_CLRREN 0x00000001
  79. #define ASCWHBSTATE_SETREN 0x00000002
  80. #define ASCWHBSTATE_CLRPE 0x00000004
  81. #define ASCWHBSTATE_CLRFE 0x00000008
  82. #define ASCWHBSTATE_CLRROE 0x00000020
  83. #define ASCTXFCON_TXFEN 0x0001
  84. #define ASCTXFCON_TXFFLU 0x0002
  85. #define ASCTXFCON_TXFITLMASK 0x3F00
  86. #define ASCTXFCON_TXFITLOFF 8
  87. #define ASCRXFCON_RXFEN 0x0001
  88. #define ASCRXFCON_RXFFLU 0x0002
  89. #define ASCRXFCON_RXFITLMASK 0x3F00
  90. #define ASCRXFCON_RXFITLOFF 8
  91. #define ASCFSTAT_RXFFLMASK 0x003F
  92. #define ASCFSTAT_TXFFLMASK 0x3F00
  93. #define ASCFSTAT_TXFREEMASK 0x3F000000
  94. #define ASCFSTAT_TXFREEOFF 24
  95. static void lqasc_tx_chars(struct uart_port *port);
  96. static struct ltq_uart_port *lqasc_port[MAXPORTS];
  97. static struct uart_driver lqasc_reg;
  98. struct ltq_soc_data {
  99. int (*fetch_irq)(struct device *dev, struct ltq_uart_port *ltq_port);
  100. int (*request_irq)(struct uart_port *port);
  101. void (*free_irq)(struct uart_port *port);
  102. };
  103. struct ltq_uart_port {
  104. struct uart_port port;
  105. /* clock used to derive divider */
  106. struct clk *freqclk;
  107. /* clock gating of the ASC core */
  108. struct clk *clk;
  109. unsigned int tx_irq;
  110. unsigned int rx_irq;
  111. unsigned int err_irq;
  112. unsigned int common_irq;
  113. spinlock_t lock; /* exclusive access for multi core */
  114. const struct ltq_soc_data *soc;
  115. };
  116. static inline void asc_update_bits(u32 clear, u32 set, void __iomem *reg)
  117. {
  118. u32 tmp = __raw_readl(reg);
  119. __raw_writel((tmp & ~clear) | set, reg);
  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. struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
  136. spin_lock_irqsave(&ltq_port->lock, flags);
  137. lqasc_tx_chars(port);
  138. spin_unlock_irqrestore(&ltq_port->lock, flags);
  139. return;
  140. }
  141. static void
  142. lqasc_stop_rx(struct uart_port *port)
  143. {
  144. __raw_writel(ASCWHBSTATE_CLRREN, port->membase + LTQ_ASC_WHBSTATE);
  145. }
  146. static int
  147. lqasc_rx_chars(struct uart_port *port)
  148. {
  149. struct tty_port *tport = &port->state->port;
  150. unsigned int ch = 0, rsr = 0, fifocnt;
  151. fifocnt = __raw_readl(port->membase + LTQ_ASC_FSTAT) &
  152. ASCFSTAT_RXFFLMASK;
  153. while (fifocnt--) {
  154. u8 flag = TTY_NORMAL;
  155. ch = readb(port->membase + LTQ_ASC_RBUF);
  156. rsr = (__raw_readl(port->membase + LTQ_ASC_STATE)
  157. & ASCSTATE_ANY) | UART_DUMMY_UER_RX;
  158. tty_flip_buffer_push(tport);
  159. port->icount.rx++;
  160. /*
  161. * Note that the error handling code is
  162. * out of the main execution path
  163. */
  164. if (rsr & ASCSTATE_ANY) {
  165. if (rsr & ASCSTATE_PE) {
  166. port->icount.parity++;
  167. asc_update_bits(0, ASCWHBSTATE_CLRPE,
  168. port->membase + LTQ_ASC_WHBSTATE);
  169. } else if (rsr & ASCSTATE_FE) {
  170. port->icount.frame++;
  171. asc_update_bits(0, ASCWHBSTATE_CLRFE,
  172. port->membase + LTQ_ASC_WHBSTATE);
  173. }
  174. if (rsr & ASCSTATE_ROE) {
  175. port->icount.overrun++;
  176. asc_update_bits(0, ASCWHBSTATE_CLRROE,
  177. port->membase + LTQ_ASC_WHBSTATE);
  178. }
  179. rsr &= port->read_status_mask;
  180. if (rsr & ASCSTATE_PE)
  181. flag = TTY_PARITY;
  182. else if (rsr & ASCSTATE_FE)
  183. flag = TTY_FRAME;
  184. }
  185. if ((rsr & port->ignore_status_mask) == 0)
  186. tty_insert_flip_char(tport, ch, flag);
  187. if (rsr & ASCSTATE_ROE)
  188. /*
  189. * Overrun is special, since it's reported
  190. * immediately, and doesn't affect the current
  191. * character
  192. */
  193. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  194. }
  195. if (ch != 0)
  196. tty_flip_buffer_push(tport);
  197. return 0;
  198. }
  199. static void
  200. lqasc_tx_chars(struct uart_port *port)
  201. {
  202. struct circ_buf *xmit = &port->state->xmit;
  203. if (uart_tx_stopped(port)) {
  204. lqasc_stop_tx(port);
  205. return;
  206. }
  207. while (((__raw_readl(port->membase + LTQ_ASC_FSTAT) &
  208. ASCFSTAT_TXFREEMASK) >> ASCFSTAT_TXFREEOFF) != 0) {
  209. if (port->x_char) {
  210. writeb(port->x_char, port->membase + LTQ_ASC_TBUF);
  211. port->icount.tx++;
  212. port->x_char = 0;
  213. continue;
  214. }
  215. if (uart_circ_empty(xmit))
  216. break;
  217. writeb(port->state->xmit.buf[port->state->xmit.tail],
  218. port->membase + LTQ_ASC_TBUF);
  219. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  220. port->icount.tx++;
  221. }
  222. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  223. uart_write_wakeup(port);
  224. }
  225. static irqreturn_t
  226. lqasc_tx_int(int irq, void *_port)
  227. {
  228. unsigned long flags;
  229. struct uart_port *port = (struct uart_port *)_port;
  230. struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
  231. spin_lock_irqsave(&ltq_port->lock, flags);
  232. __raw_writel(ASC_IRNCR_TIR, port->membase + LTQ_ASC_IRNCR);
  233. spin_unlock_irqrestore(&ltq_port->lock, flags);
  234. lqasc_start_tx(port);
  235. return IRQ_HANDLED;
  236. }
  237. static irqreturn_t
  238. lqasc_err_int(int irq, void *_port)
  239. {
  240. unsigned long flags;
  241. struct uart_port *port = (struct uart_port *)_port;
  242. struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
  243. spin_lock_irqsave(&ltq_port->lock, flags);
  244. /* clear any pending interrupts */
  245. asc_update_bits(0, ASCWHBSTATE_CLRPE | ASCWHBSTATE_CLRFE |
  246. ASCWHBSTATE_CLRROE, port->membase + LTQ_ASC_WHBSTATE);
  247. spin_unlock_irqrestore(&ltq_port->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. struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
  256. spin_lock_irqsave(&ltq_port->lock, flags);
  257. __raw_writel(ASC_IRNCR_RIR, port->membase + LTQ_ASC_IRNCR);
  258. lqasc_rx_chars(port);
  259. spin_unlock_irqrestore(&ltq_port->lock, flags);
  260. return IRQ_HANDLED;
  261. }
  262. static irqreturn_t lqasc_irq(int irq, void *p)
  263. {
  264. unsigned long flags;
  265. u32 stat;
  266. struct uart_port *port = p;
  267. struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
  268. spin_lock_irqsave(&ltq_port->lock, flags);
  269. stat = readl(port->membase + LTQ_ASC_IRNCR);
  270. spin_unlock_irqrestore(&ltq_port->lock, flags);
  271. if (!(stat & ASC_IRNCR_MASK))
  272. return IRQ_NONE;
  273. if (stat & ASC_IRNCR_TIR)
  274. lqasc_tx_int(irq, p);
  275. if (stat & ASC_IRNCR_RIR)
  276. lqasc_rx_int(irq, p);
  277. if (stat & ASC_IRNCR_EIR)
  278. lqasc_err_int(irq, p);
  279. return IRQ_HANDLED;
  280. }
  281. static unsigned int
  282. lqasc_tx_empty(struct uart_port *port)
  283. {
  284. int status;
  285. status = __raw_readl(port->membase + LTQ_ASC_FSTAT) &
  286. ASCFSTAT_TXFFLMASK;
  287. return status ? 0 : TIOCSER_TEMT;
  288. }
  289. static unsigned int
  290. lqasc_get_mctrl(struct uart_port *port)
  291. {
  292. return TIOCM_CTS | TIOCM_CAR | TIOCM_DSR;
  293. }
  294. static void
  295. lqasc_set_mctrl(struct uart_port *port, u_int mctrl)
  296. {
  297. }
  298. static void
  299. lqasc_break_ctl(struct uart_port *port, int break_state)
  300. {
  301. }
  302. static int
  303. lqasc_startup(struct uart_port *port)
  304. {
  305. struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
  306. int retval;
  307. unsigned long flags;
  308. if (!IS_ERR(ltq_port->clk))
  309. clk_prepare_enable(ltq_port->clk);
  310. port->uartclk = clk_get_rate(ltq_port->freqclk);
  311. spin_lock_irqsave(&ltq_port->lock, flags);
  312. asc_update_bits(ASCCLC_DISS | ASCCLC_RMCMASK, (1 << ASCCLC_RMCOFFSET),
  313. port->membase + LTQ_ASC_CLC);
  314. __raw_writel(0, port->membase + LTQ_ASC_PISEL);
  315. __raw_writel(
  316. ((TXFIFO_FL << ASCTXFCON_TXFITLOFF) & ASCTXFCON_TXFITLMASK) |
  317. ASCTXFCON_TXFEN | ASCTXFCON_TXFFLU,
  318. port->membase + LTQ_ASC_TXFCON);
  319. __raw_writel(
  320. ((RXFIFO_FL << ASCRXFCON_RXFITLOFF) & ASCRXFCON_RXFITLMASK)
  321. | ASCRXFCON_RXFEN | ASCRXFCON_RXFFLU,
  322. port->membase + LTQ_ASC_RXFCON);
  323. /* make sure other settings are written to hardware before
  324. * setting enable bits
  325. */
  326. wmb();
  327. asc_update_bits(0, ASCCON_M_8ASYNC | ASCCON_FEN | ASCCON_TOEN |
  328. ASCCON_ROEN, port->membase + LTQ_ASC_CON);
  329. spin_unlock_irqrestore(&ltq_port->lock, flags);
  330. retval = ltq_port->soc->request_irq(port);
  331. if (retval)
  332. return retval;
  333. __raw_writel(ASC_IRNREN_RX | ASC_IRNREN_ERR | ASC_IRNREN_TX,
  334. port->membase + LTQ_ASC_IRNREN);
  335. return retval;
  336. }
  337. static void
  338. lqasc_shutdown(struct uart_port *port)
  339. {
  340. struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
  341. unsigned long flags;
  342. ltq_port->soc->free_irq(port);
  343. spin_lock_irqsave(&ltq_port->lock, flags);
  344. __raw_writel(0, port->membase + LTQ_ASC_CON);
  345. asc_update_bits(ASCRXFCON_RXFEN, ASCRXFCON_RXFFLU,
  346. port->membase + LTQ_ASC_RXFCON);
  347. asc_update_bits(ASCTXFCON_TXFEN, ASCTXFCON_TXFFLU,
  348. port->membase + LTQ_ASC_TXFCON);
  349. spin_unlock_irqrestore(&ltq_port->lock, flags);
  350. if (!IS_ERR(ltq_port->clk))
  351. clk_disable_unprepare(ltq_port->clk);
  352. }
  353. static void
  354. lqasc_set_termios(struct uart_port *port,
  355. struct ktermios *new, struct ktermios *old)
  356. {
  357. unsigned int cflag;
  358. unsigned int iflag;
  359. unsigned int divisor;
  360. unsigned int baud;
  361. unsigned int con = 0;
  362. unsigned long flags;
  363. struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
  364. cflag = new->c_cflag;
  365. iflag = new->c_iflag;
  366. switch (cflag & CSIZE) {
  367. case CS7:
  368. con = ASCCON_M_7ASYNC;
  369. break;
  370. case CS5:
  371. case CS6:
  372. default:
  373. new->c_cflag &= ~ CSIZE;
  374. new->c_cflag |= CS8;
  375. con = ASCCON_M_8ASYNC;
  376. break;
  377. }
  378. cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
  379. if (cflag & CSTOPB)
  380. con |= ASCCON_STP;
  381. if (cflag & PARENB) {
  382. if (!(cflag & PARODD))
  383. con &= ~ASCCON_ODD;
  384. else
  385. con |= ASCCON_ODD;
  386. }
  387. port->read_status_mask = ASCSTATE_ROE;
  388. if (iflag & INPCK)
  389. port->read_status_mask |= ASCSTATE_FE | ASCSTATE_PE;
  390. port->ignore_status_mask = 0;
  391. if (iflag & IGNPAR)
  392. port->ignore_status_mask |= ASCSTATE_FE | ASCSTATE_PE;
  393. if (iflag & IGNBRK) {
  394. /*
  395. * If we're ignoring parity and break indicators,
  396. * ignore overruns too (for real raw support).
  397. */
  398. if (iflag & IGNPAR)
  399. port->ignore_status_mask |= ASCSTATE_ROE;
  400. }
  401. if ((cflag & CREAD) == 0)
  402. port->ignore_status_mask |= UART_DUMMY_UER_RX;
  403. /* set error signals - framing, parity and overrun, enable receiver */
  404. con |= ASCCON_FEN | ASCCON_TOEN | ASCCON_ROEN;
  405. spin_lock_irqsave(&ltq_port->lock, flags);
  406. /* set up CON */
  407. asc_update_bits(0, con, port->membase + LTQ_ASC_CON);
  408. /* Set baud rate - take a divider of 2 into account */
  409. baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
  410. divisor = uart_get_divisor(port, baud);
  411. divisor = divisor / 2 - 1;
  412. /* disable the baudrate generator */
  413. asc_update_bits(ASCCON_R, 0, port->membase + LTQ_ASC_CON);
  414. /* make sure the fractional divider is off */
  415. asc_update_bits(ASCCON_FDE, 0, port->membase + LTQ_ASC_CON);
  416. /* set up to use divisor of 2 */
  417. asc_update_bits(ASCCON_BRS, 0, port->membase + LTQ_ASC_CON);
  418. /* now we can write the new baudrate into the register */
  419. __raw_writel(divisor, port->membase + LTQ_ASC_BG);
  420. /* turn the baudrate generator back on */
  421. asc_update_bits(0, ASCCON_R, port->membase + LTQ_ASC_CON);
  422. /* enable rx */
  423. __raw_writel(ASCWHBSTATE_SETREN, port->membase + LTQ_ASC_WHBSTATE);
  424. spin_unlock_irqrestore(&ltq_port->lock, flags);
  425. /* Don't rewrite B0 */
  426. if (tty_termios_baud_rate(new))
  427. tty_termios_encode_baud_rate(new, baud, baud);
  428. uart_update_timeout(port, cflag, baud);
  429. }
  430. static const char*
  431. lqasc_type(struct uart_port *port)
  432. {
  433. if (port->type == PORT_LTQ_ASC)
  434. return DRVNAME;
  435. else
  436. return NULL;
  437. }
  438. static void
  439. lqasc_release_port(struct uart_port *port)
  440. {
  441. struct platform_device *pdev = to_platform_device(port->dev);
  442. if (port->flags & UPF_IOREMAP) {
  443. devm_iounmap(&pdev->dev, port->membase);
  444. port->membase = NULL;
  445. }
  446. }
  447. static int
  448. lqasc_request_port(struct uart_port *port)
  449. {
  450. struct platform_device *pdev = to_platform_device(port->dev);
  451. struct resource *res;
  452. int size;
  453. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  454. if (!res) {
  455. dev_err(&pdev->dev, "cannot obtain I/O memory region");
  456. return -ENODEV;
  457. }
  458. size = resource_size(res);
  459. res = devm_request_mem_region(&pdev->dev, res->start,
  460. size, dev_name(&pdev->dev));
  461. if (!res) {
  462. dev_err(&pdev->dev, "cannot request I/O memory region");
  463. return -EBUSY;
  464. }
  465. if (port->flags & UPF_IOREMAP) {
  466. port->membase = devm_ioremap_nocache(&pdev->dev,
  467. port->mapbase, size);
  468. if (port->membase == NULL)
  469. return -ENOMEM;
  470. }
  471. return 0;
  472. }
  473. static void
  474. lqasc_config_port(struct uart_port *port, int flags)
  475. {
  476. if (flags & UART_CONFIG_TYPE) {
  477. port->type = PORT_LTQ_ASC;
  478. lqasc_request_port(port);
  479. }
  480. }
  481. static int
  482. lqasc_verify_port(struct uart_port *port,
  483. struct serial_struct *ser)
  484. {
  485. int ret = 0;
  486. if (ser->type != PORT_UNKNOWN && ser->type != PORT_LTQ_ASC)
  487. ret = -EINVAL;
  488. if (ser->irq < 0 || ser->irq >= NR_IRQS)
  489. ret = -EINVAL;
  490. if (ser->baud_base < 9600)
  491. ret = -EINVAL;
  492. return ret;
  493. }
  494. static const struct uart_ops lqasc_pops = {
  495. .tx_empty = lqasc_tx_empty,
  496. .set_mctrl = lqasc_set_mctrl,
  497. .get_mctrl = lqasc_get_mctrl,
  498. .stop_tx = lqasc_stop_tx,
  499. .start_tx = lqasc_start_tx,
  500. .stop_rx = lqasc_stop_rx,
  501. .break_ctl = lqasc_break_ctl,
  502. .startup = lqasc_startup,
  503. .shutdown = lqasc_shutdown,
  504. .set_termios = lqasc_set_termios,
  505. .type = lqasc_type,
  506. .release_port = lqasc_release_port,
  507. .request_port = lqasc_request_port,
  508. .config_port = lqasc_config_port,
  509. .verify_port = lqasc_verify_port,
  510. };
  511. static void
  512. lqasc_console_putchar(struct uart_port *port, int ch)
  513. {
  514. int fifofree;
  515. if (!port->membase)
  516. return;
  517. do {
  518. fifofree = (__raw_readl(port->membase + LTQ_ASC_FSTAT)
  519. & ASCFSTAT_TXFREEMASK) >> ASCFSTAT_TXFREEOFF;
  520. } while (fifofree == 0);
  521. writeb(ch, port->membase + LTQ_ASC_TBUF);
  522. }
  523. static void lqasc_serial_port_write(struct uart_port *port, const char *s,
  524. u_int count)
  525. {
  526. uart_console_write(port, s, count, lqasc_console_putchar);
  527. }
  528. static void
  529. lqasc_console_write(struct console *co, const char *s, u_int count)
  530. {
  531. struct ltq_uart_port *ltq_port;
  532. unsigned long flags;
  533. if (co->index >= MAXPORTS)
  534. return;
  535. ltq_port = lqasc_port[co->index];
  536. if (!ltq_port)
  537. return;
  538. spin_lock_irqsave(&ltq_port->lock, flags);
  539. lqasc_serial_port_write(&ltq_port->port, s, count);
  540. spin_unlock_irqrestore(&ltq_port->lock, flags);
  541. }
  542. static int __init
  543. lqasc_console_setup(struct console *co, char *options)
  544. {
  545. struct ltq_uart_port *ltq_port;
  546. struct uart_port *port;
  547. int baud = 115200;
  548. int bits = 8;
  549. int parity = 'n';
  550. int flow = 'n';
  551. if (co->index >= MAXPORTS)
  552. return -ENODEV;
  553. ltq_port = lqasc_port[co->index];
  554. if (!ltq_port)
  555. return -ENODEV;
  556. port = &ltq_port->port;
  557. if (!IS_ERR(ltq_port->clk))
  558. clk_prepare_enable(ltq_port->clk);
  559. port->uartclk = clk_get_rate(ltq_port->freqclk);
  560. if (options)
  561. uart_parse_options(options, &baud, &parity, &bits, &flow);
  562. return uart_set_options(port, co, baud, parity, bits, flow);
  563. }
  564. static struct console lqasc_console = {
  565. .name = "ttyLTQ",
  566. .write = lqasc_console_write,
  567. .device = uart_console_device,
  568. .setup = lqasc_console_setup,
  569. .flags = CON_PRINTBUFFER,
  570. .index = -1,
  571. .data = &lqasc_reg,
  572. };
  573. static int __init
  574. lqasc_console_init(void)
  575. {
  576. register_console(&lqasc_console);
  577. return 0;
  578. }
  579. console_initcall(lqasc_console_init);
  580. static void lqasc_serial_early_console_write(struct console *co,
  581. const char *s,
  582. u_int count)
  583. {
  584. struct earlycon_device *dev = co->data;
  585. lqasc_serial_port_write(&dev->port, s, count);
  586. }
  587. static int __init
  588. lqasc_serial_early_console_setup(struct earlycon_device *device,
  589. const char *opt)
  590. {
  591. if (!device->port.membase)
  592. return -ENODEV;
  593. device->con->write = lqasc_serial_early_console_write;
  594. return 0;
  595. }
  596. OF_EARLYCON_DECLARE(lantiq, "lantiq,asc", lqasc_serial_early_console_setup);
  597. OF_EARLYCON_DECLARE(lantiq, "intel,lgm-asc", lqasc_serial_early_console_setup);
  598. static struct uart_driver lqasc_reg = {
  599. .owner = THIS_MODULE,
  600. .driver_name = DRVNAME,
  601. .dev_name = "ttyLTQ",
  602. .major = 0,
  603. .minor = 0,
  604. .nr = MAXPORTS,
  605. .cons = &lqasc_console,
  606. };
  607. static int fetch_irq_lantiq(struct device *dev, struct ltq_uart_port *ltq_port)
  608. {
  609. struct uart_port *port = &ltq_port->port;
  610. struct resource irqres[3];
  611. int ret;
  612. ret = of_irq_to_resource_table(dev->of_node, irqres, 3);
  613. if (ret != 3) {
  614. dev_err(dev,
  615. "failed to get IRQs for serial port\n");
  616. return -ENODEV;
  617. }
  618. ltq_port->tx_irq = irqres[0].start;
  619. ltq_port->rx_irq = irqres[1].start;
  620. ltq_port->err_irq = irqres[2].start;
  621. port->irq = irqres[0].start;
  622. return 0;
  623. }
  624. static int request_irq_lantiq(struct uart_port *port)
  625. {
  626. struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
  627. int retval;
  628. retval = request_irq(ltq_port->tx_irq, lqasc_tx_int,
  629. 0, "asc_tx", port);
  630. if (retval) {
  631. dev_err(port->dev, "failed to request asc_tx\n");
  632. return retval;
  633. }
  634. retval = request_irq(ltq_port->rx_irq, lqasc_rx_int,
  635. 0, "asc_rx", port);
  636. if (retval) {
  637. dev_err(port->dev, "failed to request asc_rx\n");
  638. goto err1;
  639. }
  640. retval = request_irq(ltq_port->err_irq, lqasc_err_int,
  641. 0, "asc_err", port);
  642. if (retval) {
  643. dev_err(port->dev, "failed to request asc_err\n");
  644. goto err2;
  645. }
  646. return 0;
  647. err2:
  648. free_irq(ltq_port->rx_irq, port);
  649. err1:
  650. free_irq(ltq_port->tx_irq, port);
  651. return retval;
  652. }
  653. static void free_irq_lantiq(struct uart_port *port)
  654. {
  655. struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
  656. free_irq(ltq_port->tx_irq, port);
  657. free_irq(ltq_port->rx_irq, port);
  658. free_irq(ltq_port->err_irq, port);
  659. }
  660. static int fetch_irq_intel(struct device *dev, struct ltq_uart_port *ltq_port)
  661. {
  662. struct uart_port *port = &ltq_port->port;
  663. int ret;
  664. ret = of_irq_get(dev->of_node, 0);
  665. if (ret < 0) {
  666. dev_err(dev, "failed to fetch IRQ for serial port\n");
  667. return ret;
  668. }
  669. ltq_port->common_irq = ret;
  670. port->irq = ret;
  671. return 0;
  672. }
  673. static int request_irq_intel(struct uart_port *port)
  674. {
  675. struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
  676. int retval;
  677. retval = request_irq(ltq_port->common_irq, lqasc_irq, 0,
  678. "asc_irq", port);
  679. if (retval)
  680. dev_err(port->dev, "failed to request asc_irq\n");
  681. return retval;
  682. }
  683. static void free_irq_intel(struct uart_port *port)
  684. {
  685. struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
  686. free_irq(ltq_port->common_irq, port);
  687. }
  688. static int __init
  689. lqasc_probe(struct platform_device *pdev)
  690. {
  691. struct device_node *node = pdev->dev.of_node;
  692. struct ltq_uart_port *ltq_port;
  693. struct uart_port *port;
  694. struct resource *mmres;
  695. int line;
  696. int ret;
  697. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  698. if (!mmres) {
  699. dev_err(&pdev->dev,
  700. "failed to get memory for serial port\n");
  701. return -ENODEV;
  702. }
  703. ltq_port = devm_kzalloc(&pdev->dev, sizeof(struct ltq_uart_port),
  704. GFP_KERNEL);
  705. if (!ltq_port)
  706. return -ENOMEM;
  707. port = &ltq_port->port;
  708. ltq_port->soc = of_device_get_match_data(&pdev->dev);
  709. ret = ltq_port->soc->fetch_irq(&pdev->dev, ltq_port);
  710. if (ret)
  711. return ret;
  712. /* get serial id */
  713. line = of_alias_get_id(node, "serial");
  714. if (line < 0) {
  715. if (IS_ENABLED(CONFIG_LANTIQ)) {
  716. if (mmres->start == CPHYSADDR(LTQ_EARLY_ASC))
  717. line = 0;
  718. else
  719. line = 1;
  720. } else {
  721. dev_err(&pdev->dev, "failed to get alias id, errno %d\n",
  722. line);
  723. return line;
  724. }
  725. }
  726. if (lqasc_port[line]) {
  727. dev_err(&pdev->dev, "port %d already allocated\n", line);
  728. return -EBUSY;
  729. }
  730. port->iotype = SERIAL_IO_MEM;
  731. port->flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
  732. port->ops = &lqasc_pops;
  733. port->fifosize = 16;
  734. port->type = PORT_LTQ_ASC,
  735. port->line = line;
  736. port->dev = &pdev->dev;
  737. /* unused, just to be backward-compatible */
  738. port->mapbase = mmres->start;
  739. if (IS_ENABLED(CONFIG_LANTIQ) && !IS_ENABLED(CONFIG_COMMON_CLK))
  740. ltq_port->freqclk = clk_get_fpi();
  741. else
  742. ltq_port->freqclk = devm_clk_get(&pdev->dev, "freq");
  743. if (IS_ERR(ltq_port->freqclk)) {
  744. pr_err("failed to get fpi clk\n");
  745. return -ENOENT;
  746. }
  747. /* not all asc ports have clock gates, lets ignore the return code */
  748. if (IS_ENABLED(CONFIG_LANTIQ) && !IS_ENABLED(CONFIG_COMMON_CLK))
  749. ltq_port->clk = clk_get(&pdev->dev, NULL);
  750. else
  751. ltq_port->clk = devm_clk_get(&pdev->dev, "asc");
  752. spin_lock_init(&ltq_port->lock);
  753. lqasc_port[line] = ltq_port;
  754. platform_set_drvdata(pdev, ltq_port);
  755. ret = uart_add_one_port(&lqasc_reg, port);
  756. return ret;
  757. }
  758. static const struct ltq_soc_data soc_data_lantiq = {
  759. .fetch_irq = fetch_irq_lantiq,
  760. .request_irq = request_irq_lantiq,
  761. .free_irq = free_irq_lantiq,
  762. };
  763. static const struct ltq_soc_data soc_data_intel = {
  764. .fetch_irq = fetch_irq_intel,
  765. .request_irq = request_irq_intel,
  766. .free_irq = free_irq_intel,
  767. };
  768. static const struct of_device_id ltq_asc_match[] = {
  769. { .compatible = "lantiq,asc", .data = &soc_data_lantiq },
  770. { .compatible = "intel,lgm-asc", .data = &soc_data_intel },
  771. {},
  772. };
  773. static struct platform_driver lqasc_driver = {
  774. .driver = {
  775. .name = DRVNAME,
  776. .of_match_table = ltq_asc_match,
  777. },
  778. };
  779. static int __init
  780. init_lqasc(void)
  781. {
  782. int ret;
  783. ret = uart_register_driver(&lqasc_reg);
  784. if (ret != 0)
  785. return ret;
  786. ret = platform_driver_probe(&lqasc_driver, lqasc_probe);
  787. if (ret != 0)
  788. uart_unregister_driver(&lqasc_reg);
  789. return ret;
  790. }
  791. device_initcall(init_lqasc);