sb1250-duart.c 24 KB

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