bcm63xx_uart.c 21 KB

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