m32r_sio.c 28 KB

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