sb1250-duart.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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_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 <macro@linux-mips.org>");
  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;
  267. int count;
  268. u8 ch, flag;
  269. for (count = 16; count; count--) {
  270. status = read_sbdchn(sport, R_DUART_STATUS);
  271. if (!(status & M_DUART_RX_RDY))
  272. break;
  273. ch = read_sbdchn(sport, R_DUART_RX_HOLD);
  274. flag = TTY_NORMAL;
  275. icount = &uport->icount;
  276. icount->rx++;
  277. if (unlikely(status &
  278. (M_DUART_RCVD_BRK | M_DUART_FRM_ERR |
  279. M_DUART_PARITY_ERR | M_DUART_OVRUN_ERR))) {
  280. if (status & M_DUART_RCVD_BRK) {
  281. icount->brk++;
  282. if (uart_handle_break(uport))
  283. continue;
  284. } else if (status & M_DUART_FRM_ERR)
  285. icount->frame++;
  286. else if (status & M_DUART_PARITY_ERR)
  287. icount->parity++;
  288. if (status & M_DUART_OVRUN_ERR)
  289. icount->overrun++;
  290. status &= uport->read_status_mask;
  291. if (status & M_DUART_RCVD_BRK)
  292. flag = TTY_BREAK;
  293. else if (status & M_DUART_FRM_ERR)
  294. flag = TTY_FRAME;
  295. else if (status & M_DUART_PARITY_ERR)
  296. flag = TTY_PARITY;
  297. }
  298. if (uart_handle_sysrq_char(uport, ch))
  299. continue;
  300. uart_insert_char(uport, status, M_DUART_OVRUN_ERR, ch, flag);
  301. }
  302. tty_flip_buffer_push(&uport->state->port);
  303. }
  304. static void sbd_transmit_chars(struct sbd_port *sport)
  305. {
  306. struct uart_port *uport = &sport->port;
  307. struct tty_port *tport = &sport->port.state->port;
  308. unsigned char ch;
  309. unsigned int mask;
  310. int stop_tx;
  311. /* XON/XOFF chars. */
  312. if (sport->port.x_char) {
  313. write_sbdchn(sport, R_DUART_TX_HOLD, sport->port.x_char);
  314. sport->port.icount.tx++;
  315. sport->port.x_char = 0;
  316. return;
  317. }
  318. /* If nothing to do or stopped or hardware stopped. */
  319. stop_tx = uart_tx_stopped(&sport->port) ||
  320. !uart_fifo_get(&sport->port, &ch);
  321. /* Send char. */
  322. if (!stop_tx) {
  323. write_sbdchn(sport, R_DUART_TX_HOLD, ch);
  324. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  325. uart_write_wakeup(&sport->port);
  326. }
  327. /* Are we are done? */
  328. if (stop_tx || kfifo_is_empty(&tport->xmit_fifo)) {
  329. /* Disable tx interrupts. */
  330. mask = read_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2));
  331. mask &= ~M_DUART_IMR_TX;
  332. write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), mask);
  333. }
  334. }
  335. static void sbd_status_handle(struct sbd_port *sport)
  336. {
  337. struct uart_port *uport = &sport->port;
  338. unsigned int delta;
  339. delta = read_sbdshr(sport, R_DUART_INCHREG((uport->line) % 2));
  340. delta >>= (uport->line) % 2;
  341. if (delta & (M_DUART_IN_PIN0_VAL << S_DUART_IN_PIN_CHNG))
  342. uart_handle_cts_change(uport, !(delta & M_DUART_IN_PIN0_VAL));
  343. if (delta & (M_DUART_IN_PIN2_VAL << S_DUART_IN_PIN_CHNG))
  344. uport->icount.dsr++;
  345. if (delta & ((M_DUART_IN_PIN2_VAL | M_DUART_IN_PIN0_VAL) <<
  346. S_DUART_IN_PIN_CHNG))
  347. wake_up_interruptible(&uport->state->port.delta_msr_wait);
  348. }
  349. static irqreturn_t sbd_interrupt(int irq, void *dev_id)
  350. {
  351. struct sbd_port *sport = dev_id;
  352. struct uart_port *uport = &sport->port;
  353. irqreturn_t status = IRQ_NONE;
  354. unsigned int intstat;
  355. int count;
  356. for (count = 16; count; count--) {
  357. intstat = read_sbdshr(sport,
  358. R_DUART_ISRREG((uport->line) % 2));
  359. intstat &= read_sbdshr(sport,
  360. R_DUART_IMRREG((uport->line) % 2));
  361. intstat &= M_DUART_ISR_ALL;
  362. if (!intstat)
  363. break;
  364. if (intstat & M_DUART_ISR_RX)
  365. sbd_receive_chars(sport);
  366. if (intstat & M_DUART_ISR_IN)
  367. sbd_status_handle(sport);
  368. if (intstat & M_DUART_ISR_TX)
  369. sbd_transmit_chars(sport);
  370. status = IRQ_HANDLED;
  371. }
  372. return status;
  373. }
  374. static int sbd_startup(struct uart_port *uport)
  375. {
  376. struct sbd_port *sport = to_sport(uport);
  377. unsigned int mode1;
  378. int ret;
  379. ret = request_irq(sport->port.irq, sbd_interrupt,
  380. IRQF_SHARED, "sb1250-duart", sport);
  381. if (ret)
  382. return ret;
  383. /* Clear the receive FIFO. */
  384. sbd_receive_drain(sport);
  385. /* Clear the interrupt registers. */
  386. write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_RESET_BREAK_INT);
  387. read_sbdshr(sport, R_DUART_INCHREG((uport->line) % 2));
  388. /* Set rx/tx interrupt to FIFO available. */
  389. mode1 = read_sbdchn(sport, R_DUART_MODE_REG_1);
  390. mode1 &= ~(M_DUART_RX_IRQ_SEL_RXFULL | M_DUART_TX_IRQ_SEL_TXEMPT);
  391. write_sbdchn(sport, R_DUART_MODE_REG_1, mode1);
  392. /* Disable tx, enable rx. */
  393. write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS | M_DUART_RX_EN);
  394. sport->tx_stopped = 1;
  395. /* Enable interrupts. */
  396. write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2),
  397. M_DUART_IMR_IN | M_DUART_IMR_RX);
  398. return 0;
  399. }
  400. static void sbd_shutdown(struct uart_port *uport)
  401. {
  402. struct sbd_port *sport = to_sport(uport);
  403. write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS | M_DUART_RX_DIS);
  404. sport->tx_stopped = 1;
  405. free_irq(sport->port.irq, sport);
  406. }
  407. static void sbd_init_port(struct sbd_port *sport)
  408. {
  409. struct uart_port *uport = &sport->port;
  410. if (sport->initialised)
  411. return;
  412. /* There is no DUART reset feature, so just set some sane defaults. */
  413. write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_RESET_TX);
  414. write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_RESET_RX);
  415. write_sbdchn(sport, R_DUART_MODE_REG_1, V_DUART_BITS_PER_CHAR_8);
  416. write_sbdchn(sport, R_DUART_MODE_REG_2, 0);
  417. write_sbdchn(sport, R_DUART_FULL_CTL,
  418. V_DUART_INT_TIME(0) | V_DUART_SIG_FULL(15));
  419. write_sbdchn(sport, R_DUART_OPCR_X, 0);
  420. write_sbdchn(sport, R_DUART_AUXCTL_X, 0);
  421. write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), 0);
  422. sport->initialised = 1;
  423. }
  424. static void sbd_set_termios(struct uart_port *uport, struct ktermios *termios,
  425. const struct ktermios *old_termios)
  426. {
  427. struct sbd_port *sport = to_sport(uport);
  428. unsigned int mode1 = 0, mode2 = 0, aux = 0;
  429. unsigned int mode1mask = 0, mode2mask = 0, auxmask = 0;
  430. unsigned int oldmode1, oldmode2, oldaux;
  431. unsigned int baud, brg;
  432. unsigned int command;
  433. mode1mask |= ~(M_DUART_PARITY_MODE | M_DUART_PARITY_TYPE_ODD |
  434. M_DUART_BITS_PER_CHAR);
  435. mode2mask |= ~M_DUART_STOP_BIT_LEN_2;
  436. auxmask |= ~M_DUART_CTS_CHNG_ENA;
  437. /* Byte size. */
  438. switch (termios->c_cflag & CSIZE) {
  439. case CS5:
  440. case CS6:
  441. /* Unsupported, leave unchanged. */
  442. mode1mask |= M_DUART_PARITY_MODE;
  443. break;
  444. case CS7:
  445. mode1 |= V_DUART_BITS_PER_CHAR_7;
  446. break;
  447. case CS8:
  448. default:
  449. mode1 |= V_DUART_BITS_PER_CHAR_8;
  450. break;
  451. }
  452. /* Parity and stop bits. */
  453. if (termios->c_cflag & CSTOPB)
  454. mode2 |= M_DUART_STOP_BIT_LEN_2;
  455. else
  456. mode2 |= M_DUART_STOP_BIT_LEN_1;
  457. if (termios->c_cflag & PARENB)
  458. mode1 |= V_DUART_PARITY_MODE_ADD;
  459. else
  460. mode1 |= V_DUART_PARITY_MODE_NONE;
  461. if (termios->c_cflag & PARODD)
  462. mode1 |= M_DUART_PARITY_TYPE_ODD;
  463. else
  464. mode1 |= M_DUART_PARITY_TYPE_EVEN;
  465. baud = uart_get_baud_rate(uport, termios, old_termios, 1200, 5000000);
  466. brg = V_DUART_BAUD_RATE(baud);
  467. /* The actual lower bound is 1221bps, so compensate. */
  468. if (brg > M_DUART_CLK_COUNTER)
  469. brg = M_DUART_CLK_COUNTER;
  470. uart_update_timeout(uport, termios->c_cflag, baud);
  471. uport->read_status_mask = M_DUART_OVRUN_ERR;
  472. if (termios->c_iflag & INPCK)
  473. uport->read_status_mask |= M_DUART_FRM_ERR |
  474. M_DUART_PARITY_ERR;
  475. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  476. uport->read_status_mask |= M_DUART_RCVD_BRK;
  477. uport->ignore_status_mask = 0;
  478. if (termios->c_iflag & IGNPAR)
  479. uport->ignore_status_mask |= M_DUART_FRM_ERR |
  480. M_DUART_PARITY_ERR;
  481. if (termios->c_iflag & IGNBRK) {
  482. uport->ignore_status_mask |= M_DUART_RCVD_BRK;
  483. if (termios->c_iflag & IGNPAR)
  484. uport->ignore_status_mask |= M_DUART_OVRUN_ERR;
  485. }
  486. if (termios->c_cflag & CREAD)
  487. command = M_DUART_RX_EN;
  488. else
  489. command = M_DUART_RX_DIS;
  490. if (termios->c_cflag & CRTSCTS)
  491. aux |= M_DUART_CTS_CHNG_ENA;
  492. else
  493. aux &= ~M_DUART_CTS_CHNG_ENA;
  494. uart_port_lock(uport);
  495. if (sport->tx_stopped)
  496. command |= M_DUART_TX_DIS;
  497. else
  498. command |= M_DUART_TX_EN;
  499. oldmode1 = read_sbdchn(sport, R_DUART_MODE_REG_1) & mode1mask;
  500. oldmode2 = read_sbdchn(sport, R_DUART_MODE_REG_2) & mode2mask;
  501. oldaux = read_sbdchn(sport, R_DUART_AUXCTL_X) & auxmask;
  502. if (!sport->tx_stopped)
  503. sbd_line_drain(sport);
  504. write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS | M_DUART_RX_DIS);
  505. write_sbdchn(sport, R_DUART_MODE_REG_1, mode1 | oldmode1);
  506. write_sbdchn(sport, R_DUART_MODE_REG_2, mode2 | oldmode2);
  507. write_sbdchn(sport, R_DUART_CLK_SEL, brg);
  508. write_sbdchn(sport, R_DUART_AUXCTL_X, aux | oldaux);
  509. write_sbdchn(sport, R_DUART_CMD, command);
  510. uart_port_unlock(uport);
  511. }
  512. static const char *sbd_type(struct uart_port *uport)
  513. {
  514. return "SB1250 DUART";
  515. }
  516. static void sbd_release_port(struct uart_port *uport)
  517. {
  518. struct sbd_port *sport = to_sport(uport);
  519. struct sbd_duart *duart = sport->duart;
  520. iounmap(sport->memctrl);
  521. sport->memctrl = NULL;
  522. iounmap(uport->membase);
  523. uport->membase = NULL;
  524. if(refcount_dec_and_test(&duart->map_guard))
  525. release_mem_region(duart->mapctrl, DUART_CHANREG_SPACING);
  526. release_mem_region(uport->mapbase, DUART_CHANREG_SPACING);
  527. }
  528. static int sbd_map_port(struct uart_port *uport)
  529. {
  530. const char *err = KERN_ERR "sbd: Cannot map MMIO\n";
  531. struct sbd_port *sport = to_sport(uport);
  532. struct sbd_duart *duart = sport->duart;
  533. if (!uport->membase)
  534. uport->membase = ioremap(uport->mapbase,
  535. DUART_CHANREG_SPACING);
  536. if (!uport->membase) {
  537. printk(err);
  538. return -ENOMEM;
  539. }
  540. if (!sport->memctrl)
  541. sport->memctrl = ioremap(duart->mapctrl,
  542. DUART_CHANREG_SPACING);
  543. if (!sport->memctrl) {
  544. printk(err);
  545. iounmap(uport->membase);
  546. uport->membase = NULL;
  547. return -ENOMEM;
  548. }
  549. return 0;
  550. }
  551. static int sbd_request_port(struct uart_port *uport)
  552. {
  553. const char *err = KERN_ERR "sbd: Unable to reserve MMIO resource\n";
  554. struct sbd_duart *duart = to_sport(uport)->duart;
  555. int ret = 0;
  556. if (!request_mem_region(uport->mapbase, DUART_CHANREG_SPACING,
  557. "sb1250-duart")) {
  558. printk(err);
  559. return -EBUSY;
  560. }
  561. refcount_inc(&duart->map_guard);
  562. if (refcount_read(&duart->map_guard) == 1) {
  563. if (!request_mem_region(duart->mapctrl, DUART_CHANREG_SPACING,
  564. "sb1250-duart")) {
  565. refcount_dec(&duart->map_guard);
  566. printk(err);
  567. ret = -EBUSY;
  568. }
  569. }
  570. if (!ret) {
  571. ret = sbd_map_port(uport);
  572. if (ret) {
  573. if (refcount_dec_and_test(&duart->map_guard))
  574. release_mem_region(duart->mapctrl,
  575. DUART_CHANREG_SPACING);
  576. }
  577. }
  578. if (ret) {
  579. release_mem_region(uport->mapbase, DUART_CHANREG_SPACING);
  580. return ret;
  581. }
  582. return 0;
  583. }
  584. static void sbd_config_port(struct uart_port *uport, int flags)
  585. {
  586. struct sbd_port *sport = to_sport(uport);
  587. if (flags & UART_CONFIG_TYPE) {
  588. if (sbd_request_port(uport))
  589. return;
  590. uport->type = PORT_SB1250_DUART;
  591. sbd_init_port(sport);
  592. }
  593. }
  594. static int sbd_verify_port(struct uart_port *uport, struct serial_struct *ser)
  595. {
  596. int ret = 0;
  597. if (ser->type != PORT_UNKNOWN && ser->type != PORT_SB1250_DUART)
  598. ret = -EINVAL;
  599. if (ser->irq != uport->irq)
  600. ret = -EINVAL;
  601. if (ser->baud_base != uport->uartclk / 16)
  602. ret = -EINVAL;
  603. return ret;
  604. }
  605. static const struct uart_ops sbd_ops = {
  606. .tx_empty = sbd_tx_empty,
  607. .set_mctrl = sbd_set_mctrl,
  608. .get_mctrl = sbd_get_mctrl,
  609. .stop_tx = sbd_stop_tx,
  610. .start_tx = sbd_start_tx,
  611. .stop_rx = sbd_stop_rx,
  612. .enable_ms = sbd_enable_ms,
  613. .break_ctl = sbd_break_ctl,
  614. .startup = sbd_startup,
  615. .shutdown = sbd_shutdown,
  616. .set_termios = sbd_set_termios,
  617. .type = sbd_type,
  618. .release_port = sbd_release_port,
  619. .request_port = sbd_request_port,
  620. .config_port = sbd_config_port,
  621. .verify_port = sbd_verify_port,
  622. };
  623. /* Initialize SB1250 DUART port structures. */
  624. static void __init sbd_probe_duarts(void)
  625. {
  626. static int probed;
  627. int chip, side;
  628. int max_lines, line;
  629. if (probed)
  630. return;
  631. /* Set the number of available units based on the SOC type. */
  632. switch (soc_type) {
  633. case K_SYS_SOC_TYPE_BCM1x55:
  634. case K_SYS_SOC_TYPE_BCM1x80:
  635. max_lines = 4;
  636. break;
  637. default:
  638. /* Assume at least two serial ports at the normal address. */
  639. max_lines = 2;
  640. break;
  641. }
  642. probed = 1;
  643. for (chip = 0, line = 0; chip < DUART_MAX_CHIP && line < max_lines;
  644. chip++) {
  645. sbd_duarts[chip].mapctrl = SBD_CTRLREGS(line);
  646. for (side = 0; side < DUART_MAX_SIDE && line < max_lines;
  647. side++, line++) {
  648. struct sbd_port *sport = &sbd_duarts[chip].sport[side];
  649. struct uart_port *uport = &sport->port;
  650. sport->duart = &sbd_duarts[chip];
  651. uport->irq = SBD_INT(line);
  652. uport->uartclk = 100000000 / 20 * 16;
  653. uport->fifosize = 16;
  654. uport->iotype = UPIO_MEM;
  655. uport->flags = UPF_BOOT_AUTOCONF;
  656. uport->ops = &sbd_ops;
  657. uport->line = line;
  658. uport->mapbase = SBD_CHANREGS(line);
  659. uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_SB1250_DUART_CONSOLE);
  660. }
  661. }
  662. }
  663. #ifdef CONFIG_SERIAL_SB1250_DUART_CONSOLE
  664. /*
  665. * Serial console stuff. Very basic, polling driver for doing serial
  666. * console output. The console_lock is held by the caller, so we
  667. * shouldn't be interrupted for more console activity.
  668. */
  669. static void sbd_console_putchar(struct uart_port *uport, unsigned char ch)
  670. {
  671. struct sbd_port *sport = to_sport(uport);
  672. sbd_transmit_drain(sport);
  673. write_sbdchn(sport, R_DUART_TX_HOLD, ch);
  674. }
  675. static void sbd_console_write(struct console *co, const char *s,
  676. unsigned int count)
  677. {
  678. int chip = co->index / DUART_MAX_SIDE;
  679. int side = co->index % DUART_MAX_SIDE;
  680. struct sbd_port *sport = &sbd_duarts[chip].sport[side];
  681. struct uart_port *uport = &sport->port;
  682. unsigned long flags;
  683. unsigned int mask;
  684. /* Disable transmit interrupts and enable the transmitter. */
  685. uart_port_lock_irqsave(uport, &flags);
  686. mask = read_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2));
  687. write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2),
  688. mask & ~M_DUART_IMR_TX);
  689. write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_EN);
  690. uart_port_unlock_irqrestore(uport, flags);
  691. uart_console_write(&sport->port, s, count, sbd_console_putchar);
  692. /* Restore transmit interrupts and the transmitter enable. */
  693. uart_port_lock_irqsave(uport, &flags);
  694. sbd_line_drain(sport);
  695. if (sport->tx_stopped)
  696. write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS);
  697. write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), mask);
  698. uart_port_unlock_irqrestore(uport, flags);
  699. }
  700. static int __init sbd_console_setup(struct console *co, char *options)
  701. {
  702. int chip = co->index / DUART_MAX_SIDE;
  703. int side = co->index % DUART_MAX_SIDE;
  704. struct sbd_port *sport = &sbd_duarts[chip].sport[side];
  705. struct uart_port *uport = &sport->port;
  706. int baud = 115200;
  707. int bits = 8;
  708. int parity = 'n';
  709. int flow = 'n';
  710. int ret;
  711. if (!sport->duart)
  712. return -ENXIO;
  713. ret = sbd_map_port(uport);
  714. if (ret)
  715. return ret;
  716. sbd_init_port(sport);
  717. if (options)
  718. uart_parse_options(options, &baud, &parity, &bits, &flow);
  719. return uart_set_options(uport, co, baud, parity, bits, flow);
  720. }
  721. static struct uart_driver sbd_reg;
  722. static struct console sbd_console = {
  723. .name = "duart",
  724. .write = sbd_console_write,
  725. .device = uart_console_device,
  726. .setup = sbd_console_setup,
  727. .flags = CON_PRINTBUFFER,
  728. .index = -1,
  729. .data = &sbd_reg
  730. };
  731. static int __init sbd_serial_console_init(void)
  732. {
  733. sbd_probe_duarts();
  734. register_console(&sbd_console);
  735. return 0;
  736. }
  737. console_initcall(sbd_serial_console_init);
  738. #define SERIAL_SB1250_DUART_CONSOLE &sbd_console
  739. #else
  740. #define SERIAL_SB1250_DUART_CONSOLE NULL
  741. #endif /* CONFIG_SERIAL_SB1250_DUART_CONSOLE */
  742. static struct uart_driver sbd_reg = {
  743. .owner = THIS_MODULE,
  744. .driver_name = "sb1250_duart",
  745. .dev_name = "duart",
  746. .major = TTY_MAJOR,
  747. .minor = SB1250_DUART_MINOR_BASE,
  748. .nr = DUART_MAX_CHIP * DUART_MAX_SIDE,
  749. .cons = SERIAL_SB1250_DUART_CONSOLE,
  750. };
  751. /* Set up the driver and register it. */
  752. static int __init sbd_init(void)
  753. {
  754. int i, ret;
  755. sbd_probe_duarts();
  756. ret = uart_register_driver(&sbd_reg);
  757. if (ret)
  758. return ret;
  759. for (i = 0; i < DUART_MAX_CHIP * DUART_MAX_SIDE; i++) {
  760. struct sbd_duart *duart = &sbd_duarts[i / DUART_MAX_SIDE];
  761. struct sbd_port *sport = &duart->sport[i % DUART_MAX_SIDE];
  762. struct uart_port *uport = &sport->port;
  763. if (sport->duart)
  764. uart_add_one_port(&sbd_reg, uport);
  765. }
  766. return 0;
  767. }
  768. /* Unload the driver. Unregister stuff, get ready to go away. */
  769. static void __exit sbd_exit(void)
  770. {
  771. int i;
  772. for (i = DUART_MAX_CHIP * DUART_MAX_SIDE - 1; i >= 0; i--) {
  773. struct sbd_duart *duart = &sbd_duarts[i / DUART_MAX_SIDE];
  774. struct sbd_port *sport = &duart->sport[i % DUART_MAX_SIDE];
  775. struct uart_port *uport = &sport->port;
  776. if (sport->duart)
  777. uart_remove_one_port(&sbd_reg, uport);
  778. }
  779. uart_unregister_driver(&sbd_reg);
  780. }
  781. module_init(sbd_init);
  782. module_exit(sbd_exit);