bcm63xx_uart.c 21 KB

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