m32r_sio.c 24 KB

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