m32r_sio.c 27 KB

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