m32r_sio.c 27 KB

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