bcm63xx_uart.c 21 KB

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