m32r_sio.c 26 KB

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