sa1100.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*
  2. * linux/drivers/char/sa1100.c
  3. *
  4. * Driver for SA11x0 serial ports
  5. *
  6. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  7. *
  8. * Copyright (C) 2000 Deep Blue Solutions Ltd.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * $Id: sa1100.c,v 1.50 2002/07/29 14:41:04 rmk Exp $
  25. *
  26. */
  27. #include <linux/config.h>
  28. #if defined(CONFIG_SERIAL_SA1100_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  29. #define SUPPORT_SYSRQ
  30. #endif
  31. #include <linux/module.h>
  32. #include <linux/ioport.h>
  33. #include <linux/init.h>
  34. #include <linux/console.h>
  35. #include <linux/sysrq.h>
  36. #include <linux/device.h>
  37. #include <linux/tty.h>
  38. #include <linux/tty_flip.h>
  39. #include <linux/serial_core.h>
  40. #include <linux/serial.h>
  41. #include <asm/io.h>
  42. #include <asm/irq.h>
  43. #include <asm/hardware.h>
  44. #include <asm/mach/serial_sa1100.h>
  45. /* We've been assigned a range on the "Low-density serial ports" major */
  46. #define SERIAL_SA1100_MAJOR 204
  47. #define MINOR_START 5
  48. #define NR_PORTS 3
  49. #define SA1100_ISR_PASS_LIMIT 256
  50. /*
  51. * Convert from ignore_status_mask or read_status_mask to UTSR[01]
  52. */
  53. #define SM_TO_UTSR0(x) ((x) & 0xff)
  54. #define SM_TO_UTSR1(x) ((x) >> 8)
  55. #define UTSR0_TO_SM(x) ((x))
  56. #define UTSR1_TO_SM(x) ((x) << 8)
  57. #define UART_GET_UTCR0(sport) __raw_readl((sport)->port.membase + UTCR0)
  58. #define UART_GET_UTCR1(sport) __raw_readl((sport)->port.membase + UTCR1)
  59. #define UART_GET_UTCR2(sport) __raw_readl((sport)->port.membase + UTCR2)
  60. #define UART_GET_UTCR3(sport) __raw_readl((sport)->port.membase + UTCR3)
  61. #define UART_GET_UTSR0(sport) __raw_readl((sport)->port.membase + UTSR0)
  62. #define UART_GET_UTSR1(sport) __raw_readl((sport)->port.membase + UTSR1)
  63. #define UART_GET_CHAR(sport) __raw_readl((sport)->port.membase + UTDR)
  64. #define UART_PUT_UTCR0(sport,v) __raw_writel((v),(sport)->port.membase + UTCR0)
  65. #define UART_PUT_UTCR1(sport,v) __raw_writel((v),(sport)->port.membase + UTCR1)
  66. #define UART_PUT_UTCR2(sport,v) __raw_writel((v),(sport)->port.membase + UTCR2)
  67. #define UART_PUT_UTCR3(sport,v) __raw_writel((v),(sport)->port.membase + UTCR3)
  68. #define UART_PUT_UTSR0(sport,v) __raw_writel((v),(sport)->port.membase + UTSR0)
  69. #define UART_PUT_UTSR1(sport,v) __raw_writel((v),(sport)->port.membase + UTSR1)
  70. #define UART_PUT_CHAR(sport,v) __raw_writel((v),(sport)->port.membase + UTDR)
  71. /*
  72. * This is the size of our serial port register set.
  73. */
  74. #define UART_PORT_SIZE 0x24
  75. /*
  76. * This determines how often we check the modem status signals
  77. * for any change. They generally aren't connected to an IRQ
  78. * so we have to poll them. We also check immediately before
  79. * filling the TX fifo incase CTS has been dropped.
  80. */
  81. #define MCTRL_TIMEOUT (250*HZ/1000)
  82. struct sa1100_port {
  83. struct uart_port port;
  84. struct timer_list timer;
  85. unsigned int old_status;
  86. };
  87. /*
  88. * Handle any change of modem status signal since we were last called.
  89. */
  90. static void sa1100_mctrl_check(struct sa1100_port *sport)
  91. {
  92. unsigned int status, changed;
  93. status = sport->port.ops->get_mctrl(&sport->port);
  94. changed = status ^ sport->old_status;
  95. if (changed == 0)
  96. return;
  97. sport->old_status = status;
  98. if (changed & TIOCM_RI)
  99. sport->port.icount.rng++;
  100. if (changed & TIOCM_DSR)
  101. sport->port.icount.dsr++;
  102. if (changed & TIOCM_CAR)
  103. uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
  104. if (changed & TIOCM_CTS)
  105. uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
  106. wake_up_interruptible(&sport->port.info->delta_msr_wait);
  107. }
  108. /*
  109. * This is our per-port timeout handler, for checking the
  110. * modem status signals.
  111. */
  112. static void sa1100_timeout(unsigned long data)
  113. {
  114. struct sa1100_port *sport = (struct sa1100_port *)data;
  115. unsigned long flags;
  116. if (sport->port.info) {
  117. spin_lock_irqsave(&sport->port.lock, flags);
  118. sa1100_mctrl_check(sport);
  119. spin_unlock_irqrestore(&sport->port.lock, flags);
  120. mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
  121. }
  122. }
  123. /*
  124. * interrupts disabled on entry
  125. */
  126. static void sa1100_stop_tx(struct uart_port *port, unsigned int tty_stop)
  127. {
  128. struct sa1100_port *sport = (struct sa1100_port *)port;
  129. u32 utcr3;
  130. utcr3 = UART_GET_UTCR3(sport);
  131. UART_PUT_UTCR3(sport, utcr3 & ~UTCR3_TIE);
  132. sport->port.read_status_mask &= ~UTSR0_TO_SM(UTSR0_TFS);
  133. }
  134. /*
  135. * interrupts may not be disabled on entry
  136. */
  137. static void sa1100_start_tx(struct uart_port *port, unsigned int tty_start)
  138. {
  139. struct sa1100_port *sport = (struct sa1100_port *)port;
  140. unsigned long flags;
  141. u32 utcr3;
  142. spin_lock_irqsave(&sport->port.lock, flags);
  143. utcr3 = UART_GET_UTCR3(sport);
  144. sport->port.read_status_mask |= UTSR0_TO_SM(UTSR0_TFS);
  145. UART_PUT_UTCR3(sport, utcr3 | UTCR3_TIE);
  146. spin_unlock_irqrestore(&sport->port.lock, flags);
  147. }
  148. /*
  149. * Interrupts enabled
  150. */
  151. static void sa1100_stop_rx(struct uart_port *port)
  152. {
  153. struct sa1100_port *sport = (struct sa1100_port *)port;
  154. u32 utcr3;
  155. utcr3 = UART_GET_UTCR3(sport);
  156. UART_PUT_UTCR3(sport, utcr3 & ~UTCR3_RIE);
  157. }
  158. /*
  159. * Set the modem control timer to fire immediately.
  160. */
  161. static void sa1100_enable_ms(struct uart_port *port)
  162. {
  163. struct sa1100_port *sport = (struct sa1100_port *)port;
  164. mod_timer(&sport->timer, jiffies);
  165. }
  166. static void
  167. sa1100_rx_chars(struct sa1100_port *sport, struct pt_regs *regs)
  168. {
  169. struct tty_struct *tty = sport->port.info->tty;
  170. unsigned int status, ch, flg, ignored = 0;
  171. status = UTSR1_TO_SM(UART_GET_UTSR1(sport)) |
  172. UTSR0_TO_SM(UART_GET_UTSR0(sport));
  173. while (status & UTSR1_TO_SM(UTSR1_RNE)) {
  174. ch = UART_GET_CHAR(sport);
  175. if (tty->flip.count >= TTY_FLIPBUF_SIZE)
  176. goto ignore_char;
  177. sport->port.icount.rx++;
  178. flg = TTY_NORMAL;
  179. /*
  180. * note that the error handling code is
  181. * out of the main execution path
  182. */
  183. if (status & UTSR1_TO_SM(UTSR1_PRE | UTSR1_FRE | UTSR1_ROR))
  184. goto handle_error;
  185. if (uart_handle_sysrq_char(&sport->port, ch, regs))
  186. goto ignore_char;
  187. error_return:
  188. tty_insert_flip_char(tty, ch, flg);
  189. ignore_char:
  190. status = UTSR1_TO_SM(UART_GET_UTSR1(sport)) |
  191. UTSR0_TO_SM(UART_GET_UTSR0(sport));
  192. }
  193. out:
  194. tty_flip_buffer_push(tty);
  195. return;
  196. handle_error:
  197. if (status & UTSR1_TO_SM(UTSR1_PRE))
  198. sport->port.icount.parity++;
  199. else if (status & UTSR1_TO_SM(UTSR1_FRE))
  200. sport->port.icount.frame++;
  201. if (status & UTSR1_TO_SM(UTSR1_ROR))
  202. sport->port.icount.overrun++;
  203. if (status & sport->port.ignore_status_mask) {
  204. if (++ignored > 100)
  205. goto out;
  206. goto ignore_char;
  207. }
  208. status &= sport->port.read_status_mask;
  209. if (status & UTSR1_TO_SM(UTSR1_PRE))
  210. flg = TTY_PARITY;
  211. else if (status & UTSR1_TO_SM(UTSR1_FRE))
  212. flg = TTY_FRAME;
  213. if (status & UTSR1_TO_SM(UTSR1_ROR)) {
  214. /*
  215. * overrun does *not* affect the character
  216. * we read from the FIFO
  217. */
  218. tty_insert_flip_char(tty, ch, flg);
  219. ch = 0;
  220. flg = TTY_OVERRUN;
  221. }
  222. #ifdef SUPPORT_SYSRQ
  223. sport->port.sysrq = 0;
  224. #endif
  225. goto error_return;
  226. }
  227. static void sa1100_tx_chars(struct sa1100_port *sport)
  228. {
  229. struct circ_buf *xmit = &sport->port.info->xmit;
  230. if (sport->port.x_char) {
  231. UART_PUT_CHAR(sport, sport->port.x_char);
  232. sport->port.icount.tx++;
  233. sport->port.x_char = 0;
  234. return;
  235. }
  236. /*
  237. * Check the modem control lines before
  238. * transmitting anything.
  239. */
  240. sa1100_mctrl_check(sport);
  241. if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
  242. sa1100_stop_tx(&sport->port, 0);
  243. return;
  244. }
  245. /*
  246. * Tried using FIFO (not checking TNF) for fifo fill:
  247. * still had the '4 bytes repeated' problem.
  248. */
  249. while (UART_GET_UTSR1(sport) & UTSR1_TNF) {
  250. UART_PUT_CHAR(sport, xmit->buf[xmit->tail]);
  251. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  252. sport->port.icount.tx++;
  253. if (uart_circ_empty(xmit))
  254. break;
  255. }
  256. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  257. uart_write_wakeup(&sport->port);
  258. if (uart_circ_empty(xmit))
  259. sa1100_stop_tx(&sport->port, 0);
  260. }
  261. static irqreturn_t sa1100_int(int irq, void *dev_id, struct pt_regs *regs)
  262. {
  263. struct sa1100_port *sport = dev_id;
  264. unsigned int status, pass_counter = 0;
  265. spin_lock(&sport->port.lock);
  266. status = UART_GET_UTSR0(sport);
  267. status &= SM_TO_UTSR0(sport->port.read_status_mask) | ~UTSR0_TFS;
  268. do {
  269. if (status & (UTSR0_RFS | UTSR0_RID)) {
  270. /* Clear the receiver idle bit, if set */
  271. if (status & UTSR0_RID)
  272. UART_PUT_UTSR0(sport, UTSR0_RID);
  273. sa1100_rx_chars(sport, regs);
  274. }
  275. /* Clear the relevant break bits */
  276. if (status & (UTSR0_RBB | UTSR0_REB))
  277. UART_PUT_UTSR0(sport, status & (UTSR0_RBB | UTSR0_REB));
  278. if (status & UTSR0_RBB)
  279. sport->port.icount.brk++;
  280. if (status & UTSR0_REB)
  281. uart_handle_break(&sport->port);
  282. if (status & UTSR0_TFS)
  283. sa1100_tx_chars(sport);
  284. if (pass_counter++ > SA1100_ISR_PASS_LIMIT)
  285. break;
  286. status = UART_GET_UTSR0(sport);
  287. status &= SM_TO_UTSR0(sport->port.read_status_mask) |
  288. ~UTSR0_TFS;
  289. } while (status & (UTSR0_TFS | UTSR0_RFS | UTSR0_RID));
  290. spin_unlock(&sport->port.lock);
  291. return IRQ_HANDLED;
  292. }
  293. /*
  294. * Return TIOCSER_TEMT when transmitter is not busy.
  295. */
  296. static unsigned int sa1100_tx_empty(struct uart_port *port)
  297. {
  298. struct sa1100_port *sport = (struct sa1100_port *)port;
  299. return UART_GET_UTSR1(sport) & UTSR1_TBY ? 0 : TIOCSER_TEMT;
  300. }
  301. static unsigned int sa1100_get_mctrl(struct uart_port *port)
  302. {
  303. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  304. }
  305. static void sa1100_set_mctrl(struct uart_port *port, unsigned int mctrl)
  306. {
  307. }
  308. /*
  309. * Interrupts always disabled.
  310. */
  311. static void sa1100_break_ctl(struct uart_port *port, int break_state)
  312. {
  313. struct sa1100_port *sport = (struct sa1100_port *)port;
  314. unsigned long flags;
  315. unsigned int utcr3;
  316. spin_lock_irqsave(&sport->port.lock, flags);
  317. utcr3 = UART_GET_UTCR3(sport);
  318. if (break_state == -1)
  319. utcr3 |= UTCR3_BRK;
  320. else
  321. utcr3 &= ~UTCR3_BRK;
  322. UART_PUT_UTCR3(sport, utcr3);
  323. spin_unlock_irqrestore(&sport->port.lock, flags);
  324. }
  325. static int sa1100_startup(struct uart_port *port)
  326. {
  327. struct sa1100_port *sport = (struct sa1100_port *)port;
  328. int retval;
  329. /*
  330. * Allocate the IRQ
  331. */
  332. retval = request_irq(sport->port.irq, sa1100_int, 0,
  333. "sa11x0-uart", sport);
  334. if (retval)
  335. return retval;
  336. /*
  337. * Finally, clear and enable interrupts
  338. */
  339. UART_PUT_UTSR0(sport, -1);
  340. UART_PUT_UTCR3(sport, UTCR3_RXE | UTCR3_TXE | UTCR3_RIE);
  341. /*
  342. * Enable modem status interrupts
  343. */
  344. spin_lock_irq(&sport->port.lock);
  345. sa1100_enable_ms(&sport->port);
  346. spin_unlock_irq(&sport->port.lock);
  347. return 0;
  348. }
  349. static void sa1100_shutdown(struct uart_port *port)
  350. {
  351. struct sa1100_port *sport = (struct sa1100_port *)port;
  352. /*
  353. * Stop our timer.
  354. */
  355. del_timer_sync(&sport->timer);
  356. /*
  357. * Free the interrupt
  358. */
  359. free_irq(sport->port.irq, sport);
  360. /*
  361. * Disable all interrupts, port and break condition.
  362. */
  363. UART_PUT_UTCR3(sport, 0);
  364. }
  365. static void
  366. sa1100_set_termios(struct uart_port *port, struct termios *termios,
  367. struct termios *old)
  368. {
  369. struct sa1100_port *sport = (struct sa1100_port *)port;
  370. unsigned long flags;
  371. unsigned int utcr0, old_utcr3, baud, quot;
  372. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  373. /*
  374. * We only support CS7 and CS8.
  375. */
  376. while ((termios->c_cflag & CSIZE) != CS7 &&
  377. (termios->c_cflag & CSIZE) != CS8) {
  378. termios->c_cflag &= ~CSIZE;
  379. termios->c_cflag |= old_csize;
  380. old_csize = CS8;
  381. }
  382. if ((termios->c_cflag & CSIZE) == CS8)
  383. utcr0 = UTCR0_DSS;
  384. else
  385. utcr0 = 0;
  386. if (termios->c_cflag & CSTOPB)
  387. utcr0 |= UTCR0_SBS;
  388. if (termios->c_cflag & PARENB) {
  389. utcr0 |= UTCR0_PE;
  390. if (!(termios->c_cflag & PARODD))
  391. utcr0 |= UTCR0_OES;
  392. }
  393. /*
  394. * Ask the core to calculate the divisor for us.
  395. */
  396. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  397. quot = uart_get_divisor(port, baud);
  398. spin_lock_irqsave(&sport->port.lock, flags);
  399. sport->port.read_status_mask &= UTSR0_TO_SM(UTSR0_TFS);
  400. sport->port.read_status_mask |= UTSR1_TO_SM(UTSR1_ROR);
  401. if (termios->c_iflag & INPCK)
  402. sport->port.read_status_mask |=
  403. UTSR1_TO_SM(UTSR1_FRE | UTSR1_PRE);
  404. if (termios->c_iflag & (BRKINT | PARMRK))
  405. sport->port.read_status_mask |=
  406. UTSR0_TO_SM(UTSR0_RBB | UTSR0_REB);
  407. /*
  408. * Characters to ignore
  409. */
  410. sport->port.ignore_status_mask = 0;
  411. if (termios->c_iflag & IGNPAR)
  412. sport->port.ignore_status_mask |=
  413. UTSR1_TO_SM(UTSR1_FRE | UTSR1_PRE);
  414. if (termios->c_iflag & IGNBRK) {
  415. sport->port.ignore_status_mask |=
  416. UTSR0_TO_SM(UTSR0_RBB | UTSR0_REB);
  417. /*
  418. * If we're ignoring parity and break indicators,
  419. * ignore overruns too (for real raw support).
  420. */
  421. if (termios->c_iflag & IGNPAR)
  422. sport->port.ignore_status_mask |=
  423. UTSR1_TO_SM(UTSR1_ROR);
  424. }
  425. del_timer_sync(&sport->timer);
  426. /*
  427. * Update the per-port timeout.
  428. */
  429. uart_update_timeout(port, termios->c_cflag, baud);
  430. /*
  431. * disable interrupts and drain transmitter
  432. */
  433. old_utcr3 = UART_GET_UTCR3(sport);
  434. UART_PUT_UTCR3(sport, old_utcr3 & ~(UTCR3_RIE | UTCR3_TIE));
  435. while (UART_GET_UTSR1(sport) & UTSR1_TBY)
  436. barrier();
  437. /* then, disable everything */
  438. UART_PUT_UTCR3(sport, 0);
  439. /* set the parity, stop bits and data size */
  440. UART_PUT_UTCR0(sport, utcr0);
  441. /* set the baud rate */
  442. quot -= 1;
  443. UART_PUT_UTCR1(sport, ((quot & 0xf00) >> 8));
  444. UART_PUT_UTCR2(sport, (quot & 0xff));
  445. UART_PUT_UTSR0(sport, -1);
  446. UART_PUT_UTCR3(sport, old_utcr3);
  447. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  448. sa1100_enable_ms(&sport->port);
  449. spin_unlock_irqrestore(&sport->port.lock, flags);
  450. }
  451. static const char *sa1100_type(struct uart_port *port)
  452. {
  453. struct sa1100_port *sport = (struct sa1100_port *)port;
  454. return sport->port.type == PORT_SA1100 ? "SA1100" : NULL;
  455. }
  456. /*
  457. * Release the memory region(s) being used by 'port'.
  458. */
  459. static void sa1100_release_port(struct uart_port *port)
  460. {
  461. struct sa1100_port *sport = (struct sa1100_port *)port;
  462. release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
  463. }
  464. /*
  465. * Request the memory region(s) being used by 'port'.
  466. */
  467. static int sa1100_request_port(struct uart_port *port)
  468. {
  469. struct sa1100_port *sport = (struct sa1100_port *)port;
  470. return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
  471. "sa11x0-uart") != NULL ? 0 : -EBUSY;
  472. }
  473. /*
  474. * Configure/autoconfigure the port.
  475. */
  476. static void sa1100_config_port(struct uart_port *port, int flags)
  477. {
  478. struct sa1100_port *sport = (struct sa1100_port *)port;
  479. if (flags & UART_CONFIG_TYPE &&
  480. sa1100_request_port(&sport->port) == 0)
  481. sport->port.type = PORT_SA1100;
  482. }
  483. /*
  484. * Verify the new serial_struct (for TIOCSSERIAL).
  485. * The only change we allow are to the flags and type, and
  486. * even then only between PORT_SA1100 and PORT_UNKNOWN
  487. */
  488. static int
  489. sa1100_verify_port(struct uart_port *port, struct serial_struct *ser)
  490. {
  491. struct sa1100_port *sport = (struct sa1100_port *)port;
  492. int ret = 0;
  493. if (ser->type != PORT_UNKNOWN && ser->type != PORT_SA1100)
  494. ret = -EINVAL;
  495. if (sport->port.irq != ser->irq)
  496. ret = -EINVAL;
  497. if (ser->io_type != SERIAL_IO_MEM)
  498. ret = -EINVAL;
  499. if (sport->port.uartclk / 16 != ser->baud_base)
  500. ret = -EINVAL;
  501. if ((void *)sport->port.mapbase != ser->iomem_base)
  502. ret = -EINVAL;
  503. if (sport->port.iobase != ser->port)
  504. ret = -EINVAL;
  505. if (ser->hub6 != 0)
  506. ret = -EINVAL;
  507. return ret;
  508. }
  509. static struct uart_ops sa1100_pops = {
  510. .tx_empty = sa1100_tx_empty,
  511. .set_mctrl = sa1100_set_mctrl,
  512. .get_mctrl = sa1100_get_mctrl,
  513. .stop_tx = sa1100_stop_tx,
  514. .start_tx = sa1100_start_tx,
  515. .stop_rx = sa1100_stop_rx,
  516. .enable_ms = sa1100_enable_ms,
  517. .break_ctl = sa1100_break_ctl,
  518. .startup = sa1100_startup,
  519. .shutdown = sa1100_shutdown,
  520. .set_termios = sa1100_set_termios,
  521. .type = sa1100_type,
  522. .release_port = sa1100_release_port,
  523. .request_port = sa1100_request_port,
  524. .config_port = sa1100_config_port,
  525. .verify_port = sa1100_verify_port,
  526. };
  527. static struct sa1100_port sa1100_ports[NR_PORTS];
  528. /*
  529. * Setup the SA1100 serial ports. Note that we don't include the IrDA
  530. * port here since we have our own SIR/FIR driver (see drivers/net/irda)
  531. *
  532. * Note also that we support "console=ttySAx" where "x" is either 0 or 1.
  533. * Which serial port this ends up being depends on the machine you're
  534. * running this kernel on. I'm not convinced that this is a good idea,
  535. * but that's the way it traditionally works.
  536. *
  537. * Note that NanoEngine UART3 becomes UART2, and UART2 is no longer
  538. * used here.
  539. */
  540. static void __init sa1100_init_ports(void)
  541. {
  542. static int first = 1;
  543. int i;
  544. if (!first)
  545. return;
  546. first = 0;
  547. for (i = 0; i < NR_PORTS; i++) {
  548. sa1100_ports[i].port.uartclk = 3686400;
  549. sa1100_ports[i].port.ops = &sa1100_pops;
  550. sa1100_ports[i].port.fifosize = 8;
  551. sa1100_ports[i].port.line = i;
  552. sa1100_ports[i].port.iotype = SERIAL_IO_MEM;
  553. init_timer(&sa1100_ports[i].timer);
  554. sa1100_ports[i].timer.function = sa1100_timeout;
  555. sa1100_ports[i].timer.data = (unsigned long)&sa1100_ports[i];
  556. }
  557. /*
  558. * make transmit lines outputs, so that when the port
  559. * is closed, the output is in the MARK state.
  560. */
  561. PPDR |= PPC_TXD1 | PPC_TXD3;
  562. PPSR |= PPC_TXD1 | PPC_TXD3;
  563. }
  564. void __init sa1100_register_uart_fns(struct sa1100_port_fns *fns)
  565. {
  566. if (fns->get_mctrl)
  567. sa1100_pops.get_mctrl = fns->get_mctrl;
  568. if (fns->set_mctrl)
  569. sa1100_pops.set_mctrl = fns->set_mctrl;
  570. sa1100_pops.pm = fns->pm;
  571. sa1100_pops.set_wake = fns->set_wake;
  572. }
  573. void __init sa1100_register_uart(int idx, int port)
  574. {
  575. if (idx >= NR_PORTS) {
  576. printk(KERN_ERR "%s: bad index number %d\n", __FUNCTION__, idx);
  577. return;
  578. }
  579. switch (port) {
  580. case 1:
  581. sa1100_ports[idx].port.membase = (void __iomem *)&Ser1UTCR0;
  582. sa1100_ports[idx].port.mapbase = _Ser1UTCR0;
  583. sa1100_ports[idx].port.irq = IRQ_Ser1UART;
  584. sa1100_ports[idx].port.flags = ASYNC_BOOT_AUTOCONF;
  585. break;
  586. case 2:
  587. sa1100_ports[idx].port.membase = (void __iomem *)&Ser2UTCR0;
  588. sa1100_ports[idx].port.mapbase = _Ser2UTCR0;
  589. sa1100_ports[idx].port.irq = IRQ_Ser2ICP;
  590. sa1100_ports[idx].port.flags = ASYNC_BOOT_AUTOCONF;
  591. break;
  592. case 3:
  593. sa1100_ports[idx].port.membase = (void __iomem *)&Ser3UTCR0;
  594. sa1100_ports[idx].port.mapbase = _Ser3UTCR0;
  595. sa1100_ports[idx].port.irq = IRQ_Ser3UART;
  596. sa1100_ports[idx].port.flags = ASYNC_BOOT_AUTOCONF;
  597. break;
  598. default:
  599. printk(KERN_ERR "%s: bad port number %d\n", __FUNCTION__, port);
  600. }
  601. }
  602. #ifdef CONFIG_SERIAL_SA1100_CONSOLE
  603. /*
  604. * Interrupts are disabled on entering
  605. */
  606. static void
  607. sa1100_console_write(struct console *co, const char *s, unsigned int count)
  608. {
  609. struct sa1100_port *sport = &sa1100_ports[co->index];
  610. unsigned int old_utcr3, status, i;
  611. /*
  612. * First, save UTCR3 and then disable interrupts
  613. */
  614. old_utcr3 = UART_GET_UTCR3(sport);
  615. UART_PUT_UTCR3(sport, (old_utcr3 & ~(UTCR3_RIE | UTCR3_TIE)) |
  616. UTCR3_TXE);
  617. /*
  618. * Now, do each character
  619. */
  620. for (i = 0; i < count; i++) {
  621. do {
  622. status = UART_GET_UTSR1(sport);
  623. } while (!(status & UTSR1_TNF));
  624. UART_PUT_CHAR(sport, s[i]);
  625. if (s[i] == '\n') {
  626. do {
  627. status = UART_GET_UTSR1(sport);
  628. } while (!(status & UTSR1_TNF));
  629. UART_PUT_CHAR(sport, '\r');
  630. }
  631. }
  632. /*
  633. * Finally, wait for transmitter to become empty
  634. * and restore UTCR3
  635. */
  636. do {
  637. status = UART_GET_UTSR1(sport);
  638. } while (status & UTSR1_TBY);
  639. UART_PUT_UTCR3(sport, old_utcr3);
  640. }
  641. /*
  642. * If the port was already initialised (eg, by a boot loader),
  643. * try to determine the current setup.
  644. */
  645. static void __init
  646. sa1100_console_get_options(struct sa1100_port *sport, int *baud,
  647. int *parity, int *bits)
  648. {
  649. unsigned int utcr3;
  650. utcr3 = UART_GET_UTCR3(sport) & (UTCR3_RXE | UTCR3_TXE);
  651. if (utcr3 == (UTCR3_RXE | UTCR3_TXE)) {
  652. /* ok, the port was enabled */
  653. unsigned int utcr0, quot;
  654. utcr0 = UART_GET_UTCR0(sport);
  655. *parity = 'n';
  656. if (utcr0 & UTCR0_PE) {
  657. if (utcr0 & UTCR0_OES)
  658. *parity = 'e';
  659. else
  660. *parity = 'o';
  661. }
  662. if (utcr0 & UTCR0_DSS)
  663. *bits = 8;
  664. else
  665. *bits = 7;
  666. quot = UART_GET_UTCR2(sport) | UART_GET_UTCR1(sport) << 8;
  667. quot &= 0xfff;
  668. *baud = sport->port.uartclk / (16 * (quot + 1));
  669. }
  670. }
  671. static int __init
  672. sa1100_console_setup(struct console *co, char *options)
  673. {
  674. struct sa1100_port *sport;
  675. int baud = 9600;
  676. int bits = 8;
  677. int parity = 'n';
  678. int flow = 'n';
  679. /*
  680. * Check whether an invalid uart number has been specified, and
  681. * if so, search for the first available port that does have
  682. * console support.
  683. */
  684. if (co->index == -1 || co->index >= NR_PORTS)
  685. co->index = 0;
  686. sport = &sa1100_ports[co->index];
  687. if (options)
  688. uart_parse_options(options, &baud, &parity, &bits, &flow);
  689. else
  690. sa1100_console_get_options(sport, &baud, &parity, &bits);
  691. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  692. }
  693. extern struct uart_driver sa1100_reg;
  694. static struct console sa1100_console = {
  695. .name = "ttySA",
  696. .write = sa1100_console_write,
  697. .device = uart_console_device,
  698. .setup = sa1100_console_setup,
  699. .flags = CON_PRINTBUFFER,
  700. .index = -1,
  701. .data = &sa1100_reg,
  702. };
  703. static int __init sa1100_rs_console_init(void)
  704. {
  705. sa1100_init_ports();
  706. register_console(&sa1100_console);
  707. return 0;
  708. }
  709. console_initcall(sa1100_rs_console_init);
  710. #define SA1100_CONSOLE &sa1100_console
  711. #else
  712. #define SA1100_CONSOLE NULL
  713. #endif
  714. static struct uart_driver sa1100_reg = {
  715. .owner = THIS_MODULE,
  716. .driver_name = "ttySA",
  717. .dev_name = "ttySA",
  718. .devfs_name = "ttySA",
  719. .major = SERIAL_SA1100_MAJOR,
  720. .minor = MINOR_START,
  721. .nr = NR_PORTS,
  722. .cons = SA1100_CONSOLE,
  723. };
  724. static int sa1100_serial_suspend(struct device *_dev, pm_message_t state, u32 level)
  725. {
  726. struct sa1100_port *sport = dev_get_drvdata(_dev);
  727. if (sport && level == SUSPEND_DISABLE)
  728. uart_suspend_port(&sa1100_reg, &sport->port);
  729. return 0;
  730. }
  731. static int sa1100_serial_resume(struct device *_dev, u32 level)
  732. {
  733. struct sa1100_port *sport = dev_get_drvdata(_dev);
  734. if (sport && level == RESUME_ENABLE)
  735. uart_resume_port(&sa1100_reg, &sport->port);
  736. return 0;
  737. }
  738. static int sa1100_serial_probe(struct device *_dev)
  739. {
  740. struct platform_device *dev = to_platform_device(_dev);
  741. struct resource *res = dev->resource;
  742. int i;
  743. for (i = 0; i < dev->num_resources; i++, res++)
  744. if (res->flags & IORESOURCE_MEM)
  745. break;
  746. if (i < dev->num_resources) {
  747. for (i = 0; i < NR_PORTS; i++) {
  748. if (sa1100_ports[i].port.mapbase != res->start)
  749. continue;
  750. sa1100_ports[i].port.dev = _dev;
  751. uart_add_one_port(&sa1100_reg, &sa1100_ports[i].port);
  752. dev_set_drvdata(_dev, &sa1100_ports[i]);
  753. break;
  754. }
  755. }
  756. return 0;
  757. }
  758. static int sa1100_serial_remove(struct device *_dev)
  759. {
  760. struct sa1100_port *sport = dev_get_drvdata(_dev);
  761. dev_set_drvdata(_dev, NULL);
  762. if (sport)
  763. uart_remove_one_port(&sa1100_reg, &sport->port);
  764. return 0;
  765. }
  766. static struct device_driver sa11x0_serial_driver = {
  767. .name = "sa11x0-uart",
  768. .bus = &platform_bus_type,
  769. .probe = sa1100_serial_probe,
  770. .remove = sa1100_serial_remove,
  771. .suspend = sa1100_serial_suspend,
  772. .resume = sa1100_serial_resume,
  773. };
  774. static int __init sa1100_serial_init(void)
  775. {
  776. int ret;
  777. printk(KERN_INFO "Serial: SA11x0 driver $Revision: 1.50 $\n");
  778. sa1100_init_ports();
  779. ret = uart_register_driver(&sa1100_reg);
  780. if (ret == 0) {
  781. ret = driver_register(&sa11x0_serial_driver);
  782. if (ret)
  783. uart_unregister_driver(&sa1100_reg);
  784. }
  785. return ret;
  786. }
  787. static void __exit sa1100_serial_exit(void)
  788. {
  789. driver_unregister(&sa11x0_serial_driver);
  790. uart_unregister_driver(&sa1100_reg);
  791. }
  792. module_init(sa1100_serial_init);
  793. module_exit(sa1100_serial_exit);
  794. MODULE_AUTHOR("Deep Blue Solutions Ltd");
  795. MODULE_DESCRIPTION("SA1100 generic serial port driver $Revision: 1.50 $");
  796. MODULE_LICENSE("GPL");
  797. MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_SA1100_MAJOR);