m32r_sio.c 26 KB

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