sb1250-duart.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Support for the asynchronous serial interface (DUART) included
  4. * in the BCM1250 and derived System-On-a-Chip (SOC) devices.
  5. *
  6. * Copyright (c) 2007 Maciej W. Rozycki
  7. *
  8. * Derived from drivers/char/sb1250_duart.c for which the following
  9. * copyright applies:
  10. *
  11. * Copyright (c) 2000, 2001, 2002, 2003, 2004 Broadcom Corporation
  12. *
  13. * References:
  14. *
  15. * "BCM1250/BCM1125/BCM1125H User Manual", Broadcom Corporation
  16. */
  17. #if defined(CONFIG_SERIAL_SB1250_DUART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  18. #define SUPPORT_SYSRQ
  19. #endif
  20. #include <linux/compiler.h>
  21. #include <linux/console.h>
  22. #include <linux/delay.h>
  23. #include <linux/errno.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/ioport.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/major.h>
  30. #include <linux/serial.h>
  31. #include <linux/serial_core.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/sysrq.h>
  34. #include <linux/tty.h>
  35. #include <linux/tty_flip.h>
  36. #include <linux/types.h>
  37. #include <linux/refcount.h>
  38. #include <asm/io.h>
  39. #include <asm/war.h>
  40. #include <asm/sibyte/sb1250.h>
  41. #include <asm/sibyte/sb1250_uart.h>
  42. #include <asm/sibyte/swarm.h>
  43. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  44. #include <asm/sibyte/bcm1480_regs.h>
  45. #include <asm/sibyte/bcm1480_int.h>
  46. #define SBD_CHANREGS(line) A_BCM1480_DUART_CHANREG((line), 0)
  47. #define SBD_CTRLREGS(line) A_BCM1480_DUART_CTRLREG((line), 0)
  48. #define SBD_INT(line) (K_BCM1480_INT_UART_0 + (line))
  49. #define DUART_CHANREG_SPACING BCM1480_DUART_CHANREG_SPACING
  50. #define R_DUART_IMRREG(line) R_BCM1480_DUART_IMRREG(line)
  51. #define R_DUART_INCHREG(line) R_BCM1480_DUART_INCHREG(line)
  52. #define R_DUART_ISRREG(line) R_BCM1480_DUART_ISRREG(line)
  53. #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
  54. #include <asm/sibyte/sb1250_regs.h>
  55. #include <asm/sibyte/sb1250_int.h>
  56. #define SBD_CHANREGS(line) A_DUART_CHANREG((line), 0)
  57. #define SBD_CTRLREGS(line) A_DUART_CTRLREG(0)
  58. #define SBD_INT(line) (K_INT_UART_0 + (line))
  59. #else
  60. #error invalid SB1250 UART configuration
  61. #endif
  62. MODULE_AUTHOR("Maciej W. Rozycki <macro@linux-mips.org>");
  63. MODULE_DESCRIPTION("BCM1xxx on-chip DUART serial driver");
  64. MODULE_LICENSE("GPL");
  65. #define DUART_MAX_CHIP 2
  66. #define DUART_MAX_SIDE 2
  67. /*
  68. * Per-port state.
  69. */
  70. struct sbd_port {
  71. struct sbd_duart *duart;
  72. struct uart_port port;
  73. unsigned char __iomem *memctrl;
  74. int tx_stopped;
  75. int initialised;
  76. };
  77. /*
  78. * Per-DUART state for the shared register space.
  79. */
  80. struct sbd_duart {
  81. struct sbd_port sport[2];
  82. unsigned long mapctrl;
  83. refcount_t map_guard;
  84. };
  85. #define to_sport(uport) container_of(uport, struct sbd_port, port)
  86. static struct sbd_duart sbd_duarts[DUART_MAX_CHIP];
  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 __maybe_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 __maybe_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->state->port);
  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.state->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->state->port.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 & (IGNBRK | 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. iounmap(sport->memctrl);
  524. sport->memctrl = NULL;
  525. iounmap(uport->membase);
  526. uport->membase = NULL;
  527. if(refcount_dec_and_test(&duart->map_guard))
  528. release_mem_region(duart->mapctrl, DUART_CHANREG_SPACING);
  529. release_mem_region(uport->mapbase, DUART_CHANREG_SPACING);
  530. }
  531. static int sbd_map_port(struct uart_port *uport)
  532. {
  533. const char *err = KERN_ERR "sbd: Cannot map MMIO\n";
  534. struct sbd_port *sport = to_sport(uport);
  535. struct sbd_duart *duart = sport->duart;
  536. if (!uport->membase)
  537. uport->membase = ioremap_nocache(uport->mapbase,
  538. DUART_CHANREG_SPACING);
  539. if (!uport->membase) {
  540. printk(err);
  541. return -ENOMEM;
  542. }
  543. if (!sport->memctrl)
  544. sport->memctrl = ioremap_nocache(duart->mapctrl,
  545. DUART_CHANREG_SPACING);
  546. if (!sport->memctrl) {
  547. printk(err);
  548. iounmap(uport->membase);
  549. uport->membase = NULL;
  550. return -ENOMEM;
  551. }
  552. return 0;
  553. }
  554. static int sbd_request_port(struct uart_port *uport)
  555. {
  556. const char *err = KERN_ERR "sbd: Unable to reserve MMIO resource\n";
  557. struct sbd_duart *duart = to_sport(uport)->duart;
  558. int ret = 0;
  559. if (!request_mem_region(uport->mapbase, DUART_CHANREG_SPACING,
  560. "sb1250-duart")) {
  561. printk(err);
  562. return -EBUSY;
  563. }
  564. refcount_inc(&duart->map_guard);
  565. if (refcount_read(&duart->map_guard) == 1) {
  566. if (!request_mem_region(duart->mapctrl, DUART_CHANREG_SPACING,
  567. "sb1250-duart")) {
  568. refcount_dec(&duart->map_guard);
  569. printk(err);
  570. ret = -EBUSY;
  571. }
  572. }
  573. if (!ret) {
  574. ret = sbd_map_port(uport);
  575. if (ret) {
  576. if (refcount_dec_and_test(&duart->map_guard))
  577. release_mem_region(duart->mapctrl,
  578. DUART_CHANREG_SPACING);
  579. }
  580. }
  581. if (ret) {
  582. release_mem_region(uport->mapbase, DUART_CHANREG_SPACING);
  583. return ret;
  584. }
  585. return 0;
  586. }
  587. static void sbd_config_port(struct uart_port *uport, int flags)
  588. {
  589. struct sbd_port *sport = to_sport(uport);
  590. if (flags & UART_CONFIG_TYPE) {
  591. if (sbd_request_port(uport))
  592. return;
  593. uport->type = PORT_SB1250_DUART;
  594. sbd_init_port(sport);
  595. }
  596. }
  597. static int sbd_verify_port(struct uart_port *uport, struct serial_struct *ser)
  598. {
  599. int ret = 0;
  600. if (ser->type != PORT_UNKNOWN && ser->type != PORT_SB1250_DUART)
  601. ret = -EINVAL;
  602. if (ser->irq != uport->irq)
  603. ret = -EINVAL;
  604. if (ser->baud_base != uport->uartclk / 16)
  605. ret = -EINVAL;
  606. return ret;
  607. }
  608. static const struct uart_ops sbd_ops = {
  609. .tx_empty = sbd_tx_empty,
  610. .set_mctrl = sbd_set_mctrl,
  611. .get_mctrl = sbd_get_mctrl,
  612. .stop_tx = sbd_stop_tx,
  613. .start_tx = sbd_start_tx,
  614. .stop_rx = sbd_stop_rx,
  615. .enable_ms = sbd_enable_ms,
  616. .break_ctl = sbd_break_ctl,
  617. .startup = sbd_startup,
  618. .shutdown = sbd_shutdown,
  619. .set_termios = sbd_set_termios,
  620. .type = sbd_type,
  621. .release_port = sbd_release_port,
  622. .request_port = sbd_request_port,
  623. .config_port = sbd_config_port,
  624. .verify_port = sbd_verify_port,
  625. };
  626. /* Initialize SB1250 DUART port structures. */
  627. static void __init sbd_probe_duarts(void)
  628. {
  629. static int probed;
  630. int chip, side;
  631. int max_lines, line;
  632. if (probed)
  633. return;
  634. /* Set the number of available units based on the SOC type. */
  635. switch (soc_type) {
  636. case K_SYS_SOC_TYPE_BCM1x55:
  637. case K_SYS_SOC_TYPE_BCM1x80:
  638. max_lines = 4;
  639. break;
  640. default:
  641. /* Assume at least two serial ports at the normal address. */
  642. max_lines = 2;
  643. break;
  644. }
  645. probed = 1;
  646. for (chip = 0, line = 0; chip < DUART_MAX_CHIP && line < max_lines;
  647. chip++) {
  648. sbd_duarts[chip].mapctrl = SBD_CTRLREGS(line);
  649. for (side = 0; side < DUART_MAX_SIDE && line < max_lines;
  650. side++, line++) {
  651. struct sbd_port *sport = &sbd_duarts[chip].sport[side];
  652. struct uart_port *uport = &sport->port;
  653. sport->duart = &sbd_duarts[chip];
  654. uport->irq = SBD_INT(line);
  655. uport->uartclk = 100000000 / 20 * 16;
  656. uport->fifosize = 16;
  657. uport->iotype = UPIO_MEM;
  658. uport->flags = UPF_BOOT_AUTOCONF;
  659. uport->ops = &sbd_ops;
  660. uport->line = line;
  661. uport->mapbase = SBD_CHANREGS(line);
  662. }
  663. }
  664. }
  665. #ifdef CONFIG_SERIAL_SB1250_DUART_CONSOLE
  666. /*
  667. * Serial console stuff. Very basic, polling driver for doing serial
  668. * console output. The console_lock is held by the caller, so we
  669. * shouldn't be interrupted for more console activity.
  670. */
  671. static void sbd_console_putchar(struct uart_port *uport, int ch)
  672. {
  673. struct sbd_port *sport = to_sport(uport);
  674. sbd_transmit_drain(sport);
  675. write_sbdchn(sport, R_DUART_TX_HOLD, ch);
  676. }
  677. static void sbd_console_write(struct console *co, const char *s,
  678. unsigned int count)
  679. {
  680. int chip = co->index / DUART_MAX_SIDE;
  681. int side = co->index % DUART_MAX_SIDE;
  682. struct sbd_port *sport = &sbd_duarts[chip].sport[side];
  683. struct uart_port *uport = &sport->port;
  684. unsigned long flags;
  685. unsigned int mask;
  686. /* Disable transmit interrupts and enable the transmitter. */
  687. spin_lock_irqsave(&uport->lock, flags);
  688. mask = read_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2));
  689. write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2),
  690. mask & ~M_DUART_IMR_TX);
  691. write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_EN);
  692. spin_unlock_irqrestore(&uport->lock, flags);
  693. uart_console_write(&sport->port, s, count, sbd_console_putchar);
  694. /* Restore transmit interrupts and the transmitter enable. */
  695. spin_lock_irqsave(&uport->lock, flags);
  696. sbd_line_drain(sport);
  697. if (sport->tx_stopped)
  698. write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS);
  699. write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), mask);
  700. spin_unlock_irqrestore(&uport->lock, flags);
  701. }
  702. static int __init sbd_console_setup(struct console *co, char *options)
  703. {
  704. int chip = co->index / DUART_MAX_SIDE;
  705. int side = co->index % DUART_MAX_SIDE;
  706. struct sbd_port *sport = &sbd_duarts[chip].sport[side];
  707. struct uart_port *uport = &sport->port;
  708. int baud = 115200;
  709. int bits = 8;
  710. int parity = 'n';
  711. int flow = 'n';
  712. int ret;
  713. if (!sport->duart)
  714. return -ENXIO;
  715. ret = sbd_map_port(uport);
  716. if (ret)
  717. return ret;
  718. sbd_init_port(sport);
  719. if (options)
  720. uart_parse_options(options, &baud, &parity, &bits, &flow);
  721. return uart_set_options(uport, co, baud, parity, bits, flow);
  722. }
  723. static struct uart_driver sbd_reg;
  724. static struct console sbd_console = {
  725. .name = "duart",
  726. .write = sbd_console_write,
  727. .device = uart_console_device,
  728. .setup = sbd_console_setup,
  729. .flags = CON_PRINTBUFFER,
  730. .index = -1,
  731. .data = &sbd_reg
  732. };
  733. static int __init sbd_serial_console_init(void)
  734. {
  735. sbd_probe_duarts();
  736. register_console(&sbd_console);
  737. return 0;
  738. }
  739. console_initcall(sbd_serial_console_init);
  740. #define SERIAL_SB1250_DUART_CONSOLE &sbd_console
  741. #else
  742. #define SERIAL_SB1250_DUART_CONSOLE NULL
  743. #endif /* CONFIG_SERIAL_SB1250_DUART_CONSOLE */
  744. static struct uart_driver sbd_reg = {
  745. .owner = THIS_MODULE,
  746. .driver_name = "sb1250_duart",
  747. .dev_name = "duart",
  748. .major = TTY_MAJOR,
  749. .minor = SB1250_DUART_MINOR_BASE,
  750. .nr = DUART_MAX_CHIP * DUART_MAX_SIDE,
  751. .cons = SERIAL_SB1250_DUART_CONSOLE,
  752. };
  753. /* Set up the driver and register it. */
  754. static int __init sbd_init(void)
  755. {
  756. int i, ret;
  757. sbd_probe_duarts();
  758. ret = uart_register_driver(&sbd_reg);
  759. if (ret)
  760. return ret;
  761. for (i = 0; i < DUART_MAX_CHIP * DUART_MAX_SIDE; i++) {
  762. struct sbd_duart *duart = &sbd_duarts[i / DUART_MAX_SIDE];
  763. struct sbd_port *sport = &duart->sport[i % DUART_MAX_SIDE];
  764. struct uart_port *uport = &sport->port;
  765. if (sport->duart)
  766. uart_add_one_port(&sbd_reg, uport);
  767. }
  768. return 0;
  769. }
  770. /* Unload the driver. Unregister stuff, get ready to go away. */
  771. static void __exit sbd_exit(void)
  772. {
  773. int i;
  774. for (i = DUART_MAX_CHIP * DUART_MAX_SIDE - 1; i >= 0; i--) {
  775. struct sbd_duart *duart = &sbd_duarts[i / DUART_MAX_SIDE];
  776. struct sbd_port *sport = &duart->sport[i % DUART_MAX_SIDE];
  777. struct uart_port *uport = &sport->port;
  778. if (sport->duart)
  779. uart_remove_one_port(&sbd_reg, uport);
  780. }
  781. uart_unregister_driver(&sbd_reg);
  782. }
  783. module_init(sbd_init);
  784. module_exit(sbd_exit);