m32r_sio.c 26 KB

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