m32r_sio.c 27 KB

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