bcm63xx_uart.c 20 KB

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