bcm63xx_uart.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Derived from many drivers using generic_serial interface.
  4. *
  5. * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
  6. *
  7. * Serial driver for BCM63xx integrated UART.
  8. *
  9. * Hardware flow control was _not_ tested since I only have RX/TX on
  10. * my board.
  11. */
  12. #if defined(CONFIG_SERIAL_BCM63XX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  13. #define SUPPORT_SYSRQ
  14. #endif
  15. #include <linux/kernel.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/init.h>
  18. #include <linux/delay.h>
  19. #include <linux/module.h>
  20. #include <linux/console.h>
  21. #include <linux/clk.h>
  22. #include <linux/tty.h>
  23. #include <linux/tty_flip.h>
  24. #include <linux/sysrq.h>
  25. #include <linux/serial.h>
  26. #include <linux/serial_core.h>
  27. #include <linux/serial_bcm63xx.h>
  28. #include <linux/io.h>
  29. #include <linux/of.h>
  30. #define BCM63XX_NR_UARTS 2
  31. static struct uart_port ports[BCM63XX_NR_UARTS];
  32. /*
  33. * rx interrupt mask / stat
  34. *
  35. * mask:
  36. * - rx fifo full
  37. * - rx fifo above threshold
  38. * - rx fifo not empty for too long
  39. */
  40. #define UART_RX_INT_MASK (UART_IR_MASK(UART_IR_RXOVER) | \
  41. UART_IR_MASK(UART_IR_RXTHRESH) | \
  42. UART_IR_MASK(UART_IR_RXTIMEOUT))
  43. #define UART_RX_INT_STAT (UART_IR_STAT(UART_IR_RXOVER) | \
  44. UART_IR_STAT(UART_IR_RXTHRESH) | \
  45. UART_IR_STAT(UART_IR_RXTIMEOUT))
  46. /*
  47. * tx interrupt mask / stat
  48. *
  49. * mask:
  50. * - tx fifo empty
  51. * - tx fifo below threshold
  52. */
  53. #define UART_TX_INT_MASK (UART_IR_MASK(UART_IR_TXEMPTY) | \
  54. UART_IR_MASK(UART_IR_TXTRESH))
  55. #define UART_TX_INT_STAT (UART_IR_STAT(UART_IR_TXEMPTY) | \
  56. UART_IR_STAT(UART_IR_TXTRESH))
  57. /*
  58. * external input interrupt
  59. *
  60. * mask: any edge on CTS, DCD
  61. */
  62. #define UART_EXTINP_INT_MASK (UART_EXTINP_IRMASK(UART_EXTINP_IR_CTS) | \
  63. UART_EXTINP_IRMASK(UART_EXTINP_IR_DCD))
  64. /*
  65. * handy uart register accessor
  66. */
  67. static inline unsigned int bcm_uart_readl(struct uart_port *port,
  68. unsigned int offset)
  69. {
  70. return __raw_readl(port->membase + offset);
  71. }
  72. static inline void bcm_uart_writel(struct uart_port *port,
  73. unsigned int value, unsigned int offset)
  74. {
  75. __raw_writel(value, port->membase + offset);
  76. }
  77. /*
  78. * serial core request to check if uart tx fifo is empty
  79. */
  80. static unsigned int bcm_uart_tx_empty(struct uart_port *port)
  81. {
  82. unsigned int val;
  83. val = bcm_uart_readl(port, UART_IR_REG);
  84. return (val & UART_IR_STAT(UART_IR_TXEMPTY)) ? 1 : 0;
  85. }
  86. /*
  87. * serial core request to set RTS and DTR pin state and loopback mode
  88. */
  89. static void bcm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  90. {
  91. unsigned int val;
  92. val = bcm_uart_readl(port, UART_MCTL_REG);
  93. val &= ~(UART_MCTL_DTR_MASK | UART_MCTL_RTS_MASK);
  94. /* invert of written value is reflected on the pin */
  95. if (!(mctrl & TIOCM_DTR))
  96. val |= UART_MCTL_DTR_MASK;
  97. if (!(mctrl & TIOCM_RTS))
  98. val |= UART_MCTL_RTS_MASK;
  99. bcm_uart_writel(port, val, UART_MCTL_REG);
  100. val = bcm_uart_readl(port, UART_CTL_REG);
  101. if (mctrl & TIOCM_LOOP)
  102. val |= UART_CTL_LOOPBACK_MASK;
  103. else
  104. val &= ~UART_CTL_LOOPBACK_MASK;
  105. bcm_uart_writel(port, val, UART_CTL_REG);
  106. }
  107. /*
  108. * serial core request to return RI, CTS, DCD and DSR pin state
  109. */
  110. static unsigned int bcm_uart_get_mctrl(struct uart_port *port)
  111. {
  112. unsigned int val, mctrl;
  113. mctrl = 0;
  114. val = bcm_uart_readl(port, UART_EXTINP_REG);
  115. if (val & UART_EXTINP_RI_MASK)
  116. mctrl |= TIOCM_RI;
  117. if (val & UART_EXTINP_CTS_MASK)
  118. mctrl |= TIOCM_CTS;
  119. if (val & UART_EXTINP_DCD_MASK)
  120. mctrl |= TIOCM_CD;
  121. if (val & UART_EXTINP_DSR_MASK)
  122. mctrl |= TIOCM_DSR;
  123. return mctrl;
  124. }
  125. /*
  126. * serial core request to disable tx ASAP (used for flow control)
  127. */
  128. static void bcm_uart_stop_tx(struct uart_port *port)
  129. {
  130. unsigned int val;
  131. val = bcm_uart_readl(port, UART_CTL_REG);
  132. val &= ~(UART_CTL_TXEN_MASK);
  133. bcm_uart_writel(port, val, UART_CTL_REG);
  134. val = bcm_uart_readl(port, UART_IR_REG);
  135. val &= ~UART_TX_INT_MASK;
  136. bcm_uart_writel(port, val, UART_IR_REG);
  137. }
  138. /*
  139. * serial core request to (re)enable tx
  140. */
  141. static void bcm_uart_start_tx(struct uart_port *port)
  142. {
  143. unsigned int val;
  144. val = bcm_uart_readl(port, UART_IR_REG);
  145. val |= UART_TX_INT_MASK;
  146. bcm_uart_writel(port, val, UART_IR_REG);
  147. val = bcm_uart_readl(port, UART_CTL_REG);
  148. val |= UART_CTL_TXEN_MASK;
  149. bcm_uart_writel(port, val, UART_CTL_REG);
  150. }
  151. /*
  152. * serial core request to stop rx, called before port shutdown
  153. */
  154. static void bcm_uart_stop_rx(struct uart_port *port)
  155. {
  156. unsigned int val;
  157. val = bcm_uart_readl(port, UART_IR_REG);
  158. val &= ~UART_RX_INT_MASK;
  159. bcm_uart_writel(port, val, UART_IR_REG);
  160. }
  161. /*
  162. * serial core request to enable modem status interrupt reporting
  163. */
  164. static void bcm_uart_enable_ms(struct uart_port *port)
  165. {
  166. unsigned int val;
  167. val = bcm_uart_readl(port, UART_IR_REG);
  168. val |= UART_IR_MASK(UART_IR_EXTIP);
  169. bcm_uart_writel(port, val, UART_IR_REG);
  170. }
  171. /*
  172. * serial core request to start/stop emitting break char
  173. */
  174. static void bcm_uart_break_ctl(struct uart_port *port, int ctl)
  175. {
  176. unsigned long flags;
  177. unsigned int val;
  178. spin_lock_irqsave(&port->lock, flags);
  179. val = bcm_uart_readl(port, UART_CTL_REG);
  180. if (ctl)
  181. val |= UART_CTL_XMITBRK_MASK;
  182. else
  183. val &= ~UART_CTL_XMITBRK_MASK;
  184. bcm_uart_writel(port, val, UART_CTL_REG);
  185. spin_unlock_irqrestore(&port->lock, flags);
  186. }
  187. /*
  188. * return port type in string format
  189. */
  190. static const char *bcm_uart_type(struct uart_port *port)
  191. {
  192. return (port->type == PORT_BCM63XX) ? "bcm63xx_uart" : NULL;
  193. }
  194. /*
  195. * read all chars in rx fifo and send them to core
  196. */
  197. static void bcm_uart_do_rx(struct uart_port *port)
  198. {
  199. struct tty_port *tty_port = &port->state->port;
  200. unsigned int max_count;
  201. /* limit number of char read in interrupt, should not be
  202. * higher than fifo size anyway since we're much faster than
  203. * serial port */
  204. max_count = 32;
  205. do {
  206. unsigned int iestat, c, cstat;
  207. char flag;
  208. /* get overrun/fifo empty information from ier
  209. * register */
  210. iestat = bcm_uart_readl(port, UART_IR_REG);
  211. if (unlikely(iestat & UART_IR_STAT(UART_IR_RXOVER))) {
  212. unsigned int val;
  213. /* fifo reset is required to clear
  214. * interrupt */
  215. val = bcm_uart_readl(port, UART_CTL_REG);
  216. val |= UART_CTL_RSTRXFIFO_MASK;
  217. bcm_uart_writel(port, val, UART_CTL_REG);
  218. port->icount.overrun++;
  219. tty_insert_flip_char(tty_port, 0, TTY_OVERRUN);
  220. }
  221. if (!(iestat & UART_IR_STAT(UART_IR_RXNOTEMPTY)))
  222. break;
  223. cstat = c = bcm_uart_readl(port, UART_FIFO_REG);
  224. port->icount.rx++;
  225. flag = TTY_NORMAL;
  226. c &= 0xff;
  227. if (unlikely((cstat & UART_FIFO_ANYERR_MASK))) {
  228. /* do stats first */
  229. if (cstat & UART_FIFO_BRKDET_MASK) {
  230. port->icount.brk++;
  231. if (uart_handle_break(port))
  232. continue;
  233. }
  234. if (cstat & UART_FIFO_PARERR_MASK)
  235. port->icount.parity++;
  236. if (cstat & UART_FIFO_FRAMEERR_MASK)
  237. port->icount.frame++;
  238. /* update flag wrt read_status_mask */
  239. cstat &= port->read_status_mask;
  240. if (cstat & UART_FIFO_BRKDET_MASK)
  241. flag = TTY_BREAK;
  242. if (cstat & UART_FIFO_FRAMEERR_MASK)
  243. flag = TTY_FRAME;
  244. if (cstat & UART_FIFO_PARERR_MASK)
  245. flag = TTY_PARITY;
  246. }
  247. if (uart_handle_sysrq_char(port, c))
  248. continue;
  249. if ((cstat & port->ignore_status_mask) == 0)
  250. tty_insert_flip_char(tty_port, c, flag);
  251. } while (--max_count);
  252. spin_unlock(&port->lock);
  253. tty_flip_buffer_push(tty_port);
  254. spin_lock(&port->lock);
  255. }
  256. /*
  257. * fill tx fifo with chars to send, stop when fifo is about to be full
  258. * or when all chars have been sent.
  259. */
  260. static void bcm_uart_do_tx(struct uart_port *port)
  261. {
  262. struct circ_buf *xmit;
  263. unsigned int val, max_count;
  264. if (port->x_char) {
  265. bcm_uart_writel(port, port->x_char, UART_FIFO_REG);
  266. port->icount.tx++;
  267. port->x_char = 0;
  268. return;
  269. }
  270. if (uart_tx_stopped(port)) {
  271. bcm_uart_stop_tx(port);
  272. return;
  273. }
  274. xmit = &port->state->xmit;
  275. if (uart_circ_empty(xmit))
  276. goto txq_empty;
  277. val = bcm_uart_readl(port, UART_MCTL_REG);
  278. val = (val & UART_MCTL_TXFIFOFILL_MASK) >> UART_MCTL_TXFIFOFILL_SHIFT;
  279. max_count = port->fifosize - val;
  280. while (max_count--) {
  281. unsigned int c;
  282. c = xmit->buf[xmit->tail];
  283. bcm_uart_writel(port, c, UART_FIFO_REG);
  284. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  285. port->icount.tx++;
  286. if (uart_circ_empty(xmit))
  287. break;
  288. }
  289. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  290. uart_write_wakeup(port);
  291. if (uart_circ_empty(xmit))
  292. goto txq_empty;
  293. return;
  294. txq_empty:
  295. /* nothing to send, disable transmit interrupt */
  296. val = bcm_uart_readl(port, UART_IR_REG);
  297. val &= ~UART_TX_INT_MASK;
  298. bcm_uart_writel(port, val, UART_IR_REG);
  299. return;
  300. }
  301. /*
  302. * process uart interrupt
  303. */
  304. static irqreturn_t bcm_uart_interrupt(int irq, void *dev_id)
  305. {
  306. struct uart_port *port;
  307. unsigned int irqstat;
  308. port = dev_id;
  309. spin_lock(&port->lock);
  310. irqstat = bcm_uart_readl(port, UART_IR_REG);
  311. if (irqstat & UART_RX_INT_STAT)
  312. bcm_uart_do_rx(port);
  313. if (irqstat & UART_TX_INT_STAT)
  314. bcm_uart_do_tx(port);
  315. if (irqstat & UART_IR_MASK(UART_IR_EXTIP)) {
  316. unsigned int estat;
  317. estat = bcm_uart_readl(port, UART_EXTINP_REG);
  318. if (estat & UART_EXTINP_IRSTAT(UART_EXTINP_IR_CTS))
  319. uart_handle_cts_change(port,
  320. estat & UART_EXTINP_CTS_MASK);
  321. if (estat & UART_EXTINP_IRSTAT(UART_EXTINP_IR_DCD))
  322. uart_handle_dcd_change(port,
  323. estat & UART_EXTINP_DCD_MASK);
  324. }
  325. spin_unlock(&port->lock);
  326. return IRQ_HANDLED;
  327. }
  328. /*
  329. * enable rx & tx operation on uart
  330. */
  331. static void bcm_uart_enable(struct uart_port *port)
  332. {
  333. unsigned int val;
  334. val = bcm_uart_readl(port, UART_CTL_REG);
  335. val |= (UART_CTL_BRGEN_MASK | UART_CTL_TXEN_MASK | UART_CTL_RXEN_MASK);
  336. bcm_uart_writel(port, val, UART_CTL_REG);
  337. }
  338. /*
  339. * disable rx & tx operation on uart
  340. */
  341. static void bcm_uart_disable(struct uart_port *port)
  342. {
  343. unsigned int val;
  344. val = bcm_uart_readl(port, UART_CTL_REG);
  345. val &= ~(UART_CTL_BRGEN_MASK | UART_CTL_TXEN_MASK |
  346. UART_CTL_RXEN_MASK);
  347. bcm_uart_writel(port, val, UART_CTL_REG);
  348. }
  349. /*
  350. * clear all unread data in rx fifo and unsent data in tx fifo
  351. */
  352. static void bcm_uart_flush(struct uart_port *port)
  353. {
  354. unsigned int val;
  355. /* empty rx and tx fifo */
  356. val = bcm_uart_readl(port, UART_CTL_REG);
  357. val |= UART_CTL_RSTRXFIFO_MASK | UART_CTL_RSTTXFIFO_MASK;
  358. bcm_uart_writel(port, val, UART_CTL_REG);
  359. /* read any pending char to make sure all irq status are
  360. * cleared */
  361. (void)bcm_uart_readl(port, UART_FIFO_REG);
  362. }
  363. /*
  364. * serial core request to initialize uart and start rx operation
  365. */
  366. static int bcm_uart_startup(struct uart_port *port)
  367. {
  368. unsigned int val;
  369. int ret;
  370. /* mask all irq and flush port */
  371. bcm_uart_disable(port);
  372. bcm_uart_writel(port, 0, UART_IR_REG);
  373. bcm_uart_flush(port);
  374. /* clear any pending external input interrupt */
  375. (void)bcm_uart_readl(port, UART_EXTINP_REG);
  376. /* set rx/tx fifo thresh to fifo half size */
  377. val = bcm_uart_readl(port, UART_MCTL_REG);
  378. val &= ~(UART_MCTL_RXFIFOTHRESH_MASK | UART_MCTL_TXFIFOTHRESH_MASK);
  379. val |= (port->fifosize / 2) << UART_MCTL_RXFIFOTHRESH_SHIFT;
  380. val |= (port->fifosize / 2) << UART_MCTL_TXFIFOTHRESH_SHIFT;
  381. bcm_uart_writel(port, val, UART_MCTL_REG);
  382. /* set rx fifo timeout to 1 char time */
  383. val = bcm_uart_readl(port, UART_CTL_REG);
  384. val &= ~UART_CTL_RXTMOUTCNT_MASK;
  385. val |= 1 << UART_CTL_RXTMOUTCNT_SHIFT;
  386. bcm_uart_writel(port, val, UART_CTL_REG);
  387. /* report any edge on dcd and cts */
  388. val = UART_EXTINP_INT_MASK;
  389. val |= UART_EXTINP_DCD_NOSENSE_MASK;
  390. val |= UART_EXTINP_CTS_NOSENSE_MASK;
  391. bcm_uart_writel(port, val, UART_EXTINP_REG);
  392. /* register irq and enable rx interrupts */
  393. ret = request_irq(port->irq, bcm_uart_interrupt, 0,
  394. dev_name(port->dev), port);
  395. if (ret)
  396. return ret;
  397. bcm_uart_writel(port, UART_RX_INT_MASK, UART_IR_REG);
  398. bcm_uart_enable(port);
  399. return 0;
  400. }
  401. /*
  402. * serial core request to flush & disable uart
  403. */
  404. static void bcm_uart_shutdown(struct uart_port *port)
  405. {
  406. unsigned long flags;
  407. spin_lock_irqsave(&port->lock, flags);
  408. bcm_uart_writel(port, 0, UART_IR_REG);
  409. spin_unlock_irqrestore(&port->lock, flags);
  410. bcm_uart_disable(port);
  411. bcm_uart_flush(port);
  412. free_irq(port->irq, port);
  413. }
  414. /*
  415. * serial core request to change current uart setting
  416. */
  417. static void bcm_uart_set_termios(struct uart_port *port,
  418. struct ktermios *new,
  419. struct ktermios *old)
  420. {
  421. unsigned int ctl, baud, quot, ier;
  422. unsigned long flags;
  423. int tries;
  424. spin_lock_irqsave(&port->lock, flags);
  425. /* Drain the hot tub fully before we power it off for the winter. */
  426. for (tries = 3; !bcm_uart_tx_empty(port) && tries; tries--)
  427. mdelay(10);
  428. /* disable uart while changing speed */
  429. bcm_uart_disable(port);
  430. bcm_uart_flush(port);
  431. /* update Control register */
  432. ctl = bcm_uart_readl(port, UART_CTL_REG);
  433. ctl &= ~UART_CTL_BITSPERSYM_MASK;
  434. switch (new->c_cflag & CSIZE) {
  435. case CS5:
  436. ctl |= (0 << UART_CTL_BITSPERSYM_SHIFT);
  437. break;
  438. case CS6:
  439. ctl |= (1 << UART_CTL_BITSPERSYM_SHIFT);
  440. break;
  441. case CS7:
  442. ctl |= (2 << UART_CTL_BITSPERSYM_SHIFT);
  443. break;
  444. default:
  445. ctl |= (3 << UART_CTL_BITSPERSYM_SHIFT);
  446. break;
  447. }
  448. ctl &= ~UART_CTL_STOPBITS_MASK;
  449. if (new->c_cflag & CSTOPB)
  450. ctl |= UART_CTL_STOPBITS_2;
  451. else
  452. ctl |= UART_CTL_STOPBITS_1;
  453. ctl &= ~(UART_CTL_RXPAREN_MASK | UART_CTL_TXPAREN_MASK);
  454. if (new->c_cflag & PARENB)
  455. ctl |= (UART_CTL_RXPAREN_MASK | UART_CTL_TXPAREN_MASK);
  456. ctl &= ~(UART_CTL_RXPAREVEN_MASK | UART_CTL_TXPAREVEN_MASK);
  457. if (new->c_cflag & PARODD)
  458. ctl |= (UART_CTL_RXPAREVEN_MASK | UART_CTL_TXPAREVEN_MASK);
  459. bcm_uart_writel(port, ctl, UART_CTL_REG);
  460. /* update Baudword register */
  461. baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
  462. quot = uart_get_divisor(port, baud) - 1;
  463. bcm_uart_writel(port, quot, UART_BAUD_REG);
  464. /* update Interrupt register */
  465. ier = bcm_uart_readl(port, UART_IR_REG);
  466. ier &= ~UART_IR_MASK(UART_IR_EXTIP);
  467. if (UART_ENABLE_MS(port, new->c_cflag))
  468. ier |= UART_IR_MASK(UART_IR_EXTIP);
  469. bcm_uart_writel(port, ier, UART_IR_REG);
  470. /* update read/ignore mask */
  471. port->read_status_mask = UART_FIFO_VALID_MASK;
  472. if (new->c_iflag & INPCK) {
  473. port->read_status_mask |= UART_FIFO_FRAMEERR_MASK;
  474. port->read_status_mask |= UART_FIFO_PARERR_MASK;
  475. }
  476. if (new->c_iflag & (IGNBRK | BRKINT))
  477. port->read_status_mask |= UART_FIFO_BRKDET_MASK;
  478. port->ignore_status_mask = 0;
  479. if (new->c_iflag & IGNPAR)
  480. port->ignore_status_mask |= UART_FIFO_PARERR_MASK;
  481. if (new->c_iflag & IGNBRK)
  482. port->ignore_status_mask |= UART_FIFO_BRKDET_MASK;
  483. if (!(new->c_cflag & CREAD))
  484. port->ignore_status_mask |= UART_FIFO_VALID_MASK;
  485. uart_update_timeout(port, new->c_cflag, baud);
  486. bcm_uart_enable(port);
  487. spin_unlock_irqrestore(&port->lock, flags);
  488. }
  489. /*
  490. * serial core request to claim uart iomem
  491. */
  492. static int bcm_uart_request_port(struct uart_port *port)
  493. {
  494. /* UARTs always present */
  495. return 0;
  496. }
  497. /*
  498. * serial core request to release uart iomem
  499. */
  500. static void bcm_uart_release_port(struct uart_port *port)
  501. {
  502. /* Nothing to release ... */
  503. }
  504. /*
  505. * serial core request to do any port required autoconfiguration
  506. */
  507. static void bcm_uart_config_port(struct uart_port *port, int flags)
  508. {
  509. if (flags & UART_CONFIG_TYPE) {
  510. if (bcm_uart_request_port(port))
  511. return;
  512. port->type = PORT_BCM63XX;
  513. }
  514. }
  515. /*
  516. * serial core request to check that port information in serinfo are
  517. * suitable
  518. */
  519. static int bcm_uart_verify_port(struct uart_port *port,
  520. struct serial_struct *serinfo)
  521. {
  522. if (port->type != PORT_BCM63XX)
  523. return -EINVAL;
  524. if (port->irq != serinfo->irq)
  525. return -EINVAL;
  526. if (port->iotype != serinfo->io_type)
  527. return -EINVAL;
  528. if (port->mapbase != (unsigned long)serinfo->iomem_base)
  529. return -EINVAL;
  530. return 0;
  531. }
  532. /* serial core callbacks */
  533. static const struct uart_ops bcm_uart_ops = {
  534. .tx_empty = bcm_uart_tx_empty,
  535. .get_mctrl = bcm_uart_get_mctrl,
  536. .set_mctrl = bcm_uart_set_mctrl,
  537. .start_tx = bcm_uart_start_tx,
  538. .stop_tx = bcm_uart_stop_tx,
  539. .stop_rx = bcm_uart_stop_rx,
  540. .enable_ms = bcm_uart_enable_ms,
  541. .break_ctl = bcm_uart_break_ctl,
  542. .startup = bcm_uart_startup,
  543. .shutdown = bcm_uart_shutdown,
  544. .set_termios = bcm_uart_set_termios,
  545. .type = bcm_uart_type,
  546. .release_port = bcm_uart_release_port,
  547. .request_port = bcm_uart_request_port,
  548. .config_port = bcm_uart_config_port,
  549. .verify_port = bcm_uart_verify_port,
  550. };
  551. #ifdef CONFIG_SERIAL_BCM63XX_CONSOLE
  552. static void wait_for_xmitr(struct uart_port *port)
  553. {
  554. unsigned int tmout;
  555. /* Wait up to 10ms for the character(s) to be sent. */
  556. tmout = 10000;
  557. while (--tmout) {
  558. unsigned int val;
  559. val = bcm_uart_readl(port, UART_IR_REG);
  560. if (val & UART_IR_STAT(UART_IR_TXEMPTY))
  561. break;
  562. udelay(1);
  563. }
  564. /* Wait up to 1s for flow control if necessary */
  565. if (port->flags & UPF_CONS_FLOW) {
  566. tmout = 1000000;
  567. while (--tmout) {
  568. unsigned int val;
  569. val = bcm_uart_readl(port, UART_EXTINP_REG);
  570. if (val & UART_EXTINP_CTS_MASK)
  571. break;
  572. udelay(1);
  573. }
  574. }
  575. }
  576. /*
  577. * output given char
  578. */
  579. static void bcm_console_putchar(struct uart_port *port, int ch)
  580. {
  581. wait_for_xmitr(port);
  582. bcm_uart_writel(port, ch, UART_FIFO_REG);
  583. }
  584. /*
  585. * console core request to output given string
  586. */
  587. static void bcm_console_write(struct console *co, const char *s,
  588. unsigned int count)
  589. {
  590. struct uart_port *port;
  591. unsigned long flags;
  592. int locked;
  593. port = &ports[co->index];
  594. local_irq_save(flags);
  595. if (port->sysrq) {
  596. /* bcm_uart_interrupt() already took the lock */
  597. locked = 0;
  598. } else if (oops_in_progress) {
  599. locked = spin_trylock(&port->lock);
  600. } else {
  601. spin_lock(&port->lock);
  602. locked = 1;
  603. }
  604. /* call helper to deal with \r\n */
  605. uart_console_write(port, s, count, bcm_console_putchar);
  606. /* and wait for char to be transmitted */
  607. wait_for_xmitr(port);
  608. if (locked)
  609. spin_unlock(&port->lock);
  610. local_irq_restore(flags);
  611. }
  612. /*
  613. * console core request to setup given console, find matching uart
  614. * port and setup it.
  615. */
  616. static int bcm_console_setup(struct console *co, char *options)
  617. {
  618. struct uart_port *port;
  619. int baud = 9600;
  620. int bits = 8;
  621. int parity = 'n';
  622. int flow = 'n';
  623. if (co->index < 0 || co->index >= BCM63XX_NR_UARTS)
  624. return -EINVAL;
  625. port = &ports[co->index];
  626. if (!port->membase)
  627. return -ENODEV;
  628. if (options)
  629. uart_parse_options(options, &baud, &parity, &bits, &flow);
  630. return uart_set_options(port, co, baud, parity, bits, flow);
  631. }
  632. static struct uart_driver bcm_uart_driver;
  633. static struct console bcm63xx_console = {
  634. .name = "ttyS",
  635. .write = bcm_console_write,
  636. .device = uart_console_device,
  637. .setup = bcm_console_setup,
  638. .flags = CON_PRINTBUFFER,
  639. .index = -1,
  640. .data = &bcm_uart_driver,
  641. };
  642. static int __init bcm63xx_console_init(void)
  643. {
  644. register_console(&bcm63xx_console);
  645. return 0;
  646. }
  647. console_initcall(bcm63xx_console_init);
  648. static void bcm_early_write(struct console *con, const char *s, unsigned n)
  649. {
  650. struct earlycon_device *dev = con->data;
  651. uart_console_write(&dev->port, s, n, bcm_console_putchar);
  652. wait_for_xmitr(&dev->port);
  653. }
  654. static int __init bcm_early_console_setup(struct earlycon_device *device,
  655. const char *opt)
  656. {
  657. if (!device->port.membase)
  658. return -ENODEV;
  659. device->con->write = bcm_early_write;
  660. return 0;
  661. }
  662. OF_EARLYCON_DECLARE(bcm63xx_uart, "brcm,bcm6345-uart", bcm_early_console_setup);
  663. #define BCM63XX_CONSOLE (&bcm63xx_console)
  664. #else
  665. #define BCM63XX_CONSOLE NULL
  666. #endif /* CONFIG_SERIAL_BCM63XX_CONSOLE */
  667. static struct uart_driver bcm_uart_driver = {
  668. .owner = THIS_MODULE,
  669. .driver_name = "bcm63xx_uart",
  670. .dev_name = "ttyS",
  671. .major = TTY_MAJOR,
  672. .minor = 64,
  673. .nr = BCM63XX_NR_UARTS,
  674. .cons = BCM63XX_CONSOLE,
  675. };
  676. /*
  677. * platform driver probe/remove callback
  678. */
  679. static int bcm_uart_probe(struct platform_device *pdev)
  680. {
  681. struct resource *res_mem, *res_irq;
  682. struct uart_port *port;
  683. struct clk *clk;
  684. int ret;
  685. if (pdev->dev.of_node) {
  686. pdev->id = of_alias_get_id(pdev->dev.of_node, "serial");
  687. if (pdev->id < 0)
  688. pdev->id = of_alias_get_id(pdev->dev.of_node, "uart");
  689. }
  690. if (pdev->id < 0 || pdev->id >= BCM63XX_NR_UARTS)
  691. return -EINVAL;
  692. port = &ports[pdev->id];
  693. if (port->membase)
  694. return -EBUSY;
  695. memset(port, 0, sizeof(*port));
  696. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  697. if (!res_mem)
  698. return -ENODEV;
  699. port->mapbase = res_mem->start;
  700. port->membase = devm_ioremap_resource(&pdev->dev, res_mem);
  701. if (IS_ERR(port->membase))
  702. return PTR_ERR(port->membase);
  703. res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  704. if (!res_irq)
  705. return -ENODEV;
  706. clk = clk_get(&pdev->dev, "refclk");
  707. if (IS_ERR(clk) && pdev->dev.of_node)
  708. clk = of_clk_get(pdev->dev.of_node, 0);
  709. if (IS_ERR(clk))
  710. return -ENODEV;
  711. port->iotype = UPIO_MEM;
  712. port->irq = res_irq->start;
  713. port->ops = &bcm_uart_ops;
  714. port->flags = UPF_BOOT_AUTOCONF;
  715. port->dev = &pdev->dev;
  716. port->fifosize = 16;
  717. port->uartclk = clk_get_rate(clk) / 2;
  718. port->line = pdev->id;
  719. clk_put(clk);
  720. ret = uart_add_one_port(&bcm_uart_driver, port);
  721. if (ret) {
  722. ports[pdev->id].membase = NULL;
  723. return ret;
  724. }
  725. platform_set_drvdata(pdev, port);
  726. return 0;
  727. }
  728. static int bcm_uart_remove(struct platform_device *pdev)
  729. {
  730. struct uart_port *port;
  731. port = platform_get_drvdata(pdev);
  732. uart_remove_one_port(&bcm_uart_driver, port);
  733. /* mark port as free */
  734. ports[pdev->id].membase = NULL;
  735. return 0;
  736. }
  737. static const struct of_device_id bcm63xx_of_match[] = {
  738. { .compatible = "brcm,bcm6345-uart" },
  739. { /* sentinel */ }
  740. };
  741. MODULE_DEVICE_TABLE(of, bcm63xx_of_match);
  742. /*
  743. * platform driver stuff
  744. */
  745. static struct platform_driver bcm_uart_platform_driver = {
  746. .probe = bcm_uart_probe,
  747. .remove = bcm_uart_remove,
  748. .driver = {
  749. .name = "bcm63xx_uart",
  750. .of_match_table = bcm63xx_of_match,
  751. },
  752. };
  753. static int __init bcm_uart_init(void)
  754. {
  755. int ret;
  756. ret = uart_register_driver(&bcm_uart_driver);
  757. if (ret)
  758. return ret;
  759. ret = platform_driver_register(&bcm_uart_platform_driver);
  760. if (ret)
  761. uart_unregister_driver(&bcm_uart_driver);
  762. return ret;
  763. }
  764. static void __exit bcm_uart_exit(void)
  765. {
  766. platform_driver_unregister(&bcm_uart_platform_driver);
  767. uart_unregister_driver(&bcm_uart_driver);
  768. }
  769. module_init(bcm_uart_init);
  770. module_exit(bcm_uart_exit);
  771. MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
  772. MODULE_DESCRIPTION("Broadcom 63xx integrated uart driver");
  773. MODULE_LICENSE("GPL");