samsung.c 34 KB

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