m32r_sio.c 24 KB

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