m32r_sio.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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(struct timer_list *t)
  427. {
  428. struct uart_sio_port *up = from_timer(up, t, timer);
  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. mod_timer(&up->timer, jiffies + timeout);
  477. } else {
  478. retval = serial_link_irq_chain(up);
  479. if (retval)
  480. return retval;
  481. }
  482. /*
  483. * Finally, enable interrupts. Note: Modem status interrupts
  484. * are set via set_termios(), which will be occurring imminently
  485. * anyway, so we don't enable them here.
  486. * - M32R_SIO: 0x0c
  487. * - M32R_PLDSIO: 0x04
  488. */
  489. up->ier = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
  490. sio_out(up, SIOTRCR, up->ier);
  491. /*
  492. * And clear the interrupt registers again for luck.
  493. */
  494. sio_reset();
  495. return 0;
  496. }
  497. static void m32r_sio_shutdown(struct uart_port *port)
  498. {
  499. struct uart_sio_port *up =
  500. container_of(port, struct uart_sio_port, port);
  501. /*
  502. * Disable interrupts from this port
  503. */
  504. up->ier = 0;
  505. sio_out(up, SIOTRCR, 0);
  506. /*
  507. * Disable break condition and FIFOs
  508. */
  509. sio_init();
  510. if (!up->port.irq)
  511. del_timer_sync(&up->timer);
  512. else
  513. serial_unlink_irq_chain(up);
  514. }
  515. static unsigned int m32r_sio_get_divisor(struct uart_port *port,
  516. unsigned int baud)
  517. {
  518. return uart_get_divisor(port, baud);
  519. }
  520. static void m32r_sio_set_termios(struct uart_port *port,
  521. struct ktermios *termios, struct ktermios *old)
  522. {
  523. struct uart_sio_port *up =
  524. container_of(port, struct uart_sio_port, port);
  525. unsigned char cval = 0;
  526. unsigned long flags;
  527. unsigned int baud, quot;
  528. switch (termios->c_cflag & CSIZE) {
  529. case CS5:
  530. cval = UART_LCR_WLEN5;
  531. break;
  532. case CS6:
  533. cval = UART_LCR_WLEN6;
  534. break;
  535. case CS7:
  536. cval = UART_LCR_WLEN7;
  537. break;
  538. default:
  539. case CS8:
  540. cval = UART_LCR_WLEN8;
  541. break;
  542. }
  543. if (termios->c_cflag & CSTOPB)
  544. cval |= UART_LCR_STOP;
  545. if (termios->c_cflag & PARENB)
  546. cval |= UART_LCR_PARITY;
  547. if (!(termios->c_cflag & PARODD))
  548. cval |= UART_LCR_EPAR;
  549. #ifdef CMSPAR
  550. if (termios->c_cflag & CMSPAR)
  551. cval |= UART_LCR_SPAR;
  552. #endif
  553. /*
  554. * Ask the core to calculate the divisor for us.
  555. */
  556. #ifdef CONFIG_SERIAL_M32R_PLDSIO
  557. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/4);
  558. #else
  559. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  560. #endif
  561. quot = m32r_sio_get_divisor(port, baud);
  562. /*
  563. * Ok, we're now changing the port state. Do it with
  564. * interrupts disabled.
  565. */
  566. spin_lock_irqsave(&up->port.lock, flags);
  567. sio_set_baud_rate(baud);
  568. /*
  569. * Update the per-port timeout.
  570. */
  571. uart_update_timeout(port, termios->c_cflag, baud);
  572. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  573. if (termios->c_iflag & INPCK)
  574. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  575. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  576. up->port.read_status_mask |= UART_LSR_BI;
  577. /*
  578. * Characteres to ignore
  579. */
  580. up->port.ignore_status_mask = 0;
  581. if (termios->c_iflag & IGNPAR)
  582. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  583. if (termios->c_iflag & IGNBRK) {
  584. up->port.ignore_status_mask |= UART_LSR_BI;
  585. /*
  586. * If we're ignoring parity and break indicators,
  587. * ignore overruns too (for real raw support).
  588. */
  589. if (termios->c_iflag & IGNPAR)
  590. up->port.ignore_status_mask |= UART_LSR_OE;
  591. }
  592. /*
  593. * ignore all characters if CREAD is not set
  594. */
  595. if ((termios->c_cflag & CREAD) == 0)
  596. up->port.ignore_status_mask |= UART_LSR_DR;
  597. /*
  598. * CTS flow control flag and modem status interrupts
  599. */
  600. up->ier &= ~UART_IER_MSI;
  601. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  602. up->ier |= UART_IER_MSI;
  603. serial_out(up, UART_IER, up->ier);
  604. spin_unlock_irqrestore(&up->port.lock, flags);
  605. }
  606. /*
  607. * Resource handling. This is complicated by the fact that resources
  608. * depend on the port type. Maybe we should be claiming the standard
  609. * 8250 ports, and then trying to get other resources as necessary?
  610. */
  611. static int
  612. m32r_sio_request_std_resource(struct uart_sio_port *up, struct resource **res)
  613. {
  614. unsigned int size = 8 << up->port.regshift;
  615. #ifndef CONFIG_SERIAL_M32R_PLDSIO
  616. unsigned long start;
  617. #endif
  618. int ret = 0;
  619. switch (up->port.iotype) {
  620. case UPIO_MEM:
  621. if (up->port.mapbase) {
  622. #ifdef CONFIG_SERIAL_M32R_PLDSIO
  623. *res = request_mem_region(up->port.mapbase, size, "serial");
  624. #else
  625. start = up->port.mapbase;
  626. *res = request_mem_region(start, size, "serial");
  627. #endif
  628. if (!*res)
  629. ret = -EBUSY;
  630. }
  631. break;
  632. case UPIO_PORT:
  633. *res = request_region(up->port.iobase, size, "serial");
  634. if (!*res)
  635. ret = -EBUSY;
  636. break;
  637. }
  638. return ret;
  639. }
  640. static void m32r_sio_release_port(struct uart_port *port)
  641. {
  642. struct uart_sio_port *up =
  643. container_of(port, struct uart_sio_port, port);
  644. unsigned long start, offset = 0, size = 0;
  645. size <<= up->port.regshift;
  646. switch (up->port.iotype) {
  647. case UPIO_MEM:
  648. if (up->port.mapbase) {
  649. /*
  650. * Unmap the area.
  651. */
  652. iounmap(up->port.membase);
  653. up->port.membase = NULL;
  654. start = up->port.mapbase;
  655. if (size)
  656. release_mem_region(start + offset, size);
  657. release_mem_region(start, 8 << up->port.regshift);
  658. }
  659. break;
  660. case UPIO_PORT:
  661. start = up->port.iobase;
  662. if (size)
  663. release_region(start + offset, size);
  664. release_region(start + offset, 8 << up->port.regshift);
  665. break;
  666. default:
  667. break;
  668. }
  669. }
  670. static int m32r_sio_request_port(struct uart_port *port)
  671. {
  672. struct uart_sio_port *up =
  673. container_of(port, struct uart_sio_port, port);
  674. struct resource *res = NULL;
  675. int ret = 0;
  676. ret = m32r_sio_request_std_resource(up, &res);
  677. /*
  678. * If we have a mapbase, then request that as well.
  679. */
  680. if (ret == 0 && up->port.flags & UPF_IOREMAP) {
  681. int size = resource_size(res);
  682. up->port.membase = ioremap(up->port.mapbase, size);
  683. if (!up->port.membase)
  684. ret = -ENOMEM;
  685. }
  686. if (ret < 0) {
  687. if (res)
  688. release_resource(res);
  689. }
  690. return ret;
  691. }
  692. static void m32r_sio_config_port(struct uart_port *port, int unused)
  693. {
  694. struct uart_sio_port *up =
  695. container_of(port, struct uart_sio_port, port);
  696. unsigned long flags;
  697. spin_lock_irqsave(&up->port.lock, flags);
  698. up->port.fifosize = 1;
  699. spin_unlock_irqrestore(&up->port.lock, flags);
  700. }
  701. static int
  702. m32r_sio_verify_port(struct uart_port *port, struct serial_struct *ser)
  703. {
  704. if (ser->irq >= nr_irqs || ser->irq < 0 || ser->baud_base < 9600)
  705. return -EINVAL;
  706. return 0;
  707. }
  708. static const struct uart_ops m32r_sio_pops = {
  709. .tx_empty = m32r_sio_tx_empty,
  710. .set_mctrl = m32r_sio_set_mctrl,
  711. .get_mctrl = m32r_sio_get_mctrl,
  712. .stop_tx = m32r_sio_stop_tx,
  713. .start_tx = m32r_sio_start_tx,
  714. .stop_rx = m32r_sio_stop_rx,
  715. .enable_ms = m32r_sio_enable_ms,
  716. .break_ctl = m32r_sio_break_ctl,
  717. .startup = m32r_sio_startup,
  718. .shutdown = m32r_sio_shutdown,
  719. .set_termios = m32r_sio_set_termios,
  720. .release_port = m32r_sio_release_port,
  721. .request_port = m32r_sio_request_port,
  722. .config_port = m32r_sio_config_port,
  723. .verify_port = m32r_sio_verify_port,
  724. };
  725. static struct uart_sio_port m32r_sio_ports[UART_NR];
  726. static void __init m32r_sio_init_ports(void)
  727. {
  728. struct uart_sio_port *up;
  729. static int first = 1;
  730. int i;
  731. if (!first)
  732. return;
  733. first = 0;
  734. for (i = 0, up = m32r_sio_ports; i < UART_NR; i++, up++) {
  735. up->port.iobase = old_serial_port[i].port;
  736. up->port.irq = irq_canonicalize(old_serial_port[i].irq);
  737. up->port.uartclk = BAUD_RATE * 16;
  738. up->port.flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST;
  739. up->port.membase = 0;
  740. up->port.iotype = 0;
  741. up->port.regshift = 0;
  742. up->port.ops = &m32r_sio_pops;
  743. }
  744. }
  745. static void __init m32r_sio_register_ports(struct uart_driver *drv)
  746. {
  747. int i;
  748. m32r_sio_init_ports();
  749. for (i = 0; i < UART_NR; i++) {
  750. struct uart_sio_port *up = &m32r_sio_ports[i];
  751. up->port.line = i;
  752. up->port.ops = &m32r_sio_pops;
  753. timer_setup(&up->timer, m32r_sio_timeout, 0);
  754. uart_add_one_port(drv, &up->port);
  755. }
  756. }
  757. #ifdef CONFIG_SERIAL_M32R_SIO_CONSOLE
  758. /*
  759. * Wait for transmitter & holding register to empty
  760. */
  761. static void wait_for_xmitr(struct uart_sio_port *up)
  762. {
  763. unsigned int status, tmout = 10000;
  764. /* Wait up to 10ms for the character(s) to be sent. */
  765. do {
  766. status = sio_in(up, SIOSTS);
  767. if (--tmout == 0)
  768. break;
  769. udelay(1);
  770. } while ((status & UART_EMPTY) != UART_EMPTY);
  771. /* Wait up to 1s for flow control if necessary */
  772. if (up->port.flags & UPF_CONS_FLOW) {
  773. tmout = 1000000;
  774. while (--tmout)
  775. udelay(1);
  776. }
  777. }
  778. static void m32r_sio_console_putchar(struct uart_port *port, int ch)
  779. {
  780. struct uart_sio_port *up =
  781. container_of(port, struct uart_sio_port, port);
  782. wait_for_xmitr(up);
  783. sio_out(up, SIOTXB, ch);
  784. }
  785. /*
  786. * Print a string to the serial port trying not to disturb
  787. * any possible real use of the port...
  788. *
  789. * The console_lock must be held when we get here.
  790. */
  791. static void m32r_sio_console_write(struct console *co, const char *s,
  792. unsigned int count)
  793. {
  794. struct uart_sio_port *up = &m32r_sio_ports[co->index];
  795. unsigned int ier;
  796. /*
  797. * First save the UER then disable the interrupts
  798. */
  799. ier = sio_in(up, SIOTRCR);
  800. sio_out(up, SIOTRCR, 0);
  801. uart_console_write(&up->port, s, count, m32r_sio_console_putchar);
  802. /*
  803. * Finally, wait for transmitter to become empty
  804. * and restore the IER
  805. */
  806. wait_for_xmitr(up);
  807. sio_out(up, SIOTRCR, ier);
  808. }
  809. static int __init m32r_sio_console_setup(struct console *co, char *options)
  810. {
  811. struct uart_port *port;
  812. int baud = 9600;
  813. int bits = 8;
  814. int parity = 'n';
  815. int flow = 'n';
  816. /*
  817. * Check whether an invalid uart number has been specified, and
  818. * if so, search for the first available port that does have
  819. * console support.
  820. */
  821. if (co->index >= UART_NR)
  822. co->index = 0;
  823. port = &m32r_sio_ports[co->index].port;
  824. /*
  825. * Temporary fix.
  826. */
  827. spin_lock_init(&port->lock);
  828. if (options)
  829. uart_parse_options(options, &baud, &parity, &bits, &flow);
  830. return uart_set_options(port, co, baud, parity, bits, flow);
  831. }
  832. static struct uart_driver m32r_sio_reg;
  833. static struct console m32r_sio_console = {
  834. .name = "ttyS",
  835. .write = m32r_sio_console_write,
  836. .device = uart_console_device,
  837. .setup = m32r_sio_console_setup,
  838. .flags = CON_PRINTBUFFER,
  839. .index = -1,
  840. .data = &m32r_sio_reg,
  841. };
  842. static int __init m32r_sio_console_init(void)
  843. {
  844. sio_reset();
  845. sio_init();
  846. m32r_sio_init_ports();
  847. register_console(&m32r_sio_console);
  848. return 0;
  849. }
  850. console_initcall(m32r_sio_console_init);
  851. #define M32R_SIO_CONSOLE &m32r_sio_console
  852. #else
  853. #define M32R_SIO_CONSOLE NULL
  854. #endif
  855. static struct uart_driver m32r_sio_reg = {
  856. .owner = THIS_MODULE,
  857. .driver_name = "sio",
  858. .dev_name = "ttyS",
  859. .major = TTY_MAJOR,
  860. .minor = 64,
  861. .nr = UART_NR,
  862. .cons = M32R_SIO_CONSOLE,
  863. };
  864. static int __init m32r_sio_init(void)
  865. {
  866. int ret, i;
  867. printk(KERN_INFO "Serial: M32R SIO driver\n");
  868. for (i = 0; i < nr_irqs; i++)
  869. spin_lock_init(&irq_lists[i].lock);
  870. ret = uart_register_driver(&m32r_sio_reg);
  871. if (ret >= 0)
  872. m32r_sio_register_ports(&m32r_sio_reg);
  873. return ret;
  874. }
  875. device_initcall(m32r_sio_init);