sa1100.c 22 KB

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