sa1100.c 23 KB

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