m32r_sio.c 25 KB

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