samsung.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  1. /* linux/drivers/serial/samsuing.c
  2. *
  3. * Driver core for Samsung SoC onboard UARTs.
  4. *
  5. * Ben Dooks, Copyright (c) 2003-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->state->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->state->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. if (cfg->has_fracval) {
  551. udivslot = (div & 15);
  552. dbg("fracval = %04x\n", udivslot);
  553. } else {
  554. udivslot = udivslot_table[div & 15];
  555. dbg("udivslot = %04x (div %d)\n", udivslot, div & 15);
  556. }
  557. }
  558. switch (termios->c_cflag & CSIZE) {
  559. case CS5:
  560. dbg("config: 5bits/char\n");
  561. ulcon = S3C2410_LCON_CS5;
  562. break;
  563. case CS6:
  564. dbg("config: 6bits/char\n");
  565. ulcon = S3C2410_LCON_CS6;
  566. break;
  567. case CS7:
  568. dbg("config: 7bits/char\n");
  569. ulcon = S3C2410_LCON_CS7;
  570. break;
  571. case CS8:
  572. default:
  573. dbg("config: 8bits/char\n");
  574. ulcon = S3C2410_LCON_CS8;
  575. break;
  576. }
  577. /* preserve original lcon IR settings */
  578. ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
  579. if (termios->c_cflag & CSTOPB)
  580. ulcon |= S3C2410_LCON_STOPB;
  581. umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
  582. if (termios->c_cflag & PARENB) {
  583. if (termios->c_cflag & PARODD)
  584. ulcon |= S3C2410_LCON_PODD;
  585. else
  586. ulcon |= S3C2410_LCON_PEVEN;
  587. } else {
  588. ulcon |= S3C2410_LCON_PNONE;
  589. }
  590. spin_lock_irqsave(&port->lock, flags);
  591. dbg("setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
  592. ulcon, quot, udivslot);
  593. wr_regl(port, S3C2410_ULCON, ulcon);
  594. wr_regl(port, S3C2410_UBRDIV, quot);
  595. wr_regl(port, S3C2410_UMCON, umcon);
  596. if (ourport->info->has_divslot)
  597. wr_regl(port, S3C2443_DIVSLOT, udivslot);
  598. dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
  599. rd_regl(port, S3C2410_ULCON),
  600. rd_regl(port, S3C2410_UCON),
  601. rd_regl(port, S3C2410_UFCON));
  602. /*
  603. * Update the per-port timeout.
  604. */
  605. uart_update_timeout(port, termios->c_cflag, baud);
  606. /*
  607. * Which character status flags are we interested in?
  608. */
  609. port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
  610. if (termios->c_iflag & INPCK)
  611. port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
  612. /*
  613. * Which character status flags should we ignore?
  614. */
  615. port->ignore_status_mask = 0;
  616. if (termios->c_iflag & IGNPAR)
  617. port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
  618. if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
  619. port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
  620. /*
  621. * Ignore all characters if CREAD is not set.
  622. */
  623. if ((termios->c_cflag & CREAD) == 0)
  624. port->ignore_status_mask |= RXSTAT_DUMMY_READ;
  625. spin_unlock_irqrestore(&port->lock, flags);
  626. }
  627. static const char *s3c24xx_serial_type(struct uart_port *port)
  628. {
  629. switch (port->type) {
  630. case PORT_S3C2410:
  631. return "S3C2410";
  632. case PORT_S3C2440:
  633. return "S3C2440";
  634. case PORT_S3C2412:
  635. return "S3C2412";
  636. case PORT_S3C6400:
  637. return "S3C6400/10";
  638. default:
  639. return NULL;
  640. }
  641. }
  642. #define MAP_SIZE (0x100)
  643. static void s3c24xx_serial_release_port(struct uart_port *port)
  644. {
  645. release_mem_region(port->mapbase, MAP_SIZE);
  646. }
  647. static int s3c24xx_serial_request_port(struct uart_port *port)
  648. {
  649. const char *name = s3c24xx_serial_portname(port);
  650. return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
  651. }
  652. static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
  653. {
  654. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  655. if (flags & UART_CONFIG_TYPE &&
  656. s3c24xx_serial_request_port(port) == 0)
  657. port->type = info->type;
  658. }
  659. /*
  660. * verify the new serial_struct (for TIOCSSERIAL).
  661. */
  662. static int
  663. s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  664. {
  665. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  666. if (ser->type != PORT_UNKNOWN && ser->type != info->type)
  667. return -EINVAL;
  668. return 0;
  669. }
  670. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  671. static struct console s3c24xx_serial_console;
  672. #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
  673. #else
  674. #define S3C24XX_SERIAL_CONSOLE NULL
  675. #endif
  676. static struct uart_ops s3c24xx_serial_ops = {
  677. .pm = s3c24xx_serial_pm,
  678. .tx_empty = s3c24xx_serial_tx_empty,
  679. .get_mctrl = s3c24xx_serial_get_mctrl,
  680. .set_mctrl = s3c24xx_serial_set_mctrl,
  681. .stop_tx = s3c24xx_serial_stop_tx,
  682. .start_tx = s3c24xx_serial_start_tx,
  683. .stop_rx = s3c24xx_serial_stop_rx,
  684. .enable_ms = s3c24xx_serial_enable_ms,
  685. .break_ctl = s3c24xx_serial_break_ctl,
  686. .startup = s3c24xx_serial_startup,
  687. .shutdown = s3c24xx_serial_shutdown,
  688. .set_termios = s3c24xx_serial_set_termios,
  689. .type = s3c24xx_serial_type,
  690. .release_port = s3c24xx_serial_release_port,
  691. .request_port = s3c24xx_serial_request_port,
  692. .config_port = s3c24xx_serial_config_port,
  693. .verify_port = s3c24xx_serial_verify_port,
  694. };
  695. static struct uart_driver s3c24xx_uart_drv = {
  696. .owner = THIS_MODULE,
  697. .dev_name = "s3c2410_serial",
  698. .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
  699. .cons = S3C24XX_SERIAL_CONSOLE,
  700. .driver_name = S3C24XX_SERIAL_NAME,
  701. .major = S3C24XX_SERIAL_MAJOR,
  702. .minor = S3C24XX_SERIAL_MINOR,
  703. };
  704. static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
  705. [0] = {
  706. .port = {
  707. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
  708. .iotype = UPIO_MEM,
  709. .irq = IRQ_S3CUART_RX0,
  710. .uartclk = 0,
  711. .fifosize = 16,
  712. .ops = &s3c24xx_serial_ops,
  713. .flags = UPF_BOOT_AUTOCONF,
  714. .line = 0,
  715. }
  716. },
  717. [1] = {
  718. .port = {
  719. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
  720. .iotype = UPIO_MEM,
  721. .irq = IRQ_S3CUART_RX1,
  722. .uartclk = 0,
  723. .fifosize = 16,
  724. .ops = &s3c24xx_serial_ops,
  725. .flags = UPF_BOOT_AUTOCONF,
  726. .line = 1,
  727. }
  728. },
  729. #if CONFIG_SERIAL_SAMSUNG_UARTS > 2
  730. [2] = {
  731. .port = {
  732. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
  733. .iotype = UPIO_MEM,
  734. .irq = IRQ_S3CUART_RX2,
  735. .uartclk = 0,
  736. .fifosize = 16,
  737. .ops = &s3c24xx_serial_ops,
  738. .flags = UPF_BOOT_AUTOCONF,
  739. .line = 2,
  740. }
  741. },
  742. #endif
  743. #if CONFIG_SERIAL_SAMSUNG_UARTS > 3
  744. [3] = {
  745. .port = {
  746. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[3].port.lock),
  747. .iotype = UPIO_MEM,
  748. .irq = IRQ_S3CUART_RX3,
  749. .uartclk = 0,
  750. .fifosize = 16,
  751. .ops = &s3c24xx_serial_ops,
  752. .flags = UPF_BOOT_AUTOCONF,
  753. .line = 3,
  754. }
  755. }
  756. #endif
  757. };
  758. /* s3c24xx_serial_resetport
  759. *
  760. * wrapper to call the specific reset for this port (reset the fifos
  761. * and the settings)
  762. */
  763. static inline int s3c24xx_serial_resetport(struct uart_port *port,
  764. struct s3c2410_uartcfg *cfg)
  765. {
  766. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  767. return (info->reset_port)(port, cfg);
  768. }
  769. #ifdef CONFIG_CPU_FREQ
  770. static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
  771. unsigned long val, void *data)
  772. {
  773. struct s3c24xx_uart_port *port;
  774. struct uart_port *uport;
  775. port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
  776. uport = &port->port;
  777. /* check to see if port is enabled */
  778. if (port->pm_level != 0)
  779. return 0;
  780. /* try and work out if the baudrate is changing, we can detect
  781. * a change in rate, but we do not have support for detecting
  782. * a disturbance in the clock-rate over the change.
  783. */
  784. if (IS_ERR(port->clk))
  785. goto exit;
  786. if (port->baudclk_rate == clk_get_rate(port->clk))
  787. goto exit;
  788. if (val == CPUFREQ_PRECHANGE) {
  789. /* we should really shut the port down whilst the
  790. * frequency change is in progress. */
  791. } else if (val == CPUFREQ_POSTCHANGE) {
  792. struct ktermios *termios;
  793. struct tty_struct *tty;
  794. if (uport->state == NULL)
  795. goto exit;
  796. tty = uport->state->port.tty;
  797. if (tty == NULL)
  798. goto exit;
  799. termios = tty->termios;
  800. if (termios == NULL) {
  801. printk(KERN_WARNING "%s: no termios?\n", __func__);
  802. goto exit;
  803. }
  804. s3c24xx_serial_set_termios(uport, termios, NULL);
  805. }
  806. exit:
  807. return 0;
  808. }
  809. static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
  810. {
  811. port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
  812. return cpufreq_register_notifier(&port->freq_transition,
  813. CPUFREQ_TRANSITION_NOTIFIER);
  814. }
  815. static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
  816. {
  817. cpufreq_unregister_notifier(&port->freq_transition,
  818. CPUFREQ_TRANSITION_NOTIFIER);
  819. }
  820. #else
  821. static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
  822. {
  823. return 0;
  824. }
  825. static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
  826. {
  827. }
  828. #endif
  829. /* s3c24xx_serial_init_port
  830. *
  831. * initialise a single serial port from the platform device given
  832. */
  833. static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
  834. struct s3c24xx_uart_info *info,
  835. struct platform_device *platdev)
  836. {
  837. struct uart_port *port = &ourport->port;
  838. struct s3c2410_uartcfg *cfg;
  839. struct resource *res;
  840. int ret;
  841. dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
  842. if (platdev == NULL)
  843. return -ENODEV;
  844. cfg = s3c24xx_dev_to_cfg(&platdev->dev);
  845. if (port->mapbase != 0)
  846. return 0;
  847. if (cfg->hwport > CONFIG_SERIAL_SAMSUNG_UARTS) {
  848. printk(KERN_ERR "%s: port %d bigger than %d\n", __func__,
  849. cfg->hwport, CONFIG_SERIAL_SAMSUNG_UARTS);
  850. return -ERANGE;
  851. }
  852. /* setup info for port */
  853. port->dev = &platdev->dev;
  854. ourport->info = info;
  855. /* copy the info in from provided structure */
  856. ourport->port.fifosize = info->fifosize;
  857. dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
  858. port->uartclk = 1;
  859. if (cfg->uart_flags & UPF_CONS_FLOW) {
  860. dbg("s3c24xx_serial_init_port: enabling flow control\n");
  861. port->flags |= UPF_CONS_FLOW;
  862. }
  863. /* sort our the physical and virtual addresses for each UART */
  864. res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
  865. if (res == NULL) {
  866. printk(KERN_ERR "failed to find memory resource for uart\n");
  867. return -EINVAL;
  868. }
  869. dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
  870. port->mapbase = res->start;
  871. port->membase = S3C_VA_UART + (res->start & 0xfffff);
  872. ret = platform_get_irq(platdev, 0);
  873. if (ret < 0)
  874. port->irq = 0;
  875. else {
  876. port->irq = ret;
  877. ourport->rx_irq = ret;
  878. ourport->tx_irq = ret + 1;
  879. }
  880. ret = platform_get_irq(platdev, 1);
  881. if (ret > 0)
  882. ourport->tx_irq = ret;
  883. ourport->clk = clk_get(&platdev->dev, "uart");
  884. dbg("port: map=%08x, mem=%08x, irq=%d (%d,%d), clock=%ld\n",
  885. port->mapbase, port->membase, port->irq,
  886. ourport->rx_irq, ourport->tx_irq, port->uartclk);
  887. /* reset the fifos (and setup the uart) */
  888. s3c24xx_serial_resetport(port, cfg);
  889. return 0;
  890. }
  891. static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
  892. struct device_attribute *attr,
  893. char *buf)
  894. {
  895. struct uart_port *port = s3c24xx_dev_to_port(dev);
  896. struct s3c24xx_uart_port *ourport = to_ourport(port);
  897. return snprintf(buf, PAGE_SIZE, "* %s\n", ourport->clksrc->name);
  898. }
  899. static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL);
  900. /* Device driver serial port probe */
  901. static int probe_index;
  902. int s3c24xx_serial_probe(struct platform_device *dev,
  903. struct s3c24xx_uart_info *info)
  904. {
  905. struct s3c24xx_uart_port *ourport;
  906. int ret;
  907. dbg("s3c24xx_serial_probe(%p, %p) %d\n", dev, info, probe_index);
  908. ourport = &s3c24xx_serial_ports[probe_index];
  909. probe_index++;
  910. dbg("%s: initialising port %p...\n", __func__, ourport);
  911. ret = s3c24xx_serial_init_port(ourport, info, dev);
  912. if (ret < 0)
  913. goto probe_err;
  914. dbg("%s: adding port\n", __func__);
  915. uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
  916. platform_set_drvdata(dev, &ourport->port);
  917. ret = device_create_file(&dev->dev, &dev_attr_clock_source);
  918. if (ret < 0)
  919. printk(KERN_ERR "%s: failed to add clksrc attr.\n", __func__);
  920. ret = s3c24xx_serial_cpufreq_register(ourport);
  921. if (ret < 0)
  922. dev_err(&dev->dev, "failed to add cpufreq notifier\n");
  923. return 0;
  924. probe_err:
  925. return ret;
  926. }
  927. EXPORT_SYMBOL_GPL(s3c24xx_serial_probe);
  928. int __devexit s3c24xx_serial_remove(struct platform_device *dev)
  929. {
  930. struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
  931. if (port) {
  932. s3c24xx_serial_cpufreq_deregister(to_ourport(port));
  933. device_remove_file(&dev->dev, &dev_attr_clock_source);
  934. uart_remove_one_port(&s3c24xx_uart_drv, port);
  935. }
  936. return 0;
  937. }
  938. EXPORT_SYMBOL_GPL(s3c24xx_serial_remove);
  939. /* UART power management code */
  940. #ifdef CONFIG_PM
  941. static int s3c24xx_serial_suspend(struct platform_device *dev, pm_message_t state)
  942. {
  943. struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
  944. if (port)
  945. uart_suspend_port(&s3c24xx_uart_drv, port);
  946. return 0;
  947. }
  948. static int s3c24xx_serial_resume(struct platform_device *dev)
  949. {
  950. struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
  951. struct s3c24xx_uart_port *ourport = to_ourport(port);
  952. if (port) {
  953. clk_enable(ourport->clk);
  954. s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
  955. clk_disable(ourport->clk);
  956. uart_resume_port(&s3c24xx_uart_drv, port);
  957. }
  958. return 0;
  959. }
  960. #endif
  961. int s3c24xx_serial_init(struct platform_driver *drv,
  962. struct s3c24xx_uart_info *info)
  963. {
  964. dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
  965. #ifdef CONFIG_PM
  966. drv->suspend = s3c24xx_serial_suspend;
  967. drv->resume = s3c24xx_serial_resume;
  968. #endif
  969. return platform_driver_register(drv);
  970. }
  971. EXPORT_SYMBOL_GPL(s3c24xx_serial_init);
  972. /* module initialisation code */
  973. static int __init s3c24xx_serial_modinit(void)
  974. {
  975. int ret;
  976. ret = uart_register_driver(&s3c24xx_uart_drv);
  977. if (ret < 0) {
  978. printk(KERN_ERR "failed to register UART driver\n");
  979. return -1;
  980. }
  981. return 0;
  982. }
  983. static void __exit s3c24xx_serial_modexit(void)
  984. {
  985. uart_unregister_driver(&s3c24xx_uart_drv);
  986. }
  987. module_init(s3c24xx_serial_modinit);
  988. module_exit(s3c24xx_serial_modexit);
  989. /* Console code */
  990. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  991. static struct uart_port *cons_uart;
  992. static int
  993. s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
  994. {
  995. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  996. unsigned long ufstat, utrstat;
  997. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  998. /* fifo mode - check amount of data in fifo registers... */
  999. ufstat = rd_regl(port, S3C2410_UFSTAT);
  1000. return (ufstat & info->tx_fifofull) ? 0 : 1;
  1001. }
  1002. /* in non-fifo mode, we go and use the tx buffer empty */
  1003. utrstat = rd_regl(port, S3C2410_UTRSTAT);
  1004. return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
  1005. }
  1006. static void
  1007. s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
  1008. {
  1009. unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
  1010. while (!s3c24xx_serial_console_txrdy(port, ufcon))
  1011. barrier();
  1012. wr_regb(cons_uart, S3C2410_UTXH, ch);
  1013. }
  1014. static void
  1015. s3c24xx_serial_console_write(struct console *co, const char *s,
  1016. unsigned int count)
  1017. {
  1018. uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
  1019. }
  1020. static void __init
  1021. s3c24xx_serial_get_options(struct uart_port *port, int *baud,
  1022. int *parity, int *bits)
  1023. {
  1024. struct s3c24xx_uart_clksrc clksrc;
  1025. struct clk *clk;
  1026. unsigned int ulcon;
  1027. unsigned int ucon;
  1028. unsigned int ubrdiv;
  1029. unsigned long rate;
  1030. ulcon = rd_regl(port, S3C2410_ULCON);
  1031. ucon = rd_regl(port, S3C2410_UCON);
  1032. ubrdiv = rd_regl(port, S3C2410_UBRDIV);
  1033. dbg("s3c24xx_serial_get_options: port=%p\n"
  1034. "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
  1035. port, ulcon, ucon, ubrdiv);
  1036. if ((ucon & 0xf) != 0) {
  1037. /* consider the serial port configured if the tx/rx mode set */
  1038. switch (ulcon & S3C2410_LCON_CSMASK) {
  1039. case S3C2410_LCON_CS5:
  1040. *bits = 5;
  1041. break;
  1042. case S3C2410_LCON_CS6:
  1043. *bits = 6;
  1044. break;
  1045. case S3C2410_LCON_CS7:
  1046. *bits = 7;
  1047. break;
  1048. default:
  1049. case S3C2410_LCON_CS8:
  1050. *bits = 8;
  1051. break;
  1052. }
  1053. switch (ulcon & S3C2410_LCON_PMASK) {
  1054. case S3C2410_LCON_PEVEN:
  1055. *parity = 'e';
  1056. break;
  1057. case S3C2410_LCON_PODD:
  1058. *parity = 'o';
  1059. break;
  1060. case S3C2410_LCON_PNONE:
  1061. default:
  1062. *parity = 'n';
  1063. }
  1064. /* now calculate the baud rate */
  1065. s3c24xx_serial_getsource(port, &clksrc);
  1066. clk = clk_get(port->dev, clksrc.name);
  1067. if (!IS_ERR(clk) && clk != NULL)
  1068. rate = clk_get_rate(clk) / clksrc.divisor;
  1069. else
  1070. rate = 1;
  1071. *baud = rate / (16 * (ubrdiv + 1));
  1072. dbg("calculated baud %d\n", *baud);
  1073. }
  1074. }
  1075. /* s3c24xx_serial_init_ports
  1076. *
  1077. * initialise the serial ports from the machine provided initialisation
  1078. * data.
  1079. */
  1080. static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info **info)
  1081. {
  1082. struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
  1083. struct platform_device **platdev_ptr;
  1084. int i;
  1085. dbg("s3c24xx_serial_init_ports: initialising ports...\n");
  1086. platdev_ptr = s3c24xx_uart_devs;
  1087. for (i = 0; i < CONFIG_SERIAL_SAMSUNG_UARTS; i++, ptr++, platdev_ptr++) {
  1088. s3c24xx_serial_init_port(ptr, info[i], *platdev_ptr);
  1089. }
  1090. return 0;
  1091. }
  1092. static int __init
  1093. s3c24xx_serial_console_setup(struct console *co, char *options)
  1094. {
  1095. struct uart_port *port;
  1096. int baud = 9600;
  1097. int bits = 8;
  1098. int parity = 'n';
  1099. int flow = 'n';
  1100. dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
  1101. co, co->index, options);
  1102. /* is this a valid port */
  1103. if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
  1104. co->index = 0;
  1105. port = &s3c24xx_serial_ports[co->index].port;
  1106. /* is the port configured? */
  1107. if (port->mapbase == 0x0) {
  1108. co->index = 0;
  1109. port = &s3c24xx_serial_ports[co->index].port;
  1110. }
  1111. cons_uart = port;
  1112. dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
  1113. /*
  1114. * Check whether an invalid uart number has been specified, and
  1115. * if so, search for the first available port that does have
  1116. * console support.
  1117. */
  1118. if (options)
  1119. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1120. else
  1121. s3c24xx_serial_get_options(port, &baud, &parity, &bits);
  1122. dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
  1123. return uart_set_options(port, co, baud, parity, bits, flow);
  1124. }
  1125. /* s3c24xx_serial_initconsole
  1126. *
  1127. * initialise the console from one of the uart drivers
  1128. */
  1129. static struct console s3c24xx_serial_console = {
  1130. .name = S3C24XX_SERIAL_NAME,
  1131. .device = uart_console_device,
  1132. .flags = CON_PRINTBUFFER,
  1133. .index = -1,
  1134. .write = s3c24xx_serial_console_write,
  1135. .setup = s3c24xx_serial_console_setup
  1136. };
  1137. int s3c24xx_serial_initconsole(struct platform_driver *drv,
  1138. struct s3c24xx_uart_info **info)
  1139. {
  1140. struct platform_device *dev = s3c24xx_uart_devs[0];
  1141. dbg("s3c24xx_serial_initconsole\n");
  1142. /* select driver based on the cpu */
  1143. if (dev == NULL) {
  1144. printk(KERN_ERR "s3c24xx: no devices for console init\n");
  1145. return 0;
  1146. }
  1147. if (strcmp(dev->name, drv->driver.name) != 0)
  1148. return 0;
  1149. s3c24xx_serial_console.data = &s3c24xx_uart_drv;
  1150. s3c24xx_serial_init_ports(info);
  1151. register_console(&s3c24xx_serial_console);
  1152. return 0;
  1153. }
  1154. #endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
  1155. MODULE_DESCRIPTION("Samsung SoC Serial port driver");
  1156. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  1157. MODULE_LICENSE("GPL v2");