m32r_sio.c 27 KB

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