m32r_sio.c 27 KB

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