sb1250-duart.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /*
  2. * drivers/serial/sb1250-duart.c
  3. *
  4. * Support for the asynchronous serial interface (DUART) included
  5. * in the BCM1250 and derived System-On-a-Chip (SOC) devices.
  6. *
  7. * Copyright (c) 2007 Maciej W. Rozycki
  8. *
  9. * Derived from drivers/char/sb1250_duart.c for which the following
  10. * copyright applies:
  11. *
  12. * Copyright (c) 2000, 2001, 2002, 2003, 2004 Broadcom Corporation
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. * References:
  20. *
  21. * "BCM1250/BCM1125/BCM1125H User Manual", Broadcom Corporation
  22. */
  23. #if defined(CONFIG_SERIAL_SB1250_DUART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  24. #define SUPPORT_SYSRQ
  25. #endif
  26. #include <linux/console.h>
  27. #include <linux/delay.h>
  28. #include <linux/errno.h>
  29. #include <linux/init.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/ioport.h>
  32. #include <linux/kernel.h>
  33. #include <linux/major.h>
  34. #include <linux/serial.h>
  35. #include <linux/serial_core.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/sysrq.h>
  38. #include <linux/tty.h>
  39. #include <linux/types.h>
  40. #include <asm/atomic.h>
  41. #include <asm/io.h>
  42. #include <asm/war.h>
  43. #include <asm/sibyte/sb1250.h>
  44. #include <asm/sibyte/sb1250_uart.h>
  45. #include <asm/sibyte/swarm.h>
  46. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  47. #include <asm/sibyte/bcm1480_regs.h>
  48. #include <asm/sibyte/bcm1480_int.h>
  49. #define SBD_CHANREGS(line) A_BCM1480_DUART_CHANREG((line), 0)
  50. #define SBD_CTRLREGS(line) A_BCM1480_DUART_CTRLREG((line), 0)
  51. #define SBD_INT(line) (K_BCM1480_INT_UART_0 + (line))
  52. #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
  53. #include <asm/sibyte/sb1250_regs.h>
  54. #include <asm/sibyte/sb1250_int.h>
  55. #define SBD_CHANREGS(line) A_DUART_CHANREG((line), 0)
  56. #define SBD_CTRLREGS(line) A_DUART_CTRLREG(0)
  57. #define SBD_INT(line) (K_INT_UART_0 + (line))
  58. #else
  59. #error invalid SB1250 UART configuration
  60. #endif
  61. MODULE_AUTHOR("Maciej W. Rozycki <macro@linux-mips.org>");
  62. MODULE_DESCRIPTION("BCM1xxx on-chip DUART serial driver");
  63. MODULE_LICENSE("GPL");
  64. #define DUART_MAX_CHIP 2
  65. #define DUART_MAX_SIDE 2
  66. /*
  67. * Per-port state.
  68. */
  69. struct sbd_port {
  70. struct sbd_duart *duart;
  71. struct uart_port port;
  72. unsigned char __iomem *memctrl;
  73. int tx_stopped;
  74. int initialised;
  75. };
  76. /*
  77. * Per-DUART state for the shared register space.
  78. */
  79. struct sbd_duart {
  80. struct sbd_port sport[2];
  81. unsigned long mapctrl;
  82. atomic_t map_guard;
  83. };
  84. #define to_sport(uport) container_of(uport, struct sbd_port, port)
  85. static struct sbd_duart sbd_duarts[DUART_MAX_CHIP];
  86. #define __unused __attribute__((__unused__))
  87. /*
  88. * Reading and writing SB1250 DUART registers.
  89. *
  90. * There are three register spaces: two per-channel ones and
  91. * a shared one. We have to define accessors appropriately.
  92. * All registers are 64-bit and all but the Baud Rate Clock
  93. * registers only define 8 least significant bits. There is
  94. * also a workaround to take into account. Raw accessors use
  95. * the full register width, but cooked ones truncate it
  96. * intentionally so that the rest of the driver does not care.
  97. */
  98. static u64 __read_sbdchn(struct sbd_port *sport, int reg)
  99. {
  100. void __iomem *csr = sport->port.membase + reg;
  101. return __raw_readq(csr);
  102. }
  103. static u64 __read_sbdshr(struct sbd_port *sport, int reg)
  104. {
  105. void __iomem *csr = sport->memctrl + reg;
  106. return __raw_readq(csr);
  107. }
  108. static void __write_sbdchn(struct sbd_port *sport, int reg, u64 value)
  109. {
  110. void __iomem *csr = sport->port.membase + reg;
  111. __raw_writeq(value, csr);
  112. }
  113. static void __write_sbdshr(struct sbd_port *sport, int reg, u64 value)
  114. {
  115. void __iomem *csr = sport->memctrl + reg;
  116. __raw_writeq(value, csr);
  117. }
  118. /*
  119. * In bug 1956, we get glitches that can mess up uart registers. This
  120. * "read-mode-reg after any register access" is an accepted workaround.
  121. */
  122. static void __war_sbd1956(struct sbd_port *sport)
  123. {
  124. __read_sbdchn(sport, R_DUART_MODE_REG_1);
  125. __read_sbdchn(sport, R_DUART_MODE_REG_2);
  126. }
  127. static unsigned char read_sbdchn(struct sbd_port *sport, int reg)
  128. {
  129. unsigned char retval;
  130. retval = __read_sbdchn(sport, reg);
  131. if (SIBYTE_1956_WAR)
  132. __war_sbd1956(sport);
  133. return retval;
  134. }
  135. static unsigned char read_sbdshr(struct sbd_port *sport, int reg)
  136. {
  137. unsigned char retval;
  138. retval = __read_sbdshr(sport, reg);
  139. if (SIBYTE_1956_WAR)
  140. __war_sbd1956(sport);
  141. return retval;
  142. }
  143. static void write_sbdchn(struct sbd_port *sport, int reg, unsigned int value)
  144. {
  145. __write_sbdchn(sport, reg, value);
  146. if (SIBYTE_1956_WAR)
  147. __war_sbd1956(sport);
  148. }
  149. static void write_sbdshr(struct sbd_port *sport, int reg, unsigned int value)
  150. {
  151. __write_sbdshr(sport, reg, value);
  152. if (SIBYTE_1956_WAR)
  153. __war_sbd1956(sport);
  154. }
  155. static int sbd_receive_ready(struct sbd_port *sport)
  156. {
  157. return read_sbdchn(sport, R_DUART_STATUS) & M_DUART_RX_RDY;
  158. }
  159. static int sbd_receive_drain(struct sbd_port *sport)
  160. {
  161. int loops = 10000;
  162. while (sbd_receive_ready(sport) && loops--)
  163. read_sbdchn(sport, R_DUART_RX_HOLD);
  164. return loops;
  165. }
  166. static int __unused sbd_transmit_ready(struct sbd_port *sport)
  167. {
  168. return read_sbdchn(sport, R_DUART_STATUS) & M_DUART_TX_RDY;
  169. }
  170. static int __unused sbd_transmit_drain(struct sbd_port *sport)
  171. {
  172. int loops = 10000;
  173. while (!sbd_transmit_ready(sport) && loops--)
  174. udelay(2);
  175. return loops;
  176. }
  177. static int sbd_transmit_empty(struct sbd_port *sport)
  178. {
  179. return read_sbdchn(sport, R_DUART_STATUS) & M_DUART_TX_EMT;
  180. }
  181. static int sbd_line_drain(struct sbd_port *sport)
  182. {
  183. int loops = 10000;
  184. while (!sbd_transmit_empty(sport) && loops--)
  185. udelay(2);
  186. return loops;
  187. }
  188. static unsigned int sbd_tx_empty(struct uart_port *uport)
  189. {
  190. struct sbd_port *sport = to_sport(uport);
  191. return sbd_transmit_empty(sport) ? TIOCSER_TEMT : 0;
  192. }
  193. static unsigned int sbd_get_mctrl(struct uart_port *uport)
  194. {
  195. struct sbd_port *sport = to_sport(uport);
  196. unsigned int mctrl, status;
  197. status = read_sbdshr(sport, R_DUART_IN_PORT);
  198. status >>= (uport->line) % 2;
  199. mctrl = (!(status & M_DUART_IN_PIN0_VAL) ? TIOCM_CTS : 0) |
  200. (!(status & M_DUART_IN_PIN4_VAL) ? TIOCM_CAR : 0) |
  201. (!(status & M_DUART_RIN0_PIN) ? TIOCM_RNG : 0) |
  202. (!(status & M_DUART_IN_PIN2_VAL) ? TIOCM_DSR : 0);
  203. return mctrl;
  204. }
  205. static void sbd_set_mctrl(struct uart_port *uport, unsigned int mctrl)
  206. {
  207. struct sbd_port *sport = to_sport(uport);
  208. unsigned int clr = 0, set = 0, mode2;
  209. if (mctrl & TIOCM_DTR)
  210. set |= M_DUART_SET_OPR2;
  211. else
  212. clr |= M_DUART_CLR_OPR2;
  213. if (mctrl & TIOCM_RTS)
  214. set |= M_DUART_SET_OPR0;
  215. else
  216. clr |= M_DUART_CLR_OPR0;
  217. clr <<= (uport->line) % 2;
  218. set <<= (uport->line) % 2;
  219. mode2 = read_sbdchn(sport, R_DUART_MODE_REG_2);
  220. mode2 &= ~M_DUART_CHAN_MODE;
  221. if (mctrl & TIOCM_LOOP)
  222. mode2 |= V_DUART_CHAN_MODE_LCL_LOOP;
  223. else
  224. mode2 |= V_DUART_CHAN_MODE_NORMAL;
  225. write_sbdshr(sport, R_DUART_CLEAR_OPR, clr);
  226. write_sbdshr(sport, R_DUART_SET_OPR, set);
  227. write_sbdchn(sport, R_DUART_MODE_REG_2, mode2);
  228. }
  229. static void sbd_stop_tx(struct uart_port *uport)
  230. {
  231. struct sbd_port *sport = to_sport(uport);
  232. write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS);
  233. sport->tx_stopped = 1;
  234. };
  235. static void sbd_start_tx(struct uart_port *uport)
  236. {
  237. struct sbd_port *sport = to_sport(uport);
  238. unsigned int mask;
  239. /* Enable tx interrupts. */
  240. mask = read_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2));
  241. mask |= M_DUART_IMR_TX;
  242. write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), mask);
  243. /* Go!, go!, go!... */
  244. write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_EN);
  245. sport->tx_stopped = 0;
  246. };
  247. static void sbd_stop_rx(struct uart_port *uport)
  248. {
  249. struct sbd_port *sport = to_sport(uport);
  250. write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), 0);
  251. };
  252. static void sbd_enable_ms(struct uart_port *uport)
  253. {
  254. struct sbd_port *sport = to_sport(uport);
  255. write_sbdchn(sport, R_DUART_AUXCTL_X,
  256. M_DUART_CIN_CHNG_ENA | M_DUART_CTS_CHNG_ENA);
  257. }
  258. static void sbd_break_ctl(struct uart_port *uport, int break_state)
  259. {
  260. struct sbd_port *sport = to_sport(uport);
  261. if (break_state == -1)
  262. write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_START_BREAK);
  263. else
  264. write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_STOP_BREAK);
  265. }
  266. static void sbd_receive_chars(struct sbd_port *sport)
  267. {
  268. struct uart_port *uport = &sport->port;
  269. struct uart_icount *icount;
  270. unsigned int status, ch, flag;
  271. int count;
  272. for (count = 16; count; count--) {
  273. status = read_sbdchn(sport, R_DUART_STATUS);
  274. if (!(status & M_DUART_RX_RDY))
  275. break;
  276. ch = read_sbdchn(sport, R_DUART_RX_HOLD);
  277. flag = TTY_NORMAL;
  278. icount = &uport->icount;
  279. icount->rx++;
  280. if (unlikely(status &
  281. (M_DUART_RCVD_BRK | M_DUART_FRM_ERR |
  282. M_DUART_PARITY_ERR | M_DUART_OVRUN_ERR))) {
  283. if (status & M_DUART_RCVD_BRK) {
  284. icount->brk++;
  285. if (uart_handle_break(uport))
  286. continue;
  287. } else if (status & M_DUART_FRM_ERR)
  288. icount->frame++;
  289. else if (status & M_DUART_PARITY_ERR)
  290. icount->parity++;
  291. if (status & M_DUART_OVRUN_ERR)
  292. icount->overrun++;
  293. status &= uport->read_status_mask;
  294. if (status & M_DUART_RCVD_BRK)
  295. flag = TTY_BREAK;
  296. else if (status & M_DUART_FRM_ERR)
  297. flag = TTY_FRAME;
  298. else if (status & M_DUART_PARITY_ERR)
  299. flag = TTY_PARITY;
  300. }
  301. if (uart_handle_sysrq_char(uport, ch))
  302. continue;
  303. uart_insert_char(uport, status, M_DUART_OVRUN_ERR, ch, flag);
  304. }
  305. tty_flip_buffer_push(uport->info->tty);
  306. }
  307. static void sbd_transmit_chars(struct sbd_port *sport)
  308. {
  309. struct uart_port *uport = &sport->port;
  310. struct circ_buf *xmit = &sport->port.info->xmit;
  311. unsigned int mask;
  312. int stop_tx;
  313. /* XON/XOFF chars. */
  314. if (sport->port.x_char) {
  315. write_sbdchn(sport, R_DUART_TX_HOLD, sport->port.x_char);
  316. sport->port.icount.tx++;
  317. sport->port.x_char = 0;
  318. return;
  319. }
  320. /* If nothing to do or stopped or hardware stopped. */
  321. stop_tx = (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port));
  322. /* Send char. */
  323. if (!stop_tx) {
  324. write_sbdchn(sport, R_DUART_TX_HOLD, xmit->buf[xmit->tail]);
  325. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  326. sport->port.icount.tx++;
  327. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  328. uart_write_wakeup(&sport->port);
  329. }
  330. /* Are we are done? */
  331. if (stop_tx || uart_circ_empty(xmit)) {
  332. /* Disable tx interrupts. */
  333. mask = read_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2));
  334. mask &= ~M_DUART_IMR_TX;
  335. write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), mask);
  336. }
  337. }
  338. static void sbd_status_handle(struct sbd_port *sport)
  339. {
  340. struct uart_port *uport = &sport->port;
  341. unsigned int delta;
  342. delta = read_sbdshr(sport, R_DUART_INCHREG((uport->line) % 2));
  343. delta >>= (uport->line) % 2;
  344. if (delta & (M_DUART_IN_PIN0_VAL << S_DUART_IN_PIN_CHNG))
  345. uart_handle_cts_change(uport, !(delta & M_DUART_IN_PIN0_VAL));
  346. if (delta & (M_DUART_IN_PIN2_VAL << S_DUART_IN_PIN_CHNG))
  347. uport->icount.dsr++;
  348. if (delta & ((M_DUART_IN_PIN2_VAL | M_DUART_IN_PIN0_VAL) <<
  349. S_DUART_IN_PIN_CHNG))
  350. wake_up_interruptible(&uport->info->delta_msr_wait);
  351. }
  352. static irqreturn_t sbd_interrupt(int irq, void *dev_id)
  353. {
  354. struct sbd_port *sport = dev_id;
  355. struct uart_port *uport = &sport->port;
  356. irqreturn_t status = IRQ_NONE;
  357. unsigned int intstat;
  358. int count;
  359. for (count = 16; count; count--) {
  360. intstat = read_sbdshr(sport,
  361. R_DUART_ISRREG((uport->line) % 2));
  362. intstat &= read_sbdshr(sport,
  363. R_DUART_IMRREG((uport->line) % 2));
  364. intstat &= M_DUART_ISR_ALL;
  365. if (!intstat)
  366. break;
  367. if (intstat & M_DUART_ISR_RX)
  368. sbd_receive_chars(sport);
  369. if (intstat & M_DUART_ISR_IN)
  370. sbd_status_handle(sport);
  371. if (intstat & M_DUART_ISR_TX)
  372. sbd_transmit_chars(sport);
  373. status = IRQ_HANDLED;
  374. }
  375. return status;
  376. }
  377. static int sbd_startup(struct uart_port *uport)
  378. {
  379. struct sbd_port *sport = to_sport(uport);
  380. unsigned int mode1;
  381. int ret;
  382. ret = request_irq(sport->port.irq, sbd_interrupt,
  383. IRQF_SHARED, "sb1250-duart", sport);
  384. if (ret)
  385. return ret;
  386. /* Clear the receive FIFO. */
  387. sbd_receive_drain(sport);
  388. /* Clear the interrupt registers. */
  389. write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_RESET_BREAK_INT);
  390. read_sbdshr(sport, R_DUART_INCHREG((uport->line) % 2));
  391. /* Set rx/tx interrupt to FIFO available. */
  392. mode1 = read_sbdchn(sport, R_DUART_MODE_REG_1);
  393. mode1 &= ~(M_DUART_RX_IRQ_SEL_RXFULL | M_DUART_TX_IRQ_SEL_TXEMPT);
  394. write_sbdchn(sport, R_DUART_MODE_REG_1, mode1);
  395. /* Disable tx, enable rx. */
  396. write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS | M_DUART_RX_EN);
  397. sport->tx_stopped = 1;
  398. /* Enable interrupts. */
  399. write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2),
  400. M_DUART_IMR_IN | M_DUART_IMR_RX);
  401. return 0;
  402. }
  403. static void sbd_shutdown(struct uart_port *uport)
  404. {
  405. struct sbd_port *sport = to_sport(uport);
  406. write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS | M_DUART_RX_DIS);
  407. sport->tx_stopped = 1;
  408. free_irq(sport->port.irq, sport);
  409. }
  410. static void sbd_init_port(struct sbd_port *sport)
  411. {
  412. struct uart_port *uport = &sport->port;
  413. if (sport->initialised)
  414. return;
  415. /* There is no DUART reset feature, so just set some sane defaults. */
  416. write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_RESET_TX);
  417. write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_RESET_RX);
  418. write_sbdchn(sport, R_DUART_MODE_REG_1, V_DUART_BITS_PER_CHAR_8);
  419. write_sbdchn(sport, R_DUART_MODE_REG_2, 0);
  420. write_sbdchn(sport, R_DUART_FULL_CTL,
  421. V_DUART_INT_TIME(0) | V_DUART_SIG_FULL(15));
  422. write_sbdchn(sport, R_DUART_OPCR_X, 0);
  423. write_sbdchn(sport, R_DUART_AUXCTL_X, 0);
  424. write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), 0);
  425. sport->initialised = 1;
  426. }
  427. static void sbd_set_termios(struct uart_port *uport, struct ktermios *termios,
  428. struct ktermios *old_termios)
  429. {
  430. struct sbd_port *sport = to_sport(uport);
  431. unsigned int mode1 = 0, mode2 = 0, aux = 0;
  432. unsigned int mode1mask = 0, mode2mask = 0, auxmask = 0;
  433. unsigned int oldmode1, oldmode2, oldaux;
  434. unsigned int baud, brg;
  435. unsigned int command;
  436. mode1mask |= ~(M_DUART_PARITY_MODE | M_DUART_PARITY_TYPE_ODD |
  437. M_DUART_BITS_PER_CHAR);
  438. mode2mask |= ~M_DUART_STOP_BIT_LEN_2;
  439. auxmask |= ~M_DUART_CTS_CHNG_ENA;
  440. /* Byte size. */
  441. switch (termios->c_cflag & CSIZE) {
  442. case CS5:
  443. case CS6:
  444. /* Unsupported, leave unchanged. */
  445. mode1mask |= M_DUART_PARITY_MODE;
  446. break;
  447. case CS7:
  448. mode1 |= V_DUART_BITS_PER_CHAR_7;
  449. break;
  450. case CS8:
  451. default:
  452. mode1 |= V_DUART_BITS_PER_CHAR_8;
  453. break;
  454. }
  455. /* Parity and stop bits. */
  456. if (termios->c_cflag & CSTOPB)
  457. mode2 |= M_DUART_STOP_BIT_LEN_2;
  458. else
  459. mode2 |= M_DUART_STOP_BIT_LEN_1;
  460. if (termios->c_cflag & PARENB)
  461. mode1 |= V_DUART_PARITY_MODE_ADD;
  462. else
  463. mode1 |= V_DUART_PARITY_MODE_NONE;
  464. if (termios->c_cflag & PARODD)
  465. mode1 |= M_DUART_PARITY_TYPE_ODD;
  466. else
  467. mode1 |= M_DUART_PARITY_TYPE_EVEN;
  468. baud = uart_get_baud_rate(uport, termios, old_termios, 1200, 5000000);
  469. brg = V_DUART_BAUD_RATE(baud);
  470. /* The actual lower bound is 1221bps, so compensate. */
  471. if (brg > M_DUART_CLK_COUNTER)
  472. brg = M_DUART_CLK_COUNTER;
  473. uart_update_timeout(uport, termios->c_cflag, baud);
  474. uport->read_status_mask = M_DUART_OVRUN_ERR;
  475. if (termios->c_iflag & INPCK)
  476. uport->read_status_mask |= M_DUART_FRM_ERR |
  477. M_DUART_PARITY_ERR;
  478. if (termios->c_iflag & (BRKINT | PARMRK))
  479. uport->read_status_mask |= M_DUART_RCVD_BRK;
  480. uport->ignore_status_mask = 0;
  481. if (termios->c_iflag & IGNPAR)
  482. uport->ignore_status_mask |= M_DUART_FRM_ERR |
  483. M_DUART_PARITY_ERR;
  484. if (termios->c_iflag & IGNBRK) {
  485. uport->ignore_status_mask |= M_DUART_RCVD_BRK;
  486. if (termios->c_iflag & IGNPAR)
  487. uport->ignore_status_mask |= M_DUART_OVRUN_ERR;
  488. }
  489. if (termios->c_cflag & CREAD)
  490. command = M_DUART_RX_EN;
  491. else
  492. command = M_DUART_RX_DIS;
  493. if (termios->c_cflag & CRTSCTS)
  494. aux |= M_DUART_CTS_CHNG_ENA;
  495. else
  496. aux &= ~M_DUART_CTS_CHNG_ENA;
  497. spin_lock(&uport->lock);
  498. if (sport->tx_stopped)
  499. command |= M_DUART_TX_DIS;
  500. else
  501. command |= M_DUART_TX_EN;
  502. oldmode1 = read_sbdchn(sport, R_DUART_MODE_REG_1) & mode1mask;
  503. oldmode2 = read_sbdchn(sport, R_DUART_MODE_REG_2) & mode2mask;
  504. oldaux = read_sbdchn(sport, R_DUART_AUXCTL_X) & auxmask;
  505. if (!sport->tx_stopped)
  506. sbd_line_drain(sport);
  507. write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS | M_DUART_RX_DIS);
  508. write_sbdchn(sport, R_DUART_MODE_REG_1, mode1 | oldmode1);
  509. write_sbdchn(sport, R_DUART_MODE_REG_2, mode2 | oldmode2);
  510. write_sbdchn(sport, R_DUART_CLK_SEL, brg);
  511. write_sbdchn(sport, R_DUART_AUXCTL_X, aux | oldaux);
  512. write_sbdchn(sport, R_DUART_CMD, command);
  513. spin_unlock(&uport->lock);
  514. }
  515. static const char *sbd_type(struct uart_port *uport)
  516. {
  517. return "SB1250 DUART";
  518. }
  519. static void sbd_release_port(struct uart_port *uport)
  520. {
  521. struct sbd_port *sport = to_sport(uport);
  522. struct sbd_duart *duart = sport->duart;
  523. int map_guard;
  524. iounmap(sport->memctrl);
  525. sport->memctrl = NULL;
  526. iounmap(uport->membase);
  527. uport->membase = NULL;
  528. map_guard = atomic_add_return(-1, &duart->map_guard);
  529. if (!map_guard)
  530. release_mem_region(duart->mapctrl, DUART_CHANREG_SPACING);
  531. release_mem_region(uport->mapbase, DUART_CHANREG_SPACING);
  532. }
  533. static int sbd_map_port(struct uart_port *uport)
  534. {
  535. static const char *err = KERN_ERR "sbd: Cannot map MMIO\n";
  536. struct sbd_port *sport = to_sport(uport);
  537. struct sbd_duart *duart = sport->duart;
  538. if (!uport->membase)
  539. uport->membase = ioremap_nocache(uport->mapbase,
  540. DUART_CHANREG_SPACING);
  541. if (!uport->membase) {
  542. printk(err);
  543. return -ENOMEM;
  544. }
  545. if (!sport->memctrl)
  546. sport->memctrl = ioremap_nocache(duart->mapctrl,
  547. DUART_CHANREG_SPACING);
  548. if (!sport->memctrl) {
  549. printk(err);
  550. iounmap(uport->membase);
  551. uport->membase = NULL;
  552. return -ENOMEM;
  553. }
  554. return 0;
  555. }
  556. static int sbd_request_port(struct uart_port *uport)
  557. {
  558. static const char *err = KERN_ERR
  559. "sbd: Unable to reserve MMIO resource\n";
  560. struct sbd_duart *duart = to_sport(uport)->duart;
  561. int map_guard;
  562. int ret = 0;
  563. if (!request_mem_region(uport->mapbase, DUART_CHANREG_SPACING,
  564. "sb1250-duart")) {
  565. printk(err);
  566. return -EBUSY;
  567. }
  568. map_guard = atomic_add_return(1, &duart->map_guard);
  569. if (map_guard == 1) {
  570. if (!request_mem_region(duart->mapctrl, DUART_CHANREG_SPACING,
  571. "sb1250-duart")) {
  572. atomic_add(-1, &duart->map_guard);
  573. printk(err);
  574. ret = -EBUSY;
  575. }
  576. }
  577. if (!ret) {
  578. ret = sbd_map_port(uport);
  579. if (ret) {
  580. map_guard = atomic_add_return(-1, &duart->map_guard);
  581. if (!map_guard)
  582. release_mem_region(duart->mapctrl,
  583. DUART_CHANREG_SPACING);
  584. }
  585. }
  586. if (ret) {
  587. release_mem_region(uport->mapbase, DUART_CHANREG_SPACING);
  588. return ret;
  589. }
  590. return 0;
  591. }
  592. static void sbd_config_port(struct uart_port *uport, int flags)
  593. {
  594. struct sbd_port *sport = to_sport(uport);
  595. if (flags & UART_CONFIG_TYPE) {
  596. if (sbd_request_port(uport))
  597. return;
  598. uport->type = PORT_SB1250_DUART;
  599. sbd_init_port(sport);
  600. }
  601. }
  602. static int sbd_verify_port(struct uart_port *uport, struct serial_struct *ser)
  603. {
  604. int ret = 0;
  605. if (ser->type != PORT_UNKNOWN && ser->type != PORT_SB1250_DUART)
  606. ret = -EINVAL;
  607. if (ser->irq != uport->irq)
  608. ret = -EINVAL;
  609. if (ser->baud_base != uport->uartclk / 16)
  610. ret = -EINVAL;
  611. return ret;
  612. }
  613. static struct uart_ops sbd_ops = {
  614. .tx_empty = sbd_tx_empty,
  615. .set_mctrl = sbd_set_mctrl,
  616. .get_mctrl = sbd_get_mctrl,
  617. .stop_tx = sbd_stop_tx,
  618. .start_tx = sbd_start_tx,
  619. .stop_rx = sbd_stop_rx,
  620. .enable_ms = sbd_enable_ms,
  621. .break_ctl = sbd_break_ctl,
  622. .startup = sbd_startup,
  623. .shutdown = sbd_shutdown,
  624. .set_termios = sbd_set_termios,
  625. .type = sbd_type,
  626. .release_port = sbd_release_port,
  627. .request_port = sbd_request_port,
  628. .config_port = sbd_config_port,
  629. .verify_port = sbd_verify_port,
  630. };
  631. /* Initialize SB1250 DUART port structures. */
  632. static void __init sbd_probe_duarts(void)
  633. {
  634. static int probed;
  635. int chip, side;
  636. int max_lines, line;
  637. if (probed)
  638. return;
  639. /* Set the number of available units based on the SOC type. */
  640. switch (soc_type) {
  641. case K_SYS_SOC_TYPE_BCM1x55:
  642. case K_SYS_SOC_TYPE_BCM1x80:
  643. max_lines = 4;
  644. break;
  645. default:
  646. /* Assume at least two serial ports at the normal address. */
  647. max_lines = 2;
  648. break;
  649. }
  650. probed = 1;
  651. for (chip = 0, line = 0; chip < DUART_MAX_CHIP && line < max_lines;
  652. chip++) {
  653. sbd_duarts[chip].mapctrl = SBD_CTRLREGS(line);
  654. for (side = 0; side < DUART_MAX_SIDE && line < max_lines;
  655. side++, line++) {
  656. struct sbd_port *sport = &sbd_duarts[chip].sport[side];
  657. struct uart_port *uport = &sport->port;
  658. sport->duart = &sbd_duarts[chip];
  659. uport->irq = SBD_INT(line);
  660. uport->uartclk = 100000000 / 20 * 16;
  661. uport->fifosize = 16;
  662. uport->iotype = UPIO_MEM;
  663. uport->flags = UPF_BOOT_AUTOCONF;
  664. uport->ops = &sbd_ops;
  665. uport->line = line;
  666. uport->mapbase = SBD_CHANREGS(line);
  667. }
  668. }
  669. }
  670. #ifdef CONFIG_SERIAL_SB1250_DUART_CONSOLE
  671. /*
  672. * Serial console stuff. Very basic, polling driver for doing serial
  673. * console output. The console_sem is held by the caller, so we
  674. * shouldn't be interrupted for more console activity.
  675. */
  676. static void sbd_console_putchar(struct uart_port *uport, int ch)
  677. {
  678. struct sbd_port *sport = to_sport(uport);
  679. sbd_transmit_drain(sport);
  680. write_sbdchn(sport, R_DUART_TX_HOLD, ch);
  681. }
  682. static void sbd_console_write(struct console *co, const char *s,
  683. unsigned int count)
  684. {
  685. int chip = co->index / DUART_MAX_SIDE;
  686. int side = co->index % DUART_MAX_SIDE;
  687. struct sbd_port *sport = &sbd_duarts[chip].sport[side];
  688. struct uart_port *uport = &sport->port;
  689. unsigned long flags;
  690. unsigned int mask;
  691. /* Disable transmit interrupts and enable the transmitter. */
  692. spin_lock_irqsave(&uport->lock, flags);
  693. mask = read_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2));
  694. write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2),
  695. mask & ~M_DUART_IMR_TX);
  696. write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_EN);
  697. spin_unlock_irqrestore(&uport->lock, flags);
  698. uart_console_write(&sport->port, s, count, sbd_console_putchar);
  699. /* Restore transmit interrupts and the transmitter enable. */
  700. spin_lock_irqsave(&uport->lock, flags);
  701. sbd_line_drain(sport);
  702. if (sport->tx_stopped)
  703. write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS);
  704. write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), mask);
  705. spin_unlock_irqrestore(&uport->lock, flags);
  706. }
  707. static int __init sbd_console_setup(struct console *co, char *options)
  708. {
  709. int chip = co->index / DUART_MAX_SIDE;
  710. int side = co->index % DUART_MAX_SIDE;
  711. struct sbd_port *sport = &sbd_duarts[chip].sport[side];
  712. struct uart_port *uport = &sport->port;
  713. int baud = 115200;
  714. int bits = 8;
  715. int parity = 'n';
  716. int flow = 'n';
  717. int ret;
  718. if (!sport->duart)
  719. return -ENXIO;
  720. ret = sbd_map_port(uport);
  721. if (ret)
  722. return ret;
  723. sbd_init_port(sport);
  724. if (options)
  725. uart_parse_options(options, &baud, &parity, &bits, &flow);
  726. return uart_set_options(uport, co, baud, parity, bits, flow);
  727. }
  728. static struct uart_driver sbd_reg;
  729. static struct console sbd_console = {
  730. .name = "duart",
  731. .write = sbd_console_write,
  732. .device = uart_console_device,
  733. .setup = sbd_console_setup,
  734. .flags = CON_PRINTBUFFER,
  735. .index = -1,
  736. .data = &sbd_reg
  737. };
  738. static int __init sbd_serial_console_init(void)
  739. {
  740. sbd_probe_duarts();
  741. register_console(&sbd_console);
  742. return 0;
  743. }
  744. console_initcall(sbd_serial_console_init);
  745. #define SERIAL_SB1250_DUART_CONSOLE &sbd_console
  746. #else
  747. #define SERIAL_SB1250_DUART_CONSOLE NULL
  748. #endif /* CONFIG_SERIAL_SB1250_DUART_CONSOLE */
  749. static struct uart_driver sbd_reg = {
  750. .owner = THIS_MODULE,
  751. .driver_name = "serial",
  752. .dev_name = "duart",
  753. .major = TTY_MAJOR,
  754. .minor = SB1250_DUART_MINOR_BASE,
  755. .nr = DUART_MAX_CHIP * DUART_MAX_SIDE,
  756. .cons = SERIAL_SB1250_DUART_CONSOLE,
  757. };
  758. /* Set up the driver and register it. */
  759. static int __init sbd_init(void)
  760. {
  761. int i, ret;
  762. sbd_probe_duarts();
  763. ret = uart_register_driver(&sbd_reg);
  764. if (ret)
  765. return ret;
  766. for (i = 0; i < DUART_MAX_CHIP * DUART_MAX_SIDE; i++) {
  767. struct sbd_duart *duart = &sbd_duarts[i / DUART_MAX_SIDE];
  768. struct sbd_port *sport = &duart->sport[i % DUART_MAX_SIDE];
  769. struct uart_port *uport = &sport->port;
  770. if (sport->duart)
  771. uart_add_one_port(&sbd_reg, uport);
  772. }
  773. return 0;
  774. }
  775. /* Unload the driver. Unregister stuff, get ready to go away. */
  776. static void __exit sbd_exit(void)
  777. {
  778. int i;
  779. for (i = DUART_MAX_CHIP * DUART_MAX_SIDE - 1; i >= 0; i--) {
  780. struct sbd_duart *duart = &sbd_duarts[i / DUART_MAX_SIDE];
  781. struct sbd_port *sport = &duart->sport[i % DUART_MAX_SIDE];
  782. struct uart_port *uport = &sport->port;
  783. if (sport->duart)
  784. uart_remove_one_port(&sbd_reg, uport);
  785. }
  786. uart_unregister_driver(&sbd_reg);
  787. }
  788. module_init(sbd_init);
  789. module_exit(sbd_exit);