bcm63xx_uart.c 21 KB

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