m32r_sio.c 27 KB

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