m32r_sio.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * m32r_sio.c
  4. *
  5. * Driver for M32R serial ports
  6. *
  7. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  8. * Based on drivers/serial/8250.c.
  9. *
  10. * Copyright (C) 2001 Russell King.
  11. * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
  12. */
  13. /*
  14. * A note about mapbase / membase
  15. *
  16. * mapbase is the physical address of the IO port. Currently, we don't
  17. * support this very well, and it may well be dropped from this driver
  18. * in future. As such, mapbase should be NULL.
  19. *
  20. * membase is an 'ioremapped' cookie. This is compatible with the old
  21. * serial.c driver, and is currently the preferred form.
  22. */
  23. #if defined(CONFIG_SERIAL_M32R_SIO_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  24. #define SUPPORT_SYSRQ
  25. #endif
  26. #include <linux/tty.h>
  27. #include <linux/tty_flip.h>
  28. #include <linux/ioport.h>
  29. #include <linux/init.h>
  30. #include <linux/console.h>
  31. #include <linux/sysrq.h>
  32. #include <linux/serial.h>
  33. #include <linux/delay.h>
  34. #include <asm/m32r.h>
  35. #include <asm/io.h>
  36. #include <asm/irq.h>
  37. #define BAUD_RATE 115200
  38. #include <linux/serial_core.h>
  39. #include "m32r_sio_reg.h"
  40. #define PASS_LIMIT 256
  41. static const struct {
  42. unsigned int port;
  43. unsigned int irq;
  44. } old_serial_port[] = {
  45. #if defined(CONFIG_PLAT_USRV)
  46. /* PORT IRQ FLAGS */
  47. { 0x3F8, PLD_IRQ_UART0 }, /* ttyS0 */
  48. { 0x2F8, PLD_IRQ_UART1 }, /* ttyS1 */
  49. #elif defined(CONFIG_SERIAL_M32R_PLDSIO)
  50. { ((unsigned long)PLD_ESIO0CR), PLD_IRQ_SIO0_RCV }, /* ttyS0 */
  51. #else
  52. { M32R_SIO_OFFSET, M32R_IRQ_SIO0_R }, /* ttyS0 */
  53. #endif
  54. };
  55. #define UART_NR ARRAY_SIZE(old_serial_port)
  56. struct uart_sio_port {
  57. struct uart_port port;
  58. struct timer_list timer; /* "no irq" timer */
  59. struct list_head list; /* ports on this IRQ */
  60. unsigned char ier;
  61. };
  62. struct irq_info {
  63. spinlock_t lock;
  64. struct list_head *head;
  65. };
  66. static struct irq_info irq_lists[NR_IRQS];
  67. #ifdef CONFIG_SERIAL_M32R_PLDSIO
  68. #define __sio_in(x) inw((unsigned long)(x))
  69. #define __sio_out(v,x) outw((v),(unsigned long)(x))
  70. static inline void sio_set_baud_rate(unsigned long baud)
  71. {
  72. unsigned short sbaud;
  73. sbaud = (boot_cpu_data.bus_clock / (baud * 4))-1;
  74. __sio_out(sbaud, PLD_ESIO0BAUR);
  75. }
  76. static void sio_reset(void)
  77. {
  78. unsigned short tmp;
  79. tmp = __sio_in(PLD_ESIO0RXB);
  80. tmp = __sio_in(PLD_ESIO0RXB);
  81. tmp = __sio_in(PLD_ESIO0CR);
  82. sio_set_baud_rate(BAUD_RATE);
  83. __sio_out(0x0300, PLD_ESIO0CR);
  84. __sio_out(0x0003, PLD_ESIO0CR);
  85. }
  86. static void sio_init(void)
  87. {
  88. unsigned short tmp;
  89. tmp = __sio_in(PLD_ESIO0RXB);
  90. tmp = __sio_in(PLD_ESIO0RXB);
  91. tmp = __sio_in(PLD_ESIO0CR);
  92. __sio_out(0x0300, PLD_ESIO0CR);
  93. __sio_out(0x0003, PLD_ESIO0CR);
  94. }
  95. static void sio_error(int *status)
  96. {
  97. printk("SIO0 error[%04x]\n", *status);
  98. do {
  99. sio_init();
  100. } while ((*status = __sio_in(PLD_ESIO0CR)) != 3);
  101. }
  102. #else /* not CONFIG_SERIAL_M32R_PLDSIO */
  103. #define __sio_in(x) inl(x)
  104. #define __sio_out(v,x) outl((v),(x))
  105. static inline void sio_set_baud_rate(unsigned long baud)
  106. {
  107. unsigned long i, j;
  108. i = boot_cpu_data.bus_clock / (baud * 16);
  109. j = (boot_cpu_data.bus_clock - (i * baud * 16)) / baud;
  110. i -= 1;
  111. j = (j + 1) >> 1;
  112. __sio_out(i, M32R_SIO0_BAUR_PORTL);
  113. __sio_out(j, M32R_SIO0_RBAUR_PORTL);
  114. }
  115. static void sio_reset(void)
  116. {
  117. __sio_out(0x00000300, M32R_SIO0_CR_PORTL); /* init status */
  118. __sio_out(0x00000800, M32R_SIO0_MOD1_PORTL); /* 8bit */
  119. __sio_out(0x00000080, M32R_SIO0_MOD0_PORTL); /* 1stop non */
  120. sio_set_baud_rate(BAUD_RATE);
  121. __sio_out(0x00000000, M32R_SIO0_TRCR_PORTL);
  122. __sio_out(0x00000003, M32R_SIO0_CR_PORTL); /* RXCEN */
  123. }
  124. static void sio_init(void)
  125. {
  126. unsigned int tmp;
  127. tmp = __sio_in(M32R_SIO0_RXB_PORTL);
  128. tmp = __sio_in(M32R_SIO0_RXB_PORTL);
  129. tmp = __sio_in(M32R_SIO0_STS_PORTL);
  130. __sio_out(0x00000003, M32R_SIO0_CR_PORTL);
  131. }
  132. static void sio_error(int *status)
  133. {
  134. printk("SIO0 error[%04x]\n", *status);
  135. do {
  136. sio_init();
  137. } while ((*status = __sio_in(M32R_SIO0_CR_PORTL)) != 3);
  138. }
  139. #endif /* CONFIG_SERIAL_M32R_PLDSIO */
  140. static unsigned int sio_in(struct uart_sio_port *up, int offset)
  141. {
  142. return __sio_in(up->port.iobase + offset);
  143. }
  144. static void sio_out(struct uart_sio_port *up, int offset, int value)
  145. {
  146. __sio_out(value, up->port.iobase + offset);
  147. }
  148. static unsigned int serial_in(struct uart_sio_port *up, int offset)
  149. {
  150. if (!offset)
  151. return 0;
  152. return __sio_in(offset);
  153. }
  154. static void serial_out(struct uart_sio_port *up, int offset, int value)
  155. {
  156. if (!offset)
  157. return;
  158. __sio_out(value, offset);
  159. }
  160. static void m32r_sio_stop_tx(struct uart_port *port)
  161. {
  162. struct uart_sio_port *up =
  163. container_of(port, struct uart_sio_port, port);
  164. if (up->ier & UART_IER_THRI) {
  165. up->ier &= ~UART_IER_THRI;
  166. serial_out(up, UART_IER, up->ier);
  167. }
  168. }
  169. static void m32r_sio_start_tx(struct uart_port *port)
  170. {
  171. #ifdef CONFIG_SERIAL_M32R_PLDSIO
  172. struct uart_sio_port *up =
  173. container_of(port, struct uart_sio_port, port);
  174. struct circ_buf *xmit = &up->port.state->xmit;
  175. if (!(up->ier & UART_IER_THRI)) {
  176. up->ier |= UART_IER_THRI;
  177. serial_out(up, UART_IER, up->ier);
  178. if (!uart_circ_empty(xmit)) {
  179. serial_out(up, UART_TX, xmit->buf[xmit->tail]);
  180. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  181. up->port.icount.tx++;
  182. }
  183. }
  184. while((serial_in(up, UART_LSR) & UART_EMPTY) != UART_EMPTY);
  185. #else
  186. struct uart_sio_port *up =
  187. container_of(port, struct uart_sio_port, port);
  188. if (!(up->ier & UART_IER_THRI)) {
  189. up->ier |= UART_IER_THRI;
  190. serial_out(up, UART_IER, up->ier);
  191. }
  192. #endif
  193. }
  194. static void m32r_sio_stop_rx(struct uart_port *port)
  195. {
  196. struct uart_sio_port *up =
  197. container_of(port, struct uart_sio_port, port);
  198. up->ier &= ~UART_IER_RLSI;
  199. up->port.read_status_mask &= ~UART_LSR_DR;
  200. serial_out(up, UART_IER, up->ier);
  201. }
  202. static void m32r_sio_enable_ms(struct uart_port *port)
  203. {
  204. struct uart_sio_port *up =
  205. container_of(port, struct uart_sio_port, port);
  206. up->ier |= UART_IER_MSI;
  207. serial_out(up, UART_IER, up->ier);
  208. }
  209. static void receive_chars(struct uart_sio_port *up, int *status)
  210. {
  211. struct tty_port *port = &up->port.state->port;
  212. unsigned char ch;
  213. unsigned char flag;
  214. int max_count = 256;
  215. do {
  216. ch = sio_in(up, SIORXB);
  217. flag = TTY_NORMAL;
  218. up->port.icount.rx++;
  219. if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
  220. UART_LSR_FE | UART_LSR_OE))) {
  221. /*
  222. * For statistics only
  223. */
  224. if (*status & UART_LSR_BI) {
  225. *status &= ~(UART_LSR_FE | UART_LSR_PE);
  226. up->port.icount.brk++;
  227. /*
  228. * We do the SysRQ and SAK checking
  229. * here because otherwise the break
  230. * may get masked by ignore_status_mask
  231. * or read_status_mask.
  232. */
  233. if (uart_handle_break(&up->port))
  234. goto ignore_char;
  235. } else if (*status & UART_LSR_PE)
  236. up->port.icount.parity++;
  237. else if (*status & UART_LSR_FE)
  238. up->port.icount.frame++;
  239. if (*status & UART_LSR_OE)
  240. up->port.icount.overrun++;
  241. /*
  242. * Mask off conditions which should be ingored.
  243. */
  244. *status &= up->port.read_status_mask;
  245. if (*status & UART_LSR_BI) {
  246. pr_debug("handling break....\n");
  247. flag = TTY_BREAK;
  248. } else if (*status & UART_LSR_PE)
  249. flag = TTY_PARITY;
  250. else if (*status & UART_LSR_FE)
  251. flag = TTY_FRAME;
  252. }
  253. if (uart_handle_sysrq_char(&up->port, ch))
  254. goto ignore_char;
  255. if ((*status & up->port.ignore_status_mask) == 0)
  256. tty_insert_flip_char(port, ch, flag);
  257. if (*status & UART_LSR_OE) {
  258. /*
  259. * Overrun is special, since it's reported
  260. * immediately, and doesn't affect the current
  261. * character.
  262. */
  263. tty_insert_flip_char(port, 0, TTY_OVERRUN);
  264. }
  265. ignore_char:
  266. *status = serial_in(up, UART_LSR);
  267. } while ((*status & UART_LSR_DR) && (max_count-- > 0));
  268. spin_unlock(&up->port.lock);
  269. tty_flip_buffer_push(port);
  270. spin_lock(&up->port.lock);
  271. }
  272. static void transmit_chars(struct uart_sio_port *up)
  273. {
  274. struct circ_buf *xmit = &up->port.state->xmit;
  275. int count;
  276. if (up->port.x_char) {
  277. #ifndef CONFIG_SERIAL_M32R_PLDSIO /* XXX */
  278. serial_out(up, UART_TX, up->port.x_char);
  279. #endif
  280. up->port.icount.tx++;
  281. up->port.x_char = 0;
  282. return;
  283. }
  284. if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
  285. m32r_sio_stop_tx(&up->port);
  286. return;
  287. }
  288. count = up->port.fifosize;
  289. do {
  290. serial_out(up, UART_TX, xmit->buf[xmit->tail]);
  291. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  292. up->port.icount.tx++;
  293. if (uart_circ_empty(xmit))
  294. break;
  295. while (!(serial_in(up, UART_LSR) & UART_LSR_THRE));
  296. } while (--count > 0);
  297. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  298. uart_write_wakeup(&up->port);
  299. pr_debug("THRE...\n");
  300. if (uart_circ_empty(xmit))
  301. m32r_sio_stop_tx(&up->port);
  302. }
  303. /*
  304. * This handles the interrupt from one port.
  305. */
  306. static inline void m32r_sio_handle_port(struct uart_sio_port *up,
  307. unsigned int status)
  308. {
  309. pr_debug("status = %x...\n", status);
  310. if (status & 0x04)
  311. receive_chars(up, &status);
  312. if (status & 0x01)
  313. transmit_chars(up);
  314. }
  315. /*
  316. * This is the serial driver's interrupt routine.
  317. *
  318. * Arjan thinks the old way was overly complex, so it got simplified.
  319. * Alan disagrees, saying that need the complexity to handle the weird
  320. * nature of ISA shared interrupts. (This is a special exception.)
  321. *
  322. * In order to handle ISA shared interrupts properly, we need to check
  323. * that all ports have been serviced, and therefore the ISA interrupt
  324. * line has been de-asserted.
  325. *
  326. * This means we need to loop through all ports. checking that they
  327. * don't have an interrupt pending.
  328. */
  329. static irqreturn_t m32r_sio_interrupt(int irq, void *dev_id)
  330. {
  331. struct irq_info *i = dev_id;
  332. struct list_head *l, *end = NULL;
  333. int pass_counter = 0;
  334. pr_debug("m32r_sio_interrupt(%d)...\n", irq);
  335. #ifdef CONFIG_SERIAL_M32R_PLDSIO
  336. // if (irq == PLD_IRQ_SIO0_SND)
  337. // irq = PLD_IRQ_SIO0_RCV;
  338. #else
  339. if (irq == M32R_IRQ_SIO0_S)
  340. irq = M32R_IRQ_SIO0_R;
  341. #endif
  342. spin_lock(&i->lock);
  343. l = i->head;
  344. do {
  345. struct uart_sio_port *up;
  346. unsigned int sts;
  347. up = list_entry(l, struct uart_sio_port, list);
  348. sts = sio_in(up, SIOSTS);
  349. if (sts & 0x5) {
  350. spin_lock(&up->port.lock);
  351. m32r_sio_handle_port(up, sts);
  352. spin_unlock(&up->port.lock);
  353. end = NULL;
  354. } else if (end == NULL)
  355. end = l;
  356. l = l->next;
  357. if (l == i->head && pass_counter++ > PASS_LIMIT) {
  358. if (sts & 0xe0)
  359. sio_error(&sts);
  360. break;
  361. }
  362. } while (l != end);
  363. spin_unlock(&i->lock);
  364. pr_debug("end.\n");
  365. return IRQ_HANDLED;
  366. }
  367. /*
  368. * To support ISA shared interrupts, we need to have one interrupt
  369. * handler that ensures that the IRQ line has been deasserted
  370. * before returning. Failing to do this will result in the IRQ
  371. * line being stuck active, and, since ISA irqs are edge triggered,
  372. * no more IRQs will be seen.
  373. */
  374. static void serial_do_unlink(struct irq_info *i, struct uart_sio_port *up)
  375. {
  376. spin_lock_irq(&i->lock);
  377. if (!list_empty(i->head)) {
  378. if (i->head == &up->list)
  379. i->head = i->head->next;
  380. list_del(&up->list);
  381. } else {
  382. BUG_ON(i->head != &up->list);
  383. i->head = NULL;
  384. }
  385. spin_unlock_irq(&i->lock);
  386. }
  387. static int serial_link_irq_chain(struct uart_sio_port *up)
  388. {
  389. struct irq_info *i = irq_lists + up->port.irq;
  390. int ret, irq_flags = 0;
  391. spin_lock_irq(&i->lock);
  392. if (i->head) {
  393. list_add(&up->list, i->head);
  394. spin_unlock_irq(&i->lock);
  395. ret = 0;
  396. } else {
  397. INIT_LIST_HEAD(&up->list);
  398. i->head = &up->list;
  399. spin_unlock_irq(&i->lock);
  400. ret = request_irq(up->port.irq, m32r_sio_interrupt,
  401. irq_flags, "SIO0-RX", i);
  402. ret |= request_irq(up->port.irq + 1, m32r_sio_interrupt,
  403. irq_flags, "SIO0-TX", i);
  404. if (ret < 0)
  405. serial_do_unlink(i, up);
  406. }
  407. return ret;
  408. }
  409. static void serial_unlink_irq_chain(struct uart_sio_port *up)
  410. {
  411. struct irq_info *i = irq_lists + up->port.irq;
  412. BUG_ON(i->head == NULL);
  413. if (list_empty(i->head)) {
  414. free_irq(up->port.irq, i);
  415. free_irq(up->port.irq + 1, i);
  416. }
  417. serial_do_unlink(i, up);
  418. }
  419. /*
  420. * This function is used to handle ports that do not have an interrupt.
  421. */
  422. static void m32r_sio_timeout(struct timer_list *t)
  423. {
  424. struct uart_sio_port *up = from_timer(up, t, timer);
  425. unsigned int timeout;
  426. unsigned int sts;
  427. sts = sio_in(up, SIOSTS);
  428. if (sts & 0x5) {
  429. spin_lock(&up->port.lock);
  430. m32r_sio_handle_port(up, sts);
  431. spin_unlock(&up->port.lock);
  432. }
  433. timeout = up->port.timeout;
  434. timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
  435. mod_timer(&up->timer, jiffies + timeout);
  436. }
  437. static unsigned int m32r_sio_tx_empty(struct uart_port *port)
  438. {
  439. struct uart_sio_port *up =
  440. container_of(port, struct uart_sio_port, port);
  441. unsigned long flags;
  442. unsigned int ret;
  443. spin_lock_irqsave(&up->port.lock, flags);
  444. ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
  445. spin_unlock_irqrestore(&up->port.lock, flags);
  446. return ret;
  447. }
  448. static unsigned int m32r_sio_get_mctrl(struct uart_port *port)
  449. {
  450. return 0;
  451. }
  452. static void m32r_sio_set_mctrl(struct uart_port *port, unsigned int mctrl)
  453. {
  454. }
  455. static void m32r_sio_break_ctl(struct uart_port *port, int break_state)
  456. {
  457. }
  458. static int m32r_sio_startup(struct uart_port *port)
  459. {
  460. struct uart_sio_port *up =
  461. container_of(port, struct uart_sio_port, port);
  462. int retval;
  463. sio_init();
  464. /*
  465. * If the "interrupt" for this port doesn't correspond with any
  466. * hardware interrupt, we use a timer-based system. The original
  467. * driver used to do this with IRQ0.
  468. */
  469. if (!up->port.irq) {
  470. unsigned int timeout = up->port.timeout;
  471. timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
  472. mod_timer(&up->timer, jiffies + timeout);
  473. } else {
  474. retval = serial_link_irq_chain(up);
  475. if (retval)
  476. return retval;
  477. }
  478. /*
  479. * Finally, enable interrupts. Note: Modem status interrupts
  480. * are set via set_termios(), which will be occurring imminently
  481. * anyway, so we don't enable them here.
  482. * - M32R_SIO: 0x0c
  483. * - M32R_PLDSIO: 0x04
  484. */
  485. up->ier = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
  486. sio_out(up, SIOTRCR, up->ier);
  487. /*
  488. * And clear the interrupt registers again for luck.
  489. */
  490. sio_reset();
  491. return 0;
  492. }
  493. static void m32r_sio_shutdown(struct uart_port *port)
  494. {
  495. struct uart_sio_port *up =
  496. container_of(port, struct uart_sio_port, port);
  497. /*
  498. * Disable interrupts from this port
  499. */
  500. up->ier = 0;
  501. sio_out(up, SIOTRCR, 0);
  502. /*
  503. * Disable break condition and FIFOs
  504. */
  505. sio_init();
  506. if (!up->port.irq)
  507. del_timer_sync(&up->timer);
  508. else
  509. serial_unlink_irq_chain(up);
  510. }
  511. static unsigned int m32r_sio_get_divisor(struct uart_port *port,
  512. unsigned int baud)
  513. {
  514. return uart_get_divisor(port, baud);
  515. }
  516. static void m32r_sio_set_termios(struct uart_port *port,
  517. struct ktermios *termios, struct ktermios *old)
  518. {
  519. struct uart_sio_port *up =
  520. container_of(port, struct uart_sio_port, port);
  521. unsigned char cval = 0;
  522. unsigned long flags;
  523. unsigned int baud, quot;
  524. switch (termios->c_cflag & CSIZE) {
  525. case CS5:
  526. cval = UART_LCR_WLEN5;
  527. break;
  528. case CS6:
  529. cval = UART_LCR_WLEN6;
  530. break;
  531. case CS7:
  532. cval = UART_LCR_WLEN7;
  533. break;
  534. default:
  535. case CS8:
  536. cval = UART_LCR_WLEN8;
  537. break;
  538. }
  539. if (termios->c_cflag & CSTOPB)
  540. cval |= UART_LCR_STOP;
  541. if (termios->c_cflag & PARENB)
  542. cval |= UART_LCR_PARITY;
  543. if (!(termios->c_cflag & PARODD))
  544. cval |= UART_LCR_EPAR;
  545. #ifdef CMSPAR
  546. if (termios->c_cflag & CMSPAR)
  547. cval |= UART_LCR_SPAR;
  548. #endif
  549. /*
  550. * Ask the core to calculate the divisor for us.
  551. */
  552. #ifdef CONFIG_SERIAL_M32R_PLDSIO
  553. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/4);
  554. #else
  555. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  556. #endif
  557. quot = m32r_sio_get_divisor(port, baud);
  558. /*
  559. * Ok, we're now changing the port state. Do it with
  560. * interrupts disabled.
  561. */
  562. spin_lock_irqsave(&up->port.lock, flags);
  563. sio_set_baud_rate(baud);
  564. /*
  565. * Update the per-port timeout.
  566. */
  567. uart_update_timeout(port, termios->c_cflag, baud);
  568. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  569. if (termios->c_iflag & INPCK)
  570. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  571. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  572. up->port.read_status_mask |= UART_LSR_BI;
  573. /*
  574. * Characteres to ignore
  575. */
  576. up->port.ignore_status_mask = 0;
  577. if (termios->c_iflag & IGNPAR)
  578. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  579. if (termios->c_iflag & IGNBRK) {
  580. up->port.ignore_status_mask |= UART_LSR_BI;
  581. /*
  582. * If we're ignoring parity and break indicators,
  583. * ignore overruns too (for real raw support).
  584. */
  585. if (termios->c_iflag & IGNPAR)
  586. up->port.ignore_status_mask |= UART_LSR_OE;
  587. }
  588. /*
  589. * ignore all characters if CREAD is not set
  590. */
  591. if ((termios->c_cflag & CREAD) == 0)
  592. up->port.ignore_status_mask |= UART_LSR_DR;
  593. /*
  594. * CTS flow control flag and modem status interrupts
  595. */
  596. up->ier &= ~UART_IER_MSI;
  597. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  598. up->ier |= UART_IER_MSI;
  599. serial_out(up, UART_IER, up->ier);
  600. spin_unlock_irqrestore(&up->port.lock, flags);
  601. }
  602. /*
  603. * Resource handling. This is complicated by the fact that resources
  604. * depend on the port type. Maybe we should be claiming the standard
  605. * 8250 ports, and then trying to get other resources as necessary?
  606. */
  607. static int
  608. m32r_sio_request_std_resource(struct uart_sio_port *up, struct resource **res)
  609. {
  610. unsigned int size = 8 << up->port.regshift;
  611. #ifndef CONFIG_SERIAL_M32R_PLDSIO
  612. unsigned long start;
  613. #endif
  614. int ret = 0;
  615. switch (up->port.iotype) {
  616. case UPIO_MEM:
  617. if (up->port.mapbase) {
  618. #ifdef CONFIG_SERIAL_M32R_PLDSIO
  619. *res = request_mem_region(up->port.mapbase, size, "serial");
  620. #else
  621. start = up->port.mapbase;
  622. *res = request_mem_region(start, size, "serial");
  623. #endif
  624. if (!*res)
  625. ret = -EBUSY;
  626. }
  627. break;
  628. case UPIO_PORT:
  629. *res = request_region(up->port.iobase, size, "serial");
  630. if (!*res)
  631. ret = -EBUSY;
  632. break;
  633. }
  634. return ret;
  635. }
  636. static void m32r_sio_release_port(struct uart_port *port)
  637. {
  638. struct uart_sio_port *up =
  639. container_of(port, struct uart_sio_port, port);
  640. unsigned long start, offset = 0, size = 0;
  641. size <<= up->port.regshift;
  642. switch (up->port.iotype) {
  643. case UPIO_MEM:
  644. if (up->port.mapbase) {
  645. /*
  646. * Unmap the area.
  647. */
  648. iounmap(up->port.membase);
  649. up->port.membase = NULL;
  650. start = up->port.mapbase;
  651. if (size)
  652. release_mem_region(start + offset, size);
  653. release_mem_region(start, 8 << up->port.regshift);
  654. }
  655. break;
  656. case UPIO_PORT:
  657. start = up->port.iobase;
  658. if (size)
  659. release_region(start + offset, size);
  660. release_region(start + offset, 8 << up->port.regshift);
  661. break;
  662. default:
  663. break;
  664. }
  665. }
  666. static int m32r_sio_request_port(struct uart_port *port)
  667. {
  668. struct uart_sio_port *up =
  669. container_of(port, struct uart_sio_port, port);
  670. struct resource *res = NULL;
  671. int ret = 0;
  672. ret = m32r_sio_request_std_resource(up, &res);
  673. /*
  674. * If we have a mapbase, then request that as well.
  675. */
  676. if (ret == 0 && up->port.flags & UPF_IOREMAP) {
  677. int size = resource_size(res);
  678. up->port.membase = ioremap(up->port.mapbase, size);
  679. if (!up->port.membase)
  680. ret = -ENOMEM;
  681. }
  682. if (ret < 0) {
  683. if (res)
  684. release_resource(res);
  685. }
  686. return ret;
  687. }
  688. static void m32r_sio_config_port(struct uart_port *port, int unused)
  689. {
  690. struct uart_sio_port *up =
  691. container_of(port, struct uart_sio_port, port);
  692. unsigned long flags;
  693. spin_lock_irqsave(&up->port.lock, flags);
  694. up->port.fifosize = 1;
  695. spin_unlock_irqrestore(&up->port.lock, flags);
  696. }
  697. static int
  698. m32r_sio_verify_port(struct uart_port *port, struct serial_struct *ser)
  699. {
  700. if (ser->irq >= nr_irqs || ser->irq < 0 || ser->baud_base < 9600)
  701. return -EINVAL;
  702. return 0;
  703. }
  704. static const struct uart_ops m32r_sio_pops = {
  705. .tx_empty = m32r_sio_tx_empty,
  706. .set_mctrl = m32r_sio_set_mctrl,
  707. .get_mctrl = m32r_sio_get_mctrl,
  708. .stop_tx = m32r_sio_stop_tx,
  709. .start_tx = m32r_sio_start_tx,
  710. .stop_rx = m32r_sio_stop_rx,
  711. .enable_ms = m32r_sio_enable_ms,
  712. .break_ctl = m32r_sio_break_ctl,
  713. .startup = m32r_sio_startup,
  714. .shutdown = m32r_sio_shutdown,
  715. .set_termios = m32r_sio_set_termios,
  716. .release_port = m32r_sio_release_port,
  717. .request_port = m32r_sio_request_port,
  718. .config_port = m32r_sio_config_port,
  719. .verify_port = m32r_sio_verify_port,
  720. };
  721. static struct uart_sio_port m32r_sio_ports[UART_NR];
  722. static void __init m32r_sio_init_ports(void)
  723. {
  724. struct uart_sio_port *up;
  725. static int first = 1;
  726. int i;
  727. if (!first)
  728. return;
  729. first = 0;
  730. for (i = 0, up = m32r_sio_ports; i < UART_NR; i++, up++) {
  731. up->port.iobase = old_serial_port[i].port;
  732. up->port.irq = irq_canonicalize(old_serial_port[i].irq);
  733. up->port.uartclk = BAUD_RATE * 16;
  734. up->port.flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST;
  735. up->port.membase = 0;
  736. up->port.iotype = 0;
  737. up->port.regshift = 0;
  738. up->port.ops = &m32r_sio_pops;
  739. }
  740. }
  741. static void __init m32r_sio_register_ports(struct uart_driver *drv)
  742. {
  743. int i;
  744. m32r_sio_init_ports();
  745. for (i = 0; i < UART_NR; i++) {
  746. struct uart_sio_port *up = &m32r_sio_ports[i];
  747. up->port.line = i;
  748. up->port.ops = &m32r_sio_pops;
  749. timer_setup(&up->timer, m32r_sio_timeout, 0);
  750. uart_add_one_port(drv, &up->port);
  751. }
  752. }
  753. #ifdef CONFIG_SERIAL_M32R_SIO_CONSOLE
  754. /*
  755. * Wait for transmitter & holding register to empty
  756. */
  757. static void wait_for_xmitr(struct uart_sio_port *up)
  758. {
  759. unsigned int status, tmout = 10000;
  760. /* Wait up to 10ms for the character(s) to be sent. */
  761. do {
  762. status = sio_in(up, SIOSTS);
  763. if (--tmout == 0)
  764. break;
  765. udelay(1);
  766. } while ((status & UART_EMPTY) != UART_EMPTY);
  767. /* Wait up to 1s for flow control if necessary */
  768. if (up->port.flags & UPF_CONS_FLOW) {
  769. tmout = 1000000;
  770. while (--tmout)
  771. udelay(1);
  772. }
  773. }
  774. static void m32r_sio_console_putchar(struct uart_port *port, int ch)
  775. {
  776. struct uart_sio_port *up =
  777. container_of(port, struct uart_sio_port, port);
  778. wait_for_xmitr(up);
  779. sio_out(up, SIOTXB, ch);
  780. }
  781. /*
  782. * Print a string to the serial port trying not to disturb
  783. * any possible real use of the port...
  784. *
  785. * The console_lock must be held when we get here.
  786. */
  787. static void m32r_sio_console_write(struct console *co, const char *s,
  788. unsigned int count)
  789. {
  790. struct uart_sio_port *up = &m32r_sio_ports[co->index];
  791. unsigned int ier;
  792. /*
  793. * First save the UER then disable the interrupts
  794. */
  795. ier = sio_in(up, SIOTRCR);
  796. sio_out(up, SIOTRCR, 0);
  797. uart_console_write(&up->port, s, count, m32r_sio_console_putchar);
  798. /*
  799. * Finally, wait for transmitter to become empty
  800. * and restore the IER
  801. */
  802. wait_for_xmitr(up);
  803. sio_out(up, SIOTRCR, ier);
  804. }
  805. static int __init m32r_sio_console_setup(struct console *co, char *options)
  806. {
  807. struct uart_port *port;
  808. int baud = 9600;
  809. int bits = 8;
  810. int parity = 'n';
  811. int flow = 'n';
  812. /*
  813. * Check whether an invalid uart number has been specified, and
  814. * if so, search for the first available port that does have
  815. * console support.
  816. */
  817. if (co->index >= UART_NR)
  818. co->index = 0;
  819. port = &m32r_sio_ports[co->index].port;
  820. /*
  821. * Temporary fix.
  822. */
  823. spin_lock_init(&port->lock);
  824. if (options)
  825. uart_parse_options(options, &baud, &parity, &bits, &flow);
  826. return uart_set_options(port, co, baud, parity, bits, flow);
  827. }
  828. static struct uart_driver m32r_sio_reg;
  829. static struct console m32r_sio_console = {
  830. .name = "ttyS",
  831. .write = m32r_sio_console_write,
  832. .device = uart_console_device,
  833. .setup = m32r_sio_console_setup,
  834. .flags = CON_PRINTBUFFER,
  835. .index = -1,
  836. .data = &m32r_sio_reg,
  837. };
  838. static int __init m32r_sio_console_init(void)
  839. {
  840. sio_reset();
  841. sio_init();
  842. m32r_sio_init_ports();
  843. register_console(&m32r_sio_console);
  844. return 0;
  845. }
  846. console_initcall(m32r_sio_console_init);
  847. #define M32R_SIO_CONSOLE &m32r_sio_console
  848. #else
  849. #define M32R_SIO_CONSOLE NULL
  850. #endif
  851. static struct uart_driver m32r_sio_reg = {
  852. .owner = THIS_MODULE,
  853. .driver_name = "sio",
  854. .dev_name = "ttyS",
  855. .major = TTY_MAJOR,
  856. .minor = 64,
  857. .nr = UART_NR,
  858. .cons = M32R_SIO_CONSOLE,
  859. };
  860. static int __init m32r_sio_init(void)
  861. {
  862. int ret, i;
  863. printk(KERN_INFO "Serial: M32R SIO driver\n");
  864. for (i = 0; i < nr_irqs; i++)
  865. spin_lock_init(&irq_lists[i].lock);
  866. ret = uart_register_driver(&m32r_sio_reg);
  867. if (ret >= 0)
  868. m32r_sio_register_ports(&m32r_sio_reg);
  869. return ret;
  870. }
  871. device_initcall(m32r_sio_init);