samsung.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. /* linux/drivers/serial/samsuing.c
  2. *
  3. * Driver core for Samsung SoC onboard UARTs.
  4. *
  5. * Ben Dooks, Copyright (c) 2003-2005,2008 Simtec Electronics
  6. * http://armlinux.simtec.co.uk/
  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 version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. /* Hote on 2410 error handling
  13. *
  14. * The s3c2410 manual has a love/hate affair with the contents of the
  15. * UERSTAT register in the UART blocks, and keeps marking some of the
  16. * error bits as reserved. Having checked with the s3c2410x01,
  17. * it copes with BREAKs properly, so I am happy to ignore the RESERVED
  18. * feature from the latter versions of the manual.
  19. *
  20. * If it becomes aparrent that latter versions of the 2410 remove these
  21. * bits, then action will have to be taken to differentiate the versions
  22. * and change the policy on BREAK
  23. *
  24. * BJD, 04-Nov-2004
  25. */
  26. #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  27. #define SUPPORT_SYSRQ
  28. #endif
  29. #include <linux/module.h>
  30. #include <linux/ioport.h>
  31. #include <linux/io.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/init.h>
  34. #include <linux/sysrq.h>
  35. #include <linux/console.h>
  36. #include <linux/tty.h>
  37. #include <linux/tty_flip.h>
  38. #include <linux/serial_core.h>
  39. #include <linux/serial.h>
  40. #include <linux/delay.h>
  41. #include <linux/clk.h>
  42. #include <asm/irq.h>
  43. #include <mach/hardware.h>
  44. #include <asm/plat-s3c/regs-serial.h>
  45. #include <mach/regs-gpio.h>
  46. #include "samsung.h"
  47. /* UART name and device definitions */
  48. #define S3C24XX_SERIAL_NAME "ttySAC"
  49. #define S3C24XX_SERIAL_MAJOR 204
  50. #define S3C24XX_SERIAL_MINOR 64
  51. /* we can support 3 uarts, but not always use them */
  52. #ifdef CONFIG_CPU_S3C2400
  53. #define NR_PORTS (2)
  54. #else
  55. #define NR_PORTS (3)
  56. #endif
  57. /* port irq numbers */
  58. #define TX_IRQ(port) ((port)->irq + 1)
  59. #define RX_IRQ(port) ((port)->irq)
  60. /* macros to change one thing to another */
  61. #define tx_enabled(port) ((port)->unused[0])
  62. #define rx_enabled(port) ((port)->unused[1])
  63. /* flag to ignore all characters comming in */
  64. #define RXSTAT_DUMMY_READ (0x10000000)
  65. static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
  66. {
  67. return container_of(port, struct s3c24xx_uart_port, port);
  68. }
  69. /* translate a port to the device name */
  70. static inline const char *s3c24xx_serial_portname(struct uart_port *port)
  71. {
  72. return to_platform_device(port->dev)->name;
  73. }
  74. static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
  75. {
  76. return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);
  77. }
  78. static void s3c24xx_serial_rx_enable(struct uart_port *port)
  79. {
  80. unsigned long flags;
  81. unsigned int ucon, ufcon;
  82. int count = 10000;
  83. spin_lock_irqsave(&port->lock, flags);
  84. while (--count && !s3c24xx_serial_txempty_nofifo(port))
  85. udelay(100);
  86. ufcon = rd_regl(port, S3C2410_UFCON);
  87. ufcon |= S3C2410_UFCON_RESETRX;
  88. wr_regl(port, S3C2410_UFCON, ufcon);
  89. ucon = rd_regl(port, S3C2410_UCON);
  90. ucon |= S3C2410_UCON_RXIRQMODE;
  91. wr_regl(port, S3C2410_UCON, ucon);
  92. rx_enabled(port) = 1;
  93. spin_unlock_irqrestore(&port->lock, flags);
  94. }
  95. static void s3c24xx_serial_rx_disable(struct uart_port *port)
  96. {
  97. unsigned long flags;
  98. unsigned int ucon;
  99. spin_lock_irqsave(&port->lock, flags);
  100. ucon = rd_regl(port, S3C2410_UCON);
  101. ucon &= ~S3C2410_UCON_RXIRQMODE;
  102. wr_regl(port, S3C2410_UCON, ucon);
  103. rx_enabled(port) = 0;
  104. spin_unlock_irqrestore(&port->lock, flags);
  105. }
  106. static void s3c24xx_serial_stop_tx(struct uart_port *port)
  107. {
  108. if (tx_enabled(port)) {
  109. disable_irq(TX_IRQ(port));
  110. tx_enabled(port) = 0;
  111. if (port->flags & UPF_CONS_FLOW)
  112. s3c24xx_serial_rx_enable(port);
  113. }
  114. }
  115. static void s3c24xx_serial_start_tx(struct uart_port *port)
  116. {
  117. if (!tx_enabled(port)) {
  118. if (port->flags & UPF_CONS_FLOW)
  119. s3c24xx_serial_rx_disable(port);
  120. enable_irq(TX_IRQ(port));
  121. tx_enabled(port) = 1;
  122. }
  123. }
  124. static void s3c24xx_serial_stop_rx(struct uart_port *port)
  125. {
  126. if (rx_enabled(port)) {
  127. dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
  128. disable_irq(RX_IRQ(port));
  129. rx_enabled(port) = 0;
  130. }
  131. }
  132. static void s3c24xx_serial_enable_ms(struct uart_port *port)
  133. {
  134. }
  135. static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
  136. {
  137. return to_ourport(port)->info;
  138. }
  139. static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
  140. {
  141. if (port->dev == NULL)
  142. return NULL;
  143. return (struct s3c2410_uartcfg *)port->dev->platform_data;
  144. }
  145. static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
  146. unsigned long ufstat)
  147. {
  148. struct s3c24xx_uart_info *info = ourport->info;
  149. if (ufstat & info->rx_fifofull)
  150. return info->fifosize;
  151. return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
  152. }
  153. /* ? - where has parity gone?? */
  154. #define S3C2410_UERSTAT_PARITY (0x1000)
  155. static irqreturn_t
  156. s3c24xx_serial_rx_chars(int irq, void *dev_id)
  157. {
  158. struct s3c24xx_uart_port *ourport = dev_id;
  159. struct uart_port *port = &ourport->port;
  160. struct tty_struct *tty = port->info->port.tty;
  161. unsigned int ufcon, ch, flag, ufstat, uerstat;
  162. int max_count = 64;
  163. while (max_count-- > 0) {
  164. ufcon = rd_regl(port, S3C2410_UFCON);
  165. ufstat = rd_regl(port, S3C2410_UFSTAT);
  166. if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
  167. break;
  168. uerstat = rd_regl(port, S3C2410_UERSTAT);
  169. ch = rd_regb(port, S3C2410_URXH);
  170. if (port->flags & UPF_CONS_FLOW) {
  171. int txe = s3c24xx_serial_txempty_nofifo(port);
  172. if (rx_enabled(port)) {
  173. if (!txe) {
  174. rx_enabled(port) = 0;
  175. continue;
  176. }
  177. } else {
  178. if (txe) {
  179. ufcon |= S3C2410_UFCON_RESETRX;
  180. wr_regl(port, S3C2410_UFCON, ufcon);
  181. rx_enabled(port) = 1;
  182. goto out;
  183. }
  184. continue;
  185. }
  186. }
  187. /* insert the character into the buffer */
  188. flag = TTY_NORMAL;
  189. port->icount.rx++;
  190. if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
  191. dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
  192. ch, uerstat);
  193. /* check for break */
  194. if (uerstat & S3C2410_UERSTAT_BREAK) {
  195. dbg("break!\n");
  196. port->icount.brk++;
  197. if (uart_handle_break(port))
  198. goto ignore_char;
  199. }
  200. if (uerstat & S3C2410_UERSTAT_FRAME)
  201. port->icount.frame++;
  202. if (uerstat & S3C2410_UERSTAT_OVERRUN)
  203. port->icount.overrun++;
  204. uerstat &= port->read_status_mask;
  205. if (uerstat & S3C2410_UERSTAT_BREAK)
  206. flag = TTY_BREAK;
  207. else if (uerstat & S3C2410_UERSTAT_PARITY)
  208. flag = TTY_PARITY;
  209. else if (uerstat & (S3C2410_UERSTAT_FRAME |
  210. S3C2410_UERSTAT_OVERRUN))
  211. flag = TTY_FRAME;
  212. }
  213. if (uart_handle_sysrq_char(port, ch))
  214. goto ignore_char;
  215. uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
  216. ch, flag);
  217. ignore_char:
  218. continue;
  219. }
  220. tty_flip_buffer_push(tty);
  221. out:
  222. return IRQ_HANDLED;
  223. }
  224. static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
  225. {
  226. struct s3c24xx_uart_port *ourport = id;
  227. struct uart_port *port = &ourport->port;
  228. struct circ_buf *xmit = &port->info->xmit;
  229. int count = 256;
  230. if (port->x_char) {
  231. wr_regb(port, S3C2410_UTXH, port->x_char);
  232. port->icount.tx++;
  233. port->x_char = 0;
  234. goto out;
  235. }
  236. /* if there isnt anything more to transmit, or the uart is now
  237. * stopped, disable the uart and exit
  238. */
  239. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  240. s3c24xx_serial_stop_tx(port);
  241. goto out;
  242. }
  243. /* try and drain the buffer... */
  244. while (!uart_circ_empty(xmit) && count-- > 0) {
  245. if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
  246. break;
  247. wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
  248. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  249. port->icount.tx++;
  250. }
  251. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  252. uart_write_wakeup(port);
  253. if (uart_circ_empty(xmit))
  254. s3c24xx_serial_stop_tx(port);
  255. out:
  256. return IRQ_HANDLED;
  257. }
  258. static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
  259. {
  260. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  261. unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
  262. unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
  263. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  264. if ((ufstat & info->tx_fifomask) != 0 ||
  265. (ufstat & info->tx_fifofull))
  266. return 0;
  267. return 1;
  268. }
  269. return s3c24xx_serial_txempty_nofifo(port);
  270. }
  271. /* no modem control lines */
  272. static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
  273. {
  274. unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
  275. if (umstat & S3C2410_UMSTAT_CTS)
  276. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  277. else
  278. return TIOCM_CAR | TIOCM_DSR;
  279. }
  280. static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  281. {
  282. /* todo - possibly remove AFC and do manual CTS */
  283. }
  284. static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
  285. {
  286. unsigned long flags;
  287. unsigned int ucon;
  288. spin_lock_irqsave(&port->lock, flags);
  289. ucon = rd_regl(port, S3C2410_UCON);
  290. if (break_state)
  291. ucon |= S3C2410_UCON_SBREAK;
  292. else
  293. ucon &= ~S3C2410_UCON_SBREAK;
  294. wr_regl(port, S3C2410_UCON, ucon);
  295. spin_unlock_irqrestore(&port->lock, flags);
  296. }
  297. static void s3c24xx_serial_shutdown(struct uart_port *port)
  298. {
  299. struct s3c24xx_uart_port *ourport = to_ourport(port);
  300. if (ourport->tx_claimed) {
  301. free_irq(TX_IRQ(port), ourport);
  302. tx_enabled(port) = 0;
  303. ourport->tx_claimed = 0;
  304. }
  305. if (ourport->rx_claimed) {
  306. free_irq(RX_IRQ(port), ourport);
  307. ourport->rx_claimed = 0;
  308. rx_enabled(port) = 0;
  309. }
  310. }
  311. static int s3c24xx_serial_startup(struct uart_port *port)
  312. {
  313. struct s3c24xx_uart_port *ourport = to_ourport(port);
  314. int ret;
  315. dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
  316. port->mapbase, port->membase);
  317. rx_enabled(port) = 1;
  318. ret = request_irq(RX_IRQ(port),
  319. s3c24xx_serial_rx_chars, 0,
  320. s3c24xx_serial_portname(port), ourport);
  321. if (ret != 0) {
  322. printk(KERN_ERR "cannot get irq %d\n", RX_IRQ(port));
  323. return ret;
  324. }
  325. ourport->rx_claimed = 1;
  326. dbg("requesting tx irq...\n");
  327. tx_enabled(port) = 1;
  328. ret = request_irq(TX_IRQ(port),
  329. s3c24xx_serial_tx_chars, 0,
  330. s3c24xx_serial_portname(port), ourport);
  331. if (ret) {
  332. printk(KERN_ERR "cannot get irq %d\n", TX_IRQ(port));
  333. goto err;
  334. }
  335. ourport->tx_claimed = 1;
  336. dbg("s3c24xx_serial_startup ok\n");
  337. /* the port reset code should have done the correct
  338. * register setup for the port controls */
  339. return ret;
  340. err:
  341. s3c24xx_serial_shutdown(port);
  342. return ret;
  343. }
  344. /* power power management control */
  345. static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
  346. unsigned int old)
  347. {
  348. struct s3c24xx_uart_port *ourport = to_ourport(port);
  349. switch (level) {
  350. case 3:
  351. if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
  352. clk_disable(ourport->baudclk);
  353. clk_disable(ourport->clk);
  354. break;
  355. case 0:
  356. clk_enable(ourport->clk);
  357. if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
  358. clk_enable(ourport->baudclk);
  359. break;
  360. default:
  361. printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
  362. }
  363. }
  364. /* baud rate calculation
  365. *
  366. * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
  367. * of different sources, including the peripheral clock ("pclk") and an
  368. * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
  369. * with a programmable extra divisor.
  370. *
  371. * The following code goes through the clock sources, and calculates the
  372. * baud clocks (and the resultant actual baud rates) and then tries to
  373. * pick the closest one and select that.
  374. *
  375. */
  376. #define MAX_CLKS (8)
  377. static struct s3c24xx_uart_clksrc tmp_clksrc = {
  378. .name = "pclk",
  379. .min_baud = 0,
  380. .max_baud = 0,
  381. .divisor = 1,
  382. };
  383. static inline int
  384. s3c24xx_serial_getsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
  385. {
  386. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  387. return (info->get_clksrc)(port, c);
  388. }
  389. static inline int
  390. s3c24xx_serial_setsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
  391. {
  392. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  393. return (info->set_clksrc)(port, c);
  394. }
  395. struct baud_calc {
  396. struct s3c24xx_uart_clksrc *clksrc;
  397. unsigned int calc;
  398. unsigned int quot;
  399. struct clk *src;
  400. };
  401. static int s3c24xx_serial_calcbaud(struct baud_calc *calc,
  402. struct uart_port *port,
  403. struct s3c24xx_uart_clksrc *clksrc,
  404. unsigned int baud)
  405. {
  406. unsigned long rate;
  407. calc->src = clk_get(port->dev, clksrc->name);
  408. if (calc->src == NULL || IS_ERR(calc->src))
  409. return 0;
  410. rate = clk_get_rate(calc->src);
  411. rate /= clksrc->divisor;
  412. calc->clksrc = clksrc;
  413. calc->quot = (rate + (8 * baud)) / (16 * baud);
  414. calc->calc = (rate / (calc->quot * 16));
  415. calc->quot--;
  416. return 1;
  417. }
  418. static unsigned int s3c24xx_serial_getclk(struct uart_port *port,
  419. struct s3c24xx_uart_clksrc **clksrc,
  420. struct clk **clk,
  421. unsigned int baud)
  422. {
  423. struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
  424. struct s3c24xx_uart_clksrc *clkp;
  425. struct baud_calc res[MAX_CLKS];
  426. struct baud_calc *resptr, *best, *sptr;
  427. int i;
  428. clkp = cfg->clocks;
  429. best = NULL;
  430. if (cfg->clocks_size < 2) {
  431. if (cfg->clocks_size == 0)
  432. clkp = &tmp_clksrc;
  433. /* check to see if we're sourcing fclk, and if so we're
  434. * going to have to update the clock source
  435. */
  436. if (strcmp(clkp->name, "fclk") == 0) {
  437. struct s3c24xx_uart_clksrc src;
  438. s3c24xx_serial_getsource(port, &src);
  439. /* check that the port already using fclk, and if
  440. * not, then re-select fclk
  441. */
  442. if (strcmp(src.name, clkp->name) == 0) {
  443. s3c24xx_serial_setsource(port, clkp);
  444. s3c24xx_serial_getsource(port, &src);
  445. }
  446. clkp->divisor = src.divisor;
  447. }
  448. s3c24xx_serial_calcbaud(res, port, clkp, baud);
  449. best = res;
  450. resptr = best + 1;
  451. } else {
  452. resptr = res;
  453. for (i = 0; i < cfg->clocks_size; i++, clkp++) {
  454. if (s3c24xx_serial_calcbaud(resptr, port, clkp, baud))
  455. resptr++;
  456. }
  457. }
  458. /* ok, we now need to select the best clock we found */
  459. if (!best) {
  460. unsigned int deviation = (1<<30)|((1<<30)-1);
  461. int calc_deviation;
  462. for (sptr = res; sptr < resptr; sptr++) {
  463. calc_deviation = baud - sptr->calc;
  464. if (calc_deviation < 0)
  465. calc_deviation = -calc_deviation;
  466. if (calc_deviation < deviation) {
  467. best = sptr;
  468. deviation = calc_deviation;
  469. }
  470. }
  471. }
  472. /* store results to pass back */
  473. *clksrc = best->clksrc;
  474. *clk = best->src;
  475. return best->quot;
  476. }
  477. static void s3c24xx_serial_set_termios(struct uart_port *port,
  478. struct ktermios *termios,
  479. struct ktermios *old)
  480. {
  481. struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
  482. struct s3c24xx_uart_port *ourport = to_ourport(port);
  483. struct s3c24xx_uart_clksrc *clksrc = NULL;
  484. struct clk *clk = NULL;
  485. unsigned long flags;
  486. unsigned int baud, quot;
  487. unsigned int ulcon;
  488. unsigned int umcon;
  489. /*
  490. * We don't support modem control lines.
  491. */
  492. termios->c_cflag &= ~(HUPCL | CMSPAR);
  493. termios->c_cflag |= CLOCAL;
  494. /*
  495. * Ask the core to calculate the divisor for us.
  496. */
  497. baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
  498. if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
  499. quot = port->custom_divisor;
  500. else
  501. quot = s3c24xx_serial_getclk(port, &clksrc, &clk, baud);
  502. /* check to see if we need to change clock source */
  503. if (ourport->clksrc != clksrc || ourport->baudclk != clk) {
  504. s3c24xx_serial_setsource(port, clksrc);
  505. if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
  506. clk_disable(ourport->baudclk);
  507. ourport->baudclk = NULL;
  508. }
  509. clk_enable(clk);
  510. ourport->clksrc = clksrc;
  511. ourport->baudclk = clk;
  512. }
  513. switch (termios->c_cflag & CSIZE) {
  514. case CS5:
  515. dbg("config: 5bits/char\n");
  516. ulcon = S3C2410_LCON_CS5;
  517. break;
  518. case CS6:
  519. dbg("config: 6bits/char\n");
  520. ulcon = S3C2410_LCON_CS6;
  521. break;
  522. case CS7:
  523. dbg("config: 7bits/char\n");
  524. ulcon = S3C2410_LCON_CS7;
  525. break;
  526. case CS8:
  527. default:
  528. dbg("config: 8bits/char\n");
  529. ulcon = S3C2410_LCON_CS8;
  530. break;
  531. }
  532. /* preserve original lcon IR settings */
  533. ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
  534. if (termios->c_cflag & CSTOPB)
  535. ulcon |= S3C2410_LCON_STOPB;
  536. umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
  537. if (termios->c_cflag & PARENB) {
  538. if (termios->c_cflag & PARODD)
  539. ulcon |= S3C2410_LCON_PODD;
  540. else
  541. ulcon |= S3C2410_LCON_PEVEN;
  542. } else {
  543. ulcon |= S3C2410_LCON_PNONE;
  544. }
  545. spin_lock_irqsave(&port->lock, flags);
  546. dbg("setting ulcon to %08x, brddiv to %d\n", ulcon, quot);
  547. wr_regl(port, S3C2410_ULCON, ulcon);
  548. wr_regl(port, S3C2410_UBRDIV, quot);
  549. wr_regl(port, S3C2410_UMCON, umcon);
  550. dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
  551. rd_regl(port, S3C2410_ULCON),
  552. rd_regl(port, S3C2410_UCON),
  553. rd_regl(port, S3C2410_UFCON));
  554. /*
  555. * Update the per-port timeout.
  556. */
  557. uart_update_timeout(port, termios->c_cflag, baud);
  558. /*
  559. * Which character status flags are we interested in?
  560. */
  561. port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
  562. if (termios->c_iflag & INPCK)
  563. port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
  564. /*
  565. * Which character status flags should we ignore?
  566. */
  567. port->ignore_status_mask = 0;
  568. if (termios->c_iflag & IGNPAR)
  569. port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
  570. if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
  571. port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
  572. /*
  573. * Ignore all characters if CREAD is not set.
  574. */
  575. if ((termios->c_cflag & CREAD) == 0)
  576. port->ignore_status_mask |= RXSTAT_DUMMY_READ;
  577. spin_unlock_irqrestore(&port->lock, flags);
  578. }
  579. static const char *s3c24xx_serial_type(struct uart_port *port)
  580. {
  581. switch (port->type) {
  582. case PORT_S3C2410:
  583. return "S3C2410";
  584. case PORT_S3C2440:
  585. return "S3C2440";
  586. case PORT_S3C2412:
  587. return "S3C2412";
  588. default:
  589. return NULL;
  590. }
  591. }
  592. #define MAP_SIZE (0x100)
  593. static void s3c24xx_serial_release_port(struct uart_port *port)
  594. {
  595. release_mem_region(port->mapbase, MAP_SIZE);
  596. }
  597. static int s3c24xx_serial_request_port(struct uart_port *port)
  598. {
  599. const char *name = s3c24xx_serial_portname(port);
  600. return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
  601. }
  602. static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
  603. {
  604. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  605. if (flags & UART_CONFIG_TYPE &&
  606. s3c24xx_serial_request_port(port) == 0)
  607. port->type = info->type;
  608. }
  609. /*
  610. * verify the new serial_struct (for TIOCSSERIAL).
  611. */
  612. static int
  613. s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  614. {
  615. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  616. if (ser->type != PORT_UNKNOWN && ser->type != info->type)
  617. return -EINVAL;
  618. return 0;
  619. }
  620. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  621. static struct console s3c24xx_serial_console;
  622. #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
  623. #else
  624. #define S3C24XX_SERIAL_CONSOLE NULL
  625. #endif
  626. static struct uart_ops s3c24xx_serial_ops = {
  627. .pm = s3c24xx_serial_pm,
  628. .tx_empty = s3c24xx_serial_tx_empty,
  629. .get_mctrl = s3c24xx_serial_get_mctrl,
  630. .set_mctrl = s3c24xx_serial_set_mctrl,
  631. .stop_tx = s3c24xx_serial_stop_tx,
  632. .start_tx = s3c24xx_serial_start_tx,
  633. .stop_rx = s3c24xx_serial_stop_rx,
  634. .enable_ms = s3c24xx_serial_enable_ms,
  635. .break_ctl = s3c24xx_serial_break_ctl,
  636. .startup = s3c24xx_serial_startup,
  637. .shutdown = s3c24xx_serial_shutdown,
  638. .set_termios = s3c24xx_serial_set_termios,
  639. .type = s3c24xx_serial_type,
  640. .release_port = s3c24xx_serial_release_port,
  641. .request_port = s3c24xx_serial_request_port,
  642. .config_port = s3c24xx_serial_config_port,
  643. .verify_port = s3c24xx_serial_verify_port,
  644. };
  645. static struct uart_driver s3c24xx_uart_drv = {
  646. .owner = THIS_MODULE,
  647. .dev_name = "s3c2410_serial",
  648. .nr = 3,
  649. .cons = S3C24XX_SERIAL_CONSOLE,
  650. .driver_name = S3C24XX_SERIAL_NAME,
  651. .major = S3C24XX_SERIAL_MAJOR,
  652. .minor = S3C24XX_SERIAL_MINOR,
  653. };
  654. static struct s3c24xx_uart_port s3c24xx_serial_ports[NR_PORTS] = {
  655. [0] = {
  656. .port = {
  657. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
  658. .iotype = UPIO_MEM,
  659. .irq = IRQ_S3CUART_RX0,
  660. .uartclk = 0,
  661. .fifosize = 16,
  662. .ops = &s3c24xx_serial_ops,
  663. .flags = UPF_BOOT_AUTOCONF,
  664. .line = 0,
  665. }
  666. },
  667. [1] = {
  668. .port = {
  669. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
  670. .iotype = UPIO_MEM,
  671. .irq = IRQ_S3CUART_RX1,
  672. .uartclk = 0,
  673. .fifosize = 16,
  674. .ops = &s3c24xx_serial_ops,
  675. .flags = UPF_BOOT_AUTOCONF,
  676. .line = 1,
  677. }
  678. },
  679. #if NR_PORTS > 2
  680. [2] = {
  681. .port = {
  682. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
  683. .iotype = UPIO_MEM,
  684. .irq = IRQ_S3CUART_RX2,
  685. .uartclk = 0,
  686. .fifosize = 16,
  687. .ops = &s3c24xx_serial_ops,
  688. .flags = UPF_BOOT_AUTOCONF,
  689. .line = 2,
  690. }
  691. }
  692. #endif
  693. };
  694. /* s3c24xx_serial_resetport
  695. *
  696. * wrapper to call the specific reset for this port (reset the fifos
  697. * and the settings)
  698. */
  699. static inline int s3c24xx_serial_resetport(struct uart_port *port,
  700. struct s3c2410_uartcfg *cfg)
  701. {
  702. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  703. return (info->reset_port)(port, cfg);
  704. }
  705. /* s3c24xx_serial_init_port
  706. *
  707. * initialise a single serial port from the platform device given
  708. */
  709. static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
  710. struct s3c24xx_uart_info *info,
  711. struct platform_device *platdev)
  712. {
  713. struct uart_port *port = &ourport->port;
  714. struct s3c2410_uartcfg *cfg;
  715. struct resource *res;
  716. int ret;
  717. dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
  718. if (platdev == NULL)
  719. return -ENODEV;
  720. cfg = s3c24xx_dev_to_cfg(&platdev->dev);
  721. if (port->mapbase != 0)
  722. return 0;
  723. if (cfg->hwport > 3)
  724. return -EINVAL;
  725. /* setup info for port */
  726. port->dev = &platdev->dev;
  727. ourport->info = info;
  728. /* copy the info in from provided structure */
  729. ourport->port.fifosize = info->fifosize;
  730. dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
  731. port->uartclk = 1;
  732. if (cfg->uart_flags & UPF_CONS_FLOW) {
  733. dbg("s3c24xx_serial_init_port: enabling flow control\n");
  734. port->flags |= UPF_CONS_FLOW;
  735. }
  736. /* sort our the physical and virtual addresses for each UART */
  737. res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
  738. if (res == NULL) {
  739. printk(KERN_ERR "failed to find memory resource for uart\n");
  740. return -EINVAL;
  741. }
  742. dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
  743. port->mapbase = res->start;
  744. port->membase = S3C24XX_VA_UART + (res->start - S3C24XX_PA_UART);
  745. ret = platform_get_irq(platdev, 0);
  746. if (ret < 0)
  747. port->irq = 0;
  748. else
  749. port->irq = ret;
  750. ourport->clk = clk_get(&platdev->dev, "uart");
  751. dbg("port: map=%08x, mem=%08x, irq=%d, clock=%ld\n",
  752. port->mapbase, port->membase, port->irq, port->uartclk);
  753. /* reset the fifos (and setup the uart) */
  754. s3c24xx_serial_resetport(port, cfg);
  755. return 0;
  756. }
  757. static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
  758. struct device_attribute *attr,
  759. char *buf)
  760. {
  761. struct uart_port *port = s3c24xx_dev_to_port(dev);
  762. struct s3c24xx_uart_port *ourport = to_ourport(port);
  763. return snprintf(buf, PAGE_SIZE, "* %s\n", ourport->clksrc->name);
  764. }
  765. static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL);
  766. /* Device driver serial port probe */
  767. static int probe_index;
  768. int s3c24xx_serial_probe(struct platform_device *dev,
  769. struct s3c24xx_uart_info *info)
  770. {
  771. struct s3c24xx_uart_port *ourport;
  772. int ret;
  773. dbg("s3c24xx_serial_probe(%p, %p) %d\n", dev, info, probe_index);
  774. ourport = &s3c24xx_serial_ports[probe_index];
  775. probe_index++;
  776. dbg("%s: initialising port %p...\n", __func__, ourport);
  777. ret = s3c24xx_serial_init_port(ourport, info, dev);
  778. if (ret < 0)
  779. goto probe_err;
  780. dbg("%s: adding port\n", __func__);
  781. uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
  782. platform_set_drvdata(dev, &ourport->port);
  783. ret = device_create_file(&dev->dev, &dev_attr_clock_source);
  784. if (ret < 0)
  785. printk(KERN_ERR "%s: failed to add clksrc attr.\n", __func__);
  786. return 0;
  787. probe_err:
  788. return ret;
  789. }
  790. EXPORT_SYMBOL_GPL(s3c24xx_serial_probe);
  791. int s3c24xx_serial_remove(struct platform_device *dev)
  792. {
  793. struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
  794. if (port) {
  795. device_remove_file(&dev->dev, &dev_attr_clock_source);
  796. uart_remove_one_port(&s3c24xx_uart_drv, port);
  797. }
  798. return 0;
  799. }
  800. EXPORT_SYMBOL_GPL(s3c24xx_serial_remove);
  801. /* UART power management code */
  802. #ifdef CONFIG_PM
  803. static int s3c24xx_serial_suspend(struct platform_device *dev, pm_message_t state)
  804. {
  805. struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
  806. if (port)
  807. uart_suspend_port(&s3c24xx_uart_drv, port);
  808. return 0;
  809. }
  810. static int s3c24xx_serial_resume(struct platform_device *dev)
  811. {
  812. struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
  813. struct s3c24xx_uart_port *ourport = to_ourport(port);
  814. if (port) {
  815. clk_enable(ourport->clk);
  816. s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
  817. clk_disable(ourport->clk);
  818. uart_resume_port(&s3c24xx_uart_drv, port);
  819. }
  820. return 0;
  821. }
  822. #endif
  823. int s3c24xx_serial_init(struct platform_driver *drv,
  824. struct s3c24xx_uart_info *info)
  825. {
  826. dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
  827. #ifdef CONFIG_PM
  828. drv->suspend = s3c24xx_serial_suspend;
  829. drv->resume = s3c24xx_serial_resume;
  830. #endif
  831. return platform_driver_register(drv);
  832. }
  833. EXPORT_SYMBOL_GPL(s3c24xx_serial_init);
  834. /* module initialisation code */
  835. static int __init s3c24xx_serial_modinit(void)
  836. {
  837. int ret;
  838. ret = uart_register_driver(&s3c24xx_uart_drv);
  839. if (ret < 0) {
  840. printk(KERN_ERR "failed to register UART driver\n");
  841. return -1;
  842. }
  843. return 0;
  844. }
  845. static void __exit s3c24xx_serial_modexit(void)
  846. {
  847. uart_unregister_driver(&s3c24xx_uart_drv);
  848. }
  849. module_init(s3c24xx_serial_modinit);
  850. module_exit(s3c24xx_serial_modexit);
  851. /* Console code */
  852. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  853. static struct uart_port *cons_uart;
  854. static int
  855. s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
  856. {
  857. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  858. unsigned long ufstat, utrstat;
  859. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  860. /* fifo mode - check ammount of data in fifo registers... */
  861. ufstat = rd_regl(port, S3C2410_UFSTAT);
  862. return (ufstat & info->tx_fifofull) ? 0 : 1;
  863. }
  864. /* in non-fifo mode, we go and use the tx buffer empty */
  865. utrstat = rd_regl(port, S3C2410_UTRSTAT);
  866. return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
  867. }
  868. static void
  869. s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
  870. {
  871. unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
  872. while (!s3c24xx_serial_console_txrdy(port, ufcon))
  873. barrier();
  874. wr_regb(cons_uart, S3C2410_UTXH, ch);
  875. }
  876. static void
  877. s3c24xx_serial_console_write(struct console *co, const char *s,
  878. unsigned int count)
  879. {
  880. uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
  881. }
  882. static void __init
  883. s3c24xx_serial_get_options(struct uart_port *port, int *baud,
  884. int *parity, int *bits)
  885. {
  886. struct s3c24xx_uart_clksrc clksrc;
  887. struct clk *clk;
  888. unsigned int ulcon;
  889. unsigned int ucon;
  890. unsigned int ubrdiv;
  891. unsigned long rate;
  892. ulcon = rd_regl(port, S3C2410_ULCON);
  893. ucon = rd_regl(port, S3C2410_UCON);
  894. ubrdiv = rd_regl(port, S3C2410_UBRDIV);
  895. dbg("s3c24xx_serial_get_options: port=%p\n"
  896. "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
  897. port, ulcon, ucon, ubrdiv);
  898. if ((ucon & 0xf) != 0) {
  899. /* consider the serial port configured if the tx/rx mode set */
  900. switch (ulcon & S3C2410_LCON_CSMASK) {
  901. case S3C2410_LCON_CS5:
  902. *bits = 5;
  903. break;
  904. case S3C2410_LCON_CS6:
  905. *bits = 6;
  906. break;
  907. case S3C2410_LCON_CS7:
  908. *bits = 7;
  909. break;
  910. default:
  911. case S3C2410_LCON_CS8:
  912. *bits = 8;
  913. break;
  914. }
  915. switch (ulcon & S3C2410_LCON_PMASK) {
  916. case S3C2410_LCON_PEVEN:
  917. *parity = 'e';
  918. break;
  919. case S3C2410_LCON_PODD:
  920. *parity = 'o';
  921. break;
  922. case S3C2410_LCON_PNONE:
  923. default:
  924. *parity = 'n';
  925. }
  926. /* now calculate the baud rate */
  927. s3c24xx_serial_getsource(port, &clksrc);
  928. clk = clk_get(port->dev, clksrc.name);
  929. if (!IS_ERR(clk) && clk != NULL)
  930. rate = clk_get_rate(clk) / clksrc.divisor;
  931. else
  932. rate = 1;
  933. *baud = rate / (16 * (ubrdiv + 1));
  934. dbg("calculated baud %d\n", *baud);
  935. }
  936. }
  937. /* s3c24xx_serial_init_ports
  938. *
  939. * initialise the serial ports from the machine provided initialisation
  940. * data.
  941. */
  942. static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info *info)
  943. {
  944. struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
  945. struct platform_device **platdev_ptr;
  946. int i;
  947. dbg("s3c24xx_serial_init_ports: initialising ports...\n");
  948. platdev_ptr = s3c24xx_uart_devs;
  949. for (i = 0; i < NR_PORTS; i++, ptr++, platdev_ptr++) {
  950. s3c24xx_serial_init_port(ptr, info, *platdev_ptr);
  951. }
  952. return 0;
  953. }
  954. static int __init
  955. s3c24xx_serial_console_setup(struct console *co, char *options)
  956. {
  957. struct uart_port *port;
  958. int baud = 9600;
  959. int bits = 8;
  960. int parity = 'n';
  961. int flow = 'n';
  962. dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
  963. co, co->index, options);
  964. /* is this a valid port */
  965. if (co->index == -1 || co->index >= NR_PORTS)
  966. co->index = 0;
  967. port = &s3c24xx_serial_ports[co->index].port;
  968. /* is the port configured? */
  969. if (port->mapbase == 0x0) {
  970. co->index = 0;
  971. port = &s3c24xx_serial_ports[co->index].port;
  972. }
  973. cons_uart = port;
  974. dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
  975. /*
  976. * Check whether an invalid uart number has been specified, and
  977. * if so, search for the first available port that does have
  978. * console support.
  979. */
  980. if (options)
  981. uart_parse_options(options, &baud, &parity, &bits, &flow);
  982. else
  983. s3c24xx_serial_get_options(port, &baud, &parity, &bits);
  984. dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
  985. return uart_set_options(port, co, baud, parity, bits, flow);
  986. }
  987. /* s3c24xx_serial_initconsole
  988. *
  989. * initialise the console from one of the uart drivers
  990. */
  991. static struct console s3c24xx_serial_console = {
  992. .name = S3C24XX_SERIAL_NAME,
  993. .device = uart_console_device,
  994. .flags = CON_PRINTBUFFER,
  995. .index = -1,
  996. .write = s3c24xx_serial_console_write,
  997. .setup = s3c24xx_serial_console_setup
  998. };
  999. int s3c24xx_serial_initconsole(struct platform_driver *drv,
  1000. struct s3c24xx_uart_info *info)
  1001. {
  1002. struct platform_device *dev = s3c24xx_uart_devs[0];
  1003. dbg("s3c24xx_serial_initconsole\n");
  1004. /* select driver based on the cpu */
  1005. if (dev == NULL) {
  1006. printk(KERN_ERR "s3c24xx: no devices for console init\n");
  1007. return 0;
  1008. }
  1009. if (strcmp(dev->name, drv->driver.name) != 0)
  1010. return 0;
  1011. s3c24xx_serial_console.data = &s3c24xx_uart_drv;
  1012. s3c24xx_serial_init_ports(info);
  1013. register_console(&s3c24xx_serial_console);
  1014. return 0;
  1015. }
  1016. #endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
  1017. MODULE_DESCRIPTION("Samsung SoC Serial port driver");
  1018. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  1019. MODULE_LICENSE("GPL v2");