bcm63xx_uart.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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. #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. uart_port_lock_irqsave(port, &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. uart_port_unlock_irqrestore(port, 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_prepare_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. unsigned int val;
  258. bool pending;
  259. u8 ch;
  260. val = bcm_uart_readl(port, UART_MCTL_REG);
  261. val = (val & UART_MCTL_TXFIFOFILL_MASK) >> UART_MCTL_TXFIFOFILL_SHIFT;
  262. pending = uart_port_tx_limited_flags(port, ch, UART_TX_NOSTOP,
  263. port->fifosize - val,
  264. true,
  265. bcm_uart_writel(port, ch, UART_FIFO_REG),
  266. ({}));
  267. if (pending)
  268. return;
  269. /* nothing to send, disable transmit interrupt */
  270. val = bcm_uart_readl(port, UART_IR_REG);
  271. val &= ~UART_TX_INT_MASK;
  272. bcm_uart_writel(port, val, UART_IR_REG);
  273. if (uart_tx_stopped(port))
  274. bcm_uart_stop_tx(port);
  275. }
  276. /*
  277. * process uart interrupt
  278. */
  279. static irqreturn_t bcm_uart_interrupt(int irq, void *dev_id)
  280. {
  281. struct uart_port *port;
  282. unsigned int irqstat;
  283. port = dev_id;
  284. uart_port_lock(port);
  285. irqstat = bcm_uart_readl(port, UART_IR_REG);
  286. if (irqstat & UART_RX_INT_STAT)
  287. bcm_uart_do_rx(port);
  288. if (irqstat & UART_TX_INT_STAT)
  289. bcm_uart_do_tx(port);
  290. if (irqstat & UART_IR_MASK(UART_IR_EXTIP)) {
  291. unsigned int estat;
  292. estat = bcm_uart_readl(port, UART_EXTINP_REG);
  293. if (estat & UART_EXTINP_IRSTAT(UART_EXTINP_IR_CTS))
  294. uart_handle_cts_change(port,
  295. estat & UART_EXTINP_CTS_MASK);
  296. if (estat & UART_EXTINP_IRSTAT(UART_EXTINP_IR_DCD))
  297. uart_handle_dcd_change(port,
  298. estat & UART_EXTINP_DCD_MASK);
  299. }
  300. uart_unlock_and_check_sysrq(port);
  301. return IRQ_HANDLED;
  302. }
  303. /*
  304. * enable rx & tx operation on uart
  305. */
  306. static void bcm_uart_enable(struct uart_port *port)
  307. {
  308. unsigned int val;
  309. val = bcm_uart_readl(port, UART_CTL_REG);
  310. val |= (UART_CTL_BRGEN_MASK | UART_CTL_TXEN_MASK | UART_CTL_RXEN_MASK);
  311. bcm_uart_writel(port, val, UART_CTL_REG);
  312. }
  313. /*
  314. * disable rx & tx operation on uart
  315. */
  316. static void bcm_uart_disable(struct uart_port *port)
  317. {
  318. unsigned int val;
  319. val = bcm_uart_readl(port, UART_CTL_REG);
  320. val &= ~(UART_CTL_BRGEN_MASK | UART_CTL_TXEN_MASK |
  321. UART_CTL_RXEN_MASK);
  322. bcm_uart_writel(port, val, UART_CTL_REG);
  323. }
  324. /*
  325. * clear all unread data in rx fifo and unsent data in tx fifo
  326. */
  327. static void bcm_uart_flush(struct uart_port *port)
  328. {
  329. unsigned int val;
  330. /* empty rx and tx fifo */
  331. val = bcm_uart_readl(port, UART_CTL_REG);
  332. val |= UART_CTL_RSTRXFIFO_MASK | UART_CTL_RSTTXFIFO_MASK;
  333. bcm_uart_writel(port, val, UART_CTL_REG);
  334. /* read any pending char to make sure all irq status are
  335. * cleared */
  336. (void)bcm_uart_readl(port, UART_FIFO_REG);
  337. }
  338. /*
  339. * serial core request to initialize uart and start rx operation
  340. */
  341. static int bcm_uart_startup(struct uart_port *port)
  342. {
  343. unsigned int val;
  344. int ret;
  345. /* mask all irq and flush port */
  346. bcm_uart_disable(port);
  347. bcm_uart_writel(port, 0, UART_IR_REG);
  348. bcm_uart_flush(port);
  349. /* clear any pending external input interrupt */
  350. (void)bcm_uart_readl(port, UART_EXTINP_REG);
  351. /* set rx/tx fifo thresh to fifo half size */
  352. val = bcm_uart_readl(port, UART_MCTL_REG);
  353. val &= ~(UART_MCTL_RXFIFOTHRESH_MASK | UART_MCTL_TXFIFOTHRESH_MASK);
  354. val |= (port->fifosize / 2) << UART_MCTL_RXFIFOTHRESH_SHIFT;
  355. val |= (port->fifosize / 2) << UART_MCTL_TXFIFOTHRESH_SHIFT;
  356. bcm_uart_writel(port, val, UART_MCTL_REG);
  357. /* set rx fifo timeout to 1 char time */
  358. val = bcm_uart_readl(port, UART_CTL_REG);
  359. val &= ~UART_CTL_RXTMOUTCNT_MASK;
  360. val |= 1 << UART_CTL_RXTMOUTCNT_SHIFT;
  361. bcm_uart_writel(port, val, UART_CTL_REG);
  362. /* report any edge on dcd and cts */
  363. val = UART_EXTINP_INT_MASK;
  364. val |= UART_EXTINP_DCD_NOSENSE_MASK;
  365. val |= UART_EXTINP_CTS_NOSENSE_MASK;
  366. bcm_uart_writel(port, val, UART_EXTINP_REG);
  367. /* register irq and enable rx interrupts */
  368. ret = request_irq(port->irq, bcm_uart_interrupt, 0,
  369. dev_name(port->dev), port);
  370. if (ret)
  371. return ret;
  372. bcm_uart_writel(port, UART_RX_INT_MASK, UART_IR_REG);
  373. bcm_uart_enable(port);
  374. return 0;
  375. }
  376. /*
  377. * serial core request to flush & disable uart
  378. */
  379. static void bcm_uart_shutdown(struct uart_port *port)
  380. {
  381. unsigned long flags;
  382. uart_port_lock_irqsave(port, &flags);
  383. bcm_uart_writel(port, 0, UART_IR_REG);
  384. uart_port_unlock_irqrestore(port, flags);
  385. bcm_uart_disable(port);
  386. bcm_uart_flush(port);
  387. free_irq(port->irq, port);
  388. }
  389. /*
  390. * serial core request to change current uart setting
  391. */
  392. static void bcm_uart_set_termios(struct uart_port *port, struct ktermios *new,
  393. const struct ktermios *old)
  394. {
  395. unsigned int ctl, baud, quot, ier;
  396. unsigned long flags;
  397. int tries;
  398. uart_port_lock_irqsave(port, &flags);
  399. /* Drain the hot tub fully before we power it off for the winter. */
  400. for (tries = 3; !bcm_uart_tx_empty(port) && tries; tries--)
  401. mdelay(10);
  402. /* disable uart while changing speed */
  403. bcm_uart_disable(port);
  404. bcm_uart_flush(port);
  405. /* update Control register */
  406. ctl = bcm_uart_readl(port, UART_CTL_REG);
  407. ctl &= ~UART_CTL_BITSPERSYM_MASK;
  408. switch (new->c_cflag & CSIZE) {
  409. case CS5:
  410. ctl |= (0 << UART_CTL_BITSPERSYM_SHIFT);
  411. break;
  412. case CS6:
  413. ctl |= (1 << UART_CTL_BITSPERSYM_SHIFT);
  414. break;
  415. case CS7:
  416. ctl |= (2 << UART_CTL_BITSPERSYM_SHIFT);
  417. break;
  418. default:
  419. ctl |= (3 << UART_CTL_BITSPERSYM_SHIFT);
  420. break;
  421. }
  422. ctl &= ~UART_CTL_STOPBITS_MASK;
  423. if (new->c_cflag & CSTOPB)
  424. ctl |= UART_CTL_STOPBITS_2;
  425. else
  426. ctl |= UART_CTL_STOPBITS_1;
  427. ctl &= ~(UART_CTL_RXPAREN_MASK | UART_CTL_TXPAREN_MASK);
  428. if (new->c_cflag & PARENB)
  429. ctl |= (UART_CTL_RXPAREN_MASK | UART_CTL_TXPAREN_MASK);
  430. ctl &= ~(UART_CTL_RXPAREVEN_MASK | UART_CTL_TXPAREVEN_MASK);
  431. if (new->c_cflag & PARODD)
  432. ctl |= (UART_CTL_RXPAREVEN_MASK | UART_CTL_TXPAREVEN_MASK);
  433. bcm_uart_writel(port, ctl, UART_CTL_REG);
  434. /* update Baudword register */
  435. baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
  436. quot = uart_get_divisor(port, baud) - 1;
  437. bcm_uart_writel(port, quot, UART_BAUD_REG);
  438. /* update Interrupt register */
  439. ier = bcm_uart_readl(port, UART_IR_REG);
  440. ier &= ~UART_IR_MASK(UART_IR_EXTIP);
  441. if (UART_ENABLE_MS(port, new->c_cflag))
  442. ier |= UART_IR_MASK(UART_IR_EXTIP);
  443. bcm_uart_writel(port, ier, UART_IR_REG);
  444. /* update read/ignore mask */
  445. port->read_status_mask = UART_FIFO_VALID_MASK;
  446. if (new->c_iflag & INPCK) {
  447. port->read_status_mask |= UART_FIFO_FRAMEERR_MASK;
  448. port->read_status_mask |= UART_FIFO_PARERR_MASK;
  449. }
  450. if (new->c_iflag & (IGNBRK | BRKINT))
  451. port->read_status_mask |= UART_FIFO_BRKDET_MASK;
  452. port->ignore_status_mask = 0;
  453. if (new->c_iflag & IGNPAR)
  454. port->ignore_status_mask |= UART_FIFO_PARERR_MASK;
  455. if (new->c_iflag & IGNBRK)
  456. port->ignore_status_mask |= UART_FIFO_BRKDET_MASK;
  457. if (!(new->c_cflag & CREAD))
  458. port->ignore_status_mask |= UART_FIFO_VALID_MASK;
  459. uart_update_timeout(port, new->c_cflag, baud);
  460. bcm_uart_enable(port);
  461. uart_port_unlock_irqrestore(port, flags);
  462. }
  463. /*
  464. * serial core request to claim uart iomem
  465. */
  466. static int bcm_uart_request_port(struct uart_port *port)
  467. {
  468. /* UARTs always present */
  469. return 0;
  470. }
  471. /*
  472. * serial core request to release uart iomem
  473. */
  474. static void bcm_uart_release_port(struct uart_port *port)
  475. {
  476. /* Nothing to release ... */
  477. }
  478. /*
  479. * serial core request to do any port required autoconfiguration
  480. */
  481. static void bcm_uart_config_port(struct uart_port *port, int flags)
  482. {
  483. if (flags & UART_CONFIG_TYPE) {
  484. if (bcm_uart_request_port(port))
  485. return;
  486. port->type = PORT_BCM63XX;
  487. }
  488. }
  489. /*
  490. * serial core request to check that port information in serinfo are
  491. * suitable
  492. */
  493. static int bcm_uart_verify_port(struct uart_port *port,
  494. struct serial_struct *serinfo)
  495. {
  496. if (port->type != PORT_BCM63XX)
  497. return -EINVAL;
  498. if (port->irq != serinfo->irq)
  499. return -EINVAL;
  500. if (port->iotype != serinfo->io_type)
  501. return -EINVAL;
  502. if (port->mapbase != (unsigned long)serinfo->iomem_base)
  503. return -EINVAL;
  504. return 0;
  505. }
  506. #ifdef CONFIG_CONSOLE_POLL
  507. /*
  508. * return true when outstanding tx equals fifo size
  509. */
  510. static bool bcm_uart_tx_full(struct uart_port *port)
  511. {
  512. unsigned int val;
  513. val = bcm_uart_readl(port, UART_MCTL_REG);
  514. val = (val & UART_MCTL_TXFIFOFILL_MASK) >> UART_MCTL_TXFIFOFILL_SHIFT;
  515. return !(port->fifosize - val);
  516. }
  517. static int bcm_uart_poll_get_char(struct uart_port *port)
  518. {
  519. unsigned int iestat;
  520. iestat = bcm_uart_readl(port, UART_IR_REG);
  521. if (!(iestat & UART_IR_STAT(UART_IR_RXNOTEMPTY)))
  522. return NO_POLL_CHAR;
  523. return bcm_uart_readl(port, UART_FIFO_REG);
  524. }
  525. static void bcm_uart_poll_put_char(struct uart_port *port, unsigned char c)
  526. {
  527. while (bcm_uart_tx_full(port)) {
  528. cpu_relax();
  529. }
  530. bcm_uart_writel(port, c, UART_FIFO_REG);
  531. }
  532. #endif
  533. /* serial core callbacks */
  534. static const struct uart_ops bcm_uart_ops = {
  535. .tx_empty = bcm_uart_tx_empty,
  536. .get_mctrl = bcm_uart_get_mctrl,
  537. .set_mctrl = bcm_uart_set_mctrl,
  538. .start_tx = bcm_uart_start_tx,
  539. .stop_tx = bcm_uart_stop_tx,
  540. .stop_rx = bcm_uart_stop_rx,
  541. .enable_ms = bcm_uart_enable_ms,
  542. .break_ctl = bcm_uart_break_ctl,
  543. .startup = bcm_uart_startup,
  544. .shutdown = bcm_uart_shutdown,
  545. .set_termios = bcm_uart_set_termios,
  546. .type = bcm_uart_type,
  547. .release_port = bcm_uart_release_port,
  548. .request_port = bcm_uart_request_port,
  549. .config_port = bcm_uart_config_port,
  550. .verify_port = bcm_uart_verify_port,
  551. #ifdef CONFIG_CONSOLE_POLL
  552. .poll_get_char = bcm_uart_poll_get_char,
  553. .poll_put_char = bcm_uart_poll_put_char,
  554. #endif
  555. };
  556. #ifdef CONFIG_SERIAL_BCM63XX_CONSOLE
  557. static void wait_for_xmitr(struct uart_port *port)
  558. {
  559. unsigned int tmout;
  560. /* Wait up to 10ms for the character(s) to be sent. */
  561. tmout = 10000;
  562. while (--tmout) {
  563. unsigned int val;
  564. val = bcm_uart_readl(port, UART_IR_REG);
  565. if (val & UART_IR_STAT(UART_IR_TXEMPTY))
  566. break;
  567. udelay(1);
  568. }
  569. /* Wait up to 1s for flow control if necessary */
  570. if (port->flags & UPF_CONS_FLOW) {
  571. tmout = 1000000;
  572. while (--tmout) {
  573. unsigned int val;
  574. val = bcm_uart_readl(port, UART_EXTINP_REG);
  575. if (val & UART_EXTINP_CTS_MASK)
  576. break;
  577. udelay(1);
  578. }
  579. }
  580. }
  581. /*
  582. * output given char
  583. */
  584. static void bcm_console_putchar(struct uart_port *port, unsigned char ch)
  585. {
  586. wait_for_xmitr(port);
  587. bcm_uart_writel(port, ch, UART_FIFO_REG);
  588. }
  589. /*
  590. * console core request to output given string
  591. */
  592. static void bcm_console_write(struct console *co, const char *s,
  593. unsigned int count)
  594. {
  595. struct uart_port *port;
  596. unsigned long flags;
  597. int locked = 1;
  598. port = &ports[co->index];
  599. if (oops_in_progress)
  600. locked = uart_port_trylock_irqsave(port, &flags);
  601. else
  602. uart_port_lock_irqsave(port, &flags);
  603. /* call helper to deal with \r\n */
  604. uart_console_write(port, s, count, bcm_console_putchar);
  605. /* and wait for char to be transmitted */
  606. wait_for_xmitr(port);
  607. if (locked)
  608. uart_port_unlock_irqrestore(port, flags);
  609. }
  610. /*
  611. * console core request to setup given console, find matching uart
  612. * port and setup it.
  613. */
  614. static int bcm_console_setup(struct console *co, char *options)
  615. {
  616. struct uart_port *port;
  617. int baud = 9600;
  618. int bits = 8;
  619. int parity = 'n';
  620. int flow = 'n';
  621. if (co->index < 0 || co->index >= BCM63XX_NR_UARTS)
  622. return -EINVAL;
  623. port = &ports[co->index];
  624. if (!port->membase)
  625. return -ENODEV;
  626. if (options)
  627. uart_parse_options(options, &baud, &parity, &bits, &flow);
  628. return uart_set_options(port, co, baud, parity, bits, flow);
  629. }
  630. static struct uart_driver bcm_uart_driver;
  631. static struct console bcm63xx_console = {
  632. .name = "ttyS",
  633. .write = bcm_console_write,
  634. .device = uart_console_device,
  635. .setup = bcm_console_setup,
  636. .flags = CON_PRINTBUFFER,
  637. .index = -1,
  638. .data = &bcm_uart_driver,
  639. };
  640. static int __init bcm63xx_console_init(void)
  641. {
  642. register_console(&bcm63xx_console);
  643. return 0;
  644. }
  645. console_initcall(bcm63xx_console_init);
  646. static void bcm_early_write(struct console *con, const char *s, unsigned n)
  647. {
  648. struct earlycon_device *dev = con->data;
  649. uart_console_write(&dev->port, s, n, bcm_console_putchar);
  650. wait_for_xmitr(&dev->port);
  651. }
  652. static int __init bcm_early_console_setup(struct earlycon_device *device,
  653. const char *opt)
  654. {
  655. if (!device->port.membase)
  656. return -ENODEV;
  657. device->con->write = bcm_early_write;
  658. return 0;
  659. }
  660. OF_EARLYCON_DECLARE(bcm63xx_uart, "brcm,bcm6345-uart", bcm_early_console_setup);
  661. #define BCM63XX_CONSOLE (&bcm63xx_console)
  662. #else
  663. #define BCM63XX_CONSOLE NULL
  664. #endif /* CONFIG_SERIAL_BCM63XX_CONSOLE */
  665. static struct uart_driver bcm_uart_driver = {
  666. .owner = THIS_MODULE,
  667. .driver_name = "bcm63xx_uart",
  668. .dev_name = "ttyS",
  669. .major = TTY_MAJOR,
  670. .minor = 64,
  671. .nr = BCM63XX_NR_UARTS,
  672. .cons = BCM63XX_CONSOLE,
  673. };
  674. /*
  675. * platform driver probe/remove callback
  676. */
  677. static int bcm_uart_probe(struct platform_device *pdev)
  678. {
  679. struct resource *res_mem;
  680. struct uart_port *port;
  681. struct clk *clk;
  682. int ret;
  683. if (pdev->dev.of_node) {
  684. pdev->id = of_alias_get_id(pdev->dev.of_node, "serial");
  685. if (pdev->id < 0)
  686. pdev->id = of_alias_get_id(pdev->dev.of_node, "uart");
  687. }
  688. if (pdev->id < 0 || pdev->id >= BCM63XX_NR_UARTS)
  689. return -EINVAL;
  690. port = &ports[pdev->id];
  691. if (port->membase)
  692. return -EBUSY;
  693. memset(port, 0, sizeof(*port));
  694. port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res_mem);
  695. if (IS_ERR(port->membase))
  696. return PTR_ERR(port->membase);
  697. port->mapbase = res_mem->start;
  698. ret = platform_get_irq(pdev, 0);
  699. if (ret < 0)
  700. return ret;
  701. port->irq = ret;
  702. clk = clk_get(&pdev->dev, "refclk");
  703. if (IS_ERR(clk) && pdev->dev.of_node)
  704. clk = of_clk_get(pdev->dev.of_node, 0);
  705. if (IS_ERR(clk))
  706. return -ENODEV;
  707. port->iotype = UPIO_MEM;
  708. port->ops = &bcm_uart_ops;
  709. port->flags = UPF_BOOT_AUTOCONF;
  710. port->dev = &pdev->dev;
  711. port->fifosize = 16;
  712. port->uartclk = clk_get_rate(clk) / 2;
  713. port->line = pdev->id;
  714. port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_BCM63XX_CONSOLE);
  715. clk_put(clk);
  716. ret = uart_add_one_port(&bcm_uart_driver, port);
  717. if (ret) {
  718. ports[pdev->id].membase = NULL;
  719. return ret;
  720. }
  721. platform_set_drvdata(pdev, port);
  722. return 0;
  723. }
  724. static void bcm_uart_remove(struct platform_device *pdev)
  725. {
  726. struct uart_port *port;
  727. port = platform_get_drvdata(pdev);
  728. uart_remove_one_port(&bcm_uart_driver, port);
  729. /* mark port as free */
  730. ports[pdev->id].membase = NULL;
  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 <mbizon@freebox.fr>");
  767. MODULE_DESCRIPTION("Broadcom 63xx integrated uart driver");
  768. MODULE_LICENSE("GPL");