sb1250-duart.c 24 KB

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