bcm63xx_uart.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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_struct *tty;
  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. tty = port->state->port.tty;
  210. do {
  211. unsigned int iestat, c, cstat;
  212. char flag;
  213. /* get overrun/fifo empty information from ier
  214. * register */
  215. iestat = bcm_uart_readl(port, UART_IR_REG);
  216. if (unlikely(iestat & UART_IR_STAT(UART_IR_RXOVER))) {
  217. unsigned int val;
  218. /* fifo reset is required to clear
  219. * interrupt */
  220. val = bcm_uart_readl(port, UART_CTL_REG);
  221. val |= UART_CTL_RSTRXFIFO_MASK;
  222. bcm_uart_writel(port, val, UART_CTL_REG);
  223. port->icount.overrun++;
  224. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  225. }
  226. if (!(iestat & UART_IR_STAT(UART_IR_RXNOTEMPTY)))
  227. break;
  228. cstat = c = bcm_uart_readl(port, UART_FIFO_REG);
  229. port->icount.rx++;
  230. flag = TTY_NORMAL;
  231. c &= 0xff;
  232. if (unlikely((cstat & UART_FIFO_ANYERR_MASK))) {
  233. /* do stats first */
  234. if (cstat & UART_FIFO_BRKDET_MASK) {
  235. port->icount.brk++;
  236. if (uart_handle_break(port))
  237. continue;
  238. }
  239. if (cstat & UART_FIFO_PARERR_MASK)
  240. port->icount.parity++;
  241. if (cstat & UART_FIFO_FRAMEERR_MASK)
  242. port->icount.frame++;
  243. /* update flag wrt read_status_mask */
  244. cstat &= port->read_status_mask;
  245. if (cstat & UART_FIFO_BRKDET_MASK)
  246. flag = TTY_BREAK;
  247. if (cstat & UART_FIFO_FRAMEERR_MASK)
  248. flag = TTY_FRAME;
  249. if (cstat & UART_FIFO_PARERR_MASK)
  250. flag = TTY_PARITY;
  251. }
  252. if (uart_handle_sysrq_char(port, c))
  253. continue;
  254. if ((cstat & port->ignore_status_mask) == 0)
  255. tty_insert_flip_char(tty, c, flag);
  256. } while (--max_count);
  257. tty_flip_buffer_push(tty);
  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. bcm_uart_type(port), 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 & (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. unsigned int size;
  494. size = RSET_UART_SIZE;
  495. if (!request_mem_region(port->mapbase, size, "bcm63xx")) {
  496. dev_err(port->dev, "Memory region busy\n");
  497. return -EBUSY;
  498. }
  499. port->membase = ioremap(port->mapbase, size);
  500. if (!port->membase) {
  501. dev_err(port->dev, "Unable to map registers\n");
  502. release_mem_region(port->mapbase, size);
  503. return -EBUSY;
  504. }
  505. return 0;
  506. }
  507. /*
  508. * serial core request to release uart iomem
  509. */
  510. static void bcm_uart_release_port(struct uart_port *port)
  511. {
  512. release_mem_region(port->mapbase, RSET_UART_SIZE);
  513. iounmap(port->membase);
  514. }
  515. /*
  516. * serial core request to do any port required autoconfiguration
  517. */
  518. static void bcm_uart_config_port(struct uart_port *port, int flags)
  519. {
  520. if (flags & UART_CONFIG_TYPE) {
  521. if (bcm_uart_request_port(port))
  522. return;
  523. port->type = PORT_BCM63XX;
  524. }
  525. }
  526. /*
  527. * serial core request to check that port information in serinfo are
  528. * suitable
  529. */
  530. static int bcm_uart_verify_port(struct uart_port *port,
  531. struct serial_struct *serinfo)
  532. {
  533. if (port->type != PORT_BCM63XX)
  534. return -EINVAL;
  535. if (port->irq != serinfo->irq)
  536. return -EINVAL;
  537. if (port->iotype != serinfo->io_type)
  538. return -EINVAL;
  539. if (port->mapbase != (unsigned long)serinfo->iomem_base)
  540. return -EINVAL;
  541. return 0;
  542. }
  543. /* serial core callbacks */
  544. static struct uart_ops bcm_uart_ops = {
  545. .tx_empty = bcm_uart_tx_empty,
  546. .get_mctrl = bcm_uart_get_mctrl,
  547. .set_mctrl = bcm_uart_set_mctrl,
  548. .start_tx = bcm_uart_start_tx,
  549. .stop_tx = bcm_uart_stop_tx,
  550. .stop_rx = bcm_uart_stop_rx,
  551. .enable_ms = bcm_uart_enable_ms,
  552. .break_ctl = bcm_uart_break_ctl,
  553. .startup = bcm_uart_startup,
  554. .shutdown = bcm_uart_shutdown,
  555. .set_termios = bcm_uart_set_termios,
  556. .type = bcm_uart_type,
  557. .release_port = bcm_uart_release_port,
  558. .request_port = bcm_uart_request_port,
  559. .config_port = bcm_uart_config_port,
  560. .verify_port = bcm_uart_verify_port,
  561. };
  562. #ifdef CONFIG_SERIAL_BCM63XX_CONSOLE
  563. static inline void wait_for_xmitr(struct uart_port *port)
  564. {
  565. unsigned int tmout;
  566. /* Wait up to 10ms for the character(s) to be sent. */
  567. tmout = 10000;
  568. while (--tmout) {
  569. unsigned int val;
  570. val = bcm_uart_readl(port, UART_IR_REG);
  571. if (val & UART_IR_STAT(UART_IR_TXEMPTY))
  572. break;
  573. udelay(1);
  574. }
  575. /* Wait up to 1s for flow control if necessary */
  576. if (port->flags & UPF_CONS_FLOW) {
  577. tmout = 1000000;
  578. while (--tmout) {
  579. unsigned int val;
  580. val = bcm_uart_readl(port, UART_EXTINP_REG);
  581. if (val & UART_EXTINP_CTS_MASK)
  582. break;
  583. udelay(1);
  584. }
  585. }
  586. }
  587. /*
  588. * output given char
  589. */
  590. static void bcm_console_putchar(struct uart_port *port, int ch)
  591. {
  592. wait_for_xmitr(port);
  593. bcm_uart_writel(port, ch, UART_FIFO_REG);
  594. }
  595. /*
  596. * console core request to output given string
  597. */
  598. static void bcm_console_write(struct console *co, const char *s,
  599. unsigned int count)
  600. {
  601. struct uart_port *port;
  602. unsigned long flags;
  603. int locked;
  604. port = &ports[co->index];
  605. local_irq_save(flags);
  606. if (port->sysrq) {
  607. /* bcm_uart_interrupt() already took the lock */
  608. locked = 0;
  609. } else if (oops_in_progress) {
  610. locked = spin_trylock(&port->lock);
  611. } else {
  612. spin_lock(&port->lock);
  613. locked = 1;
  614. }
  615. /* call helper to deal with \r\n */
  616. uart_console_write(port, s, count, bcm_console_putchar);
  617. /* and wait for char to be transmitted */
  618. wait_for_xmitr(port);
  619. if (locked)
  620. spin_unlock(&port->lock);
  621. local_irq_restore(flags);
  622. }
  623. /*
  624. * console core request to setup given console, find matching uart
  625. * port and setup it.
  626. */
  627. static int bcm_console_setup(struct console *co, char *options)
  628. {
  629. struct uart_port *port;
  630. int baud = 9600;
  631. int bits = 8;
  632. int parity = 'n';
  633. int flow = 'n';
  634. if (co->index < 0 || co->index >= BCM63XX_NR_UARTS)
  635. return -EINVAL;
  636. port = &ports[co->index];
  637. if (!port->membase)
  638. return -ENODEV;
  639. if (options)
  640. uart_parse_options(options, &baud, &parity, &bits, &flow);
  641. return uart_set_options(port, co, baud, parity, bits, flow);
  642. }
  643. static struct uart_driver bcm_uart_driver;
  644. static struct console bcm63xx_console = {
  645. .name = "ttyS",
  646. .write = bcm_console_write,
  647. .device = uart_console_device,
  648. .setup = bcm_console_setup,
  649. .flags = CON_PRINTBUFFER,
  650. .index = -1,
  651. .data = &bcm_uart_driver,
  652. };
  653. static int __init bcm63xx_console_init(void)
  654. {
  655. register_console(&bcm63xx_console);
  656. return 0;
  657. }
  658. console_initcall(bcm63xx_console_init);
  659. #define BCM63XX_CONSOLE (&bcm63xx_console)
  660. #else
  661. #define BCM63XX_CONSOLE NULL
  662. #endif /* CONFIG_SERIAL_BCM63XX_CONSOLE */
  663. static struct uart_driver bcm_uart_driver = {
  664. .owner = THIS_MODULE,
  665. .driver_name = "bcm63xx_uart",
  666. .dev_name = "ttyS",
  667. .major = TTY_MAJOR,
  668. .minor = 64,
  669. .nr = BCM63XX_NR_UARTS,
  670. .cons = BCM63XX_CONSOLE,
  671. };
  672. /*
  673. * platform driver probe/remove callback
  674. */
  675. static int __devinit bcm_uart_probe(struct platform_device *pdev)
  676. {
  677. struct resource *res_mem, *res_irq;
  678. struct uart_port *port;
  679. struct clk *clk;
  680. int ret;
  681. if (pdev->id < 0 || pdev->id >= BCM63XX_NR_UARTS)
  682. return -EINVAL;
  683. if (ports[pdev->id].membase)
  684. return -EBUSY;
  685. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  686. if (!res_mem)
  687. return -ENODEV;
  688. res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  689. if (!res_irq)
  690. return -ENODEV;
  691. clk = clk_get(&pdev->dev, "periph");
  692. if (IS_ERR(clk))
  693. return -ENODEV;
  694. port = &ports[pdev->id];
  695. memset(port, 0, sizeof(*port));
  696. port->iotype = UPIO_MEM;
  697. port->mapbase = res_mem->start;
  698. port->irq = res_irq->start;
  699. port->ops = &bcm_uart_ops;
  700. port->flags = UPF_BOOT_AUTOCONF;
  701. port->dev = &pdev->dev;
  702. port->fifosize = 16;
  703. port->uartclk = clk_get_rate(clk) / 2;
  704. port->line = pdev->id;
  705. clk_put(clk);
  706. ret = uart_add_one_port(&bcm_uart_driver, port);
  707. if (ret) {
  708. ports[pdev->id].membase = 0;
  709. return ret;
  710. }
  711. platform_set_drvdata(pdev, port);
  712. return 0;
  713. }
  714. static int __devexit bcm_uart_remove(struct platform_device *pdev)
  715. {
  716. struct uart_port *port;
  717. port = platform_get_drvdata(pdev);
  718. uart_remove_one_port(&bcm_uart_driver, port);
  719. platform_set_drvdata(pdev, NULL);
  720. /* mark port as free */
  721. ports[pdev->id].membase = 0;
  722. return 0;
  723. }
  724. /*
  725. * platform driver stuff
  726. */
  727. static struct platform_driver bcm_uart_platform_driver = {
  728. .probe = bcm_uart_probe,
  729. .remove = __devexit_p(bcm_uart_remove),
  730. .driver = {
  731. .owner = THIS_MODULE,
  732. .name = "bcm63xx_uart",
  733. },
  734. };
  735. static int __init bcm_uart_init(void)
  736. {
  737. int ret;
  738. ret = uart_register_driver(&bcm_uart_driver);
  739. if (ret)
  740. return ret;
  741. ret = platform_driver_register(&bcm_uart_platform_driver);
  742. if (ret)
  743. uart_unregister_driver(&bcm_uart_driver);
  744. return ret;
  745. }
  746. static void __exit bcm_uart_exit(void)
  747. {
  748. platform_driver_unregister(&bcm_uart_platform_driver);
  749. uart_unregister_driver(&bcm_uart_driver);
  750. }
  751. module_init(bcm_uart_init);
  752. module_exit(bcm_uart_exit);
  753. MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
  754. MODULE_DESCRIPTION("Broadcom 63<xx integrated uart driver");
  755. MODULE_LICENSE("GPL");