sa1100.c 22 KB

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