m32r_sio.c 26 KB

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