m32r_sio.c 26 KB

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