samsung.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  1. /*
  2. * Driver core for Samsung SoC onboard UARTs.
  3. *
  4. * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
  5. * http://armlinux.simtec.co.uk/
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. /* Hote on 2410 error handling
  12. *
  13. * The s3c2410 manual has a love/hate affair with the contents of the
  14. * UERSTAT register in the UART blocks, and keeps marking some of the
  15. * error bits as reserved. Having checked with the s3c2410x01,
  16. * it copes with BREAKs properly, so I am happy to ignore the RESERVED
  17. * feature from the latter versions of the manual.
  18. *
  19. * If it becomes aparrent that latter versions of the 2410 remove these
  20. * bits, then action will have to be taken to differentiate the versions
  21. * and change the policy on BREAK
  22. *
  23. * BJD, 04-Nov-2004
  24. */
  25. #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  26. #define SUPPORT_SYSRQ
  27. #endif
  28. #include <linux/module.h>
  29. #include <linux/ioport.h>
  30. #include <linux/io.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/init.h>
  33. #include <linux/sysrq.h>
  34. #include <linux/console.h>
  35. #include <linux/tty.h>
  36. #include <linux/tty_flip.h>
  37. #include <linux/serial_core.h>
  38. #include <linux/serial.h>
  39. #include <linux/delay.h>
  40. #include <linux/clk.h>
  41. #include <linux/cpufreq.h>
  42. #include <linux/of.h>
  43. #include <asm/irq.h>
  44. #include <mach/hardware.h>
  45. #include <mach/map.h>
  46. #include <plat/regs-serial.h>
  47. #include <plat/clock.h>
  48. #include "samsung.h"
  49. /* UART name and device definitions */
  50. #define S3C24XX_SERIAL_NAME "ttySAC"
  51. #define S3C24XX_SERIAL_MAJOR 204
  52. #define S3C24XX_SERIAL_MINOR 64
  53. /* macros to change one thing to another */
  54. #define tx_enabled(port) ((port)->unused[0])
  55. #define rx_enabled(port) ((port)->unused[1])
  56. /* flag to ignore all characters coming in */
  57. #define RXSTAT_DUMMY_READ (0x10000000)
  58. static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
  59. {
  60. return container_of(port, struct s3c24xx_uart_port, port);
  61. }
  62. /* translate a port to the device name */
  63. static inline const char *s3c24xx_serial_portname(struct uart_port *port)
  64. {
  65. return to_platform_device(port->dev)->name;
  66. }
  67. static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
  68. {
  69. return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);
  70. }
  71. /*
  72. * s3c64xx and later SoC's include the interrupt mask and status registers in
  73. * the controller itself, unlike the s3c24xx SoC's which have these registers
  74. * in the interrupt controller. Check if the port type is s3c64xx or higher.
  75. */
  76. static int s3c24xx_serial_has_interrupt_mask(struct uart_port *port)
  77. {
  78. return to_ourport(port)->info->type == PORT_S3C6400;
  79. }
  80. static void s3c24xx_serial_rx_enable(struct uart_port *port)
  81. {
  82. unsigned long flags;
  83. unsigned int ucon, ufcon;
  84. int count = 10000;
  85. spin_lock_irqsave(&port->lock, flags);
  86. while (--count && !s3c24xx_serial_txempty_nofifo(port))
  87. udelay(100);
  88. ufcon = rd_regl(port, S3C2410_UFCON);
  89. ufcon |= S3C2410_UFCON_RESETRX;
  90. wr_regl(port, S3C2410_UFCON, ufcon);
  91. ucon = rd_regl(port, S3C2410_UCON);
  92. ucon |= S3C2410_UCON_RXIRQMODE;
  93. wr_regl(port, S3C2410_UCON, ucon);
  94. rx_enabled(port) = 1;
  95. spin_unlock_irqrestore(&port->lock, flags);
  96. }
  97. static void s3c24xx_serial_rx_disable(struct uart_port *port)
  98. {
  99. unsigned long flags;
  100. unsigned int ucon;
  101. spin_lock_irqsave(&port->lock, flags);
  102. ucon = rd_regl(port, S3C2410_UCON);
  103. ucon &= ~S3C2410_UCON_RXIRQMODE;
  104. wr_regl(port, S3C2410_UCON, ucon);
  105. rx_enabled(port) = 0;
  106. spin_unlock_irqrestore(&port->lock, flags);
  107. }
  108. static void s3c24xx_serial_stop_tx(struct uart_port *port)
  109. {
  110. struct s3c24xx_uart_port *ourport = to_ourport(port);
  111. if (tx_enabled(port)) {
  112. if (s3c24xx_serial_has_interrupt_mask(port))
  113. __set_bit(S3C64XX_UINTM_TXD,
  114. portaddrl(port, S3C64XX_UINTM));
  115. else
  116. disable_irq_nosync(ourport->tx_irq);
  117. tx_enabled(port) = 0;
  118. if (port->flags & UPF_CONS_FLOW)
  119. s3c24xx_serial_rx_enable(port);
  120. }
  121. }
  122. static void s3c24xx_serial_start_tx(struct uart_port *port)
  123. {
  124. struct s3c24xx_uart_port *ourport = to_ourport(port);
  125. if (!tx_enabled(port)) {
  126. if (port->flags & UPF_CONS_FLOW)
  127. s3c24xx_serial_rx_disable(port);
  128. if (s3c24xx_serial_has_interrupt_mask(port))
  129. __clear_bit(S3C64XX_UINTM_TXD,
  130. portaddrl(port, S3C64XX_UINTM));
  131. else
  132. enable_irq(ourport->tx_irq);
  133. tx_enabled(port) = 1;
  134. }
  135. }
  136. static void s3c24xx_serial_stop_rx(struct uart_port *port)
  137. {
  138. struct s3c24xx_uart_port *ourport = to_ourport(port);
  139. if (rx_enabled(port)) {
  140. dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
  141. if (s3c24xx_serial_has_interrupt_mask(port))
  142. __set_bit(S3C64XX_UINTM_RXD,
  143. portaddrl(port, S3C64XX_UINTM));
  144. else
  145. disable_irq_nosync(ourport->rx_irq);
  146. rx_enabled(port) = 0;
  147. }
  148. }
  149. static void s3c24xx_serial_enable_ms(struct uart_port *port)
  150. {
  151. }
  152. static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
  153. {
  154. return to_ourport(port)->info;
  155. }
  156. static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
  157. {
  158. struct s3c24xx_uart_port *ourport;
  159. if (port->dev == NULL)
  160. return NULL;
  161. ourport = container_of(port, struct s3c24xx_uart_port, port);
  162. return ourport->cfg;
  163. }
  164. static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
  165. unsigned long ufstat)
  166. {
  167. struct s3c24xx_uart_info *info = ourport->info;
  168. if (ufstat & info->rx_fifofull)
  169. return ourport->port.fifosize;
  170. return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
  171. }
  172. /* ? - where has parity gone?? */
  173. #define S3C2410_UERSTAT_PARITY (0x1000)
  174. static irqreturn_t
  175. s3c24xx_serial_rx_chars(int irq, void *dev_id)
  176. {
  177. struct s3c24xx_uart_port *ourport = dev_id;
  178. struct uart_port *port = &ourport->port;
  179. struct tty_struct *tty = port->state->port.tty;
  180. unsigned int ufcon, ch, flag, ufstat, uerstat;
  181. int max_count = 64;
  182. while (max_count-- > 0) {
  183. ufcon = rd_regl(port, S3C2410_UFCON);
  184. ufstat = rd_regl(port, S3C2410_UFSTAT);
  185. if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
  186. break;
  187. uerstat = rd_regl(port, S3C2410_UERSTAT);
  188. ch = rd_regb(port, S3C2410_URXH);
  189. if (port->flags & UPF_CONS_FLOW) {
  190. int txe = s3c24xx_serial_txempty_nofifo(port);
  191. if (rx_enabled(port)) {
  192. if (!txe) {
  193. rx_enabled(port) = 0;
  194. continue;
  195. }
  196. } else {
  197. if (txe) {
  198. ufcon |= S3C2410_UFCON_RESETRX;
  199. wr_regl(port, S3C2410_UFCON, ufcon);
  200. rx_enabled(port) = 1;
  201. goto out;
  202. }
  203. continue;
  204. }
  205. }
  206. /* insert the character into the buffer */
  207. flag = TTY_NORMAL;
  208. port->icount.rx++;
  209. if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
  210. dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
  211. ch, uerstat);
  212. /* check for break */
  213. if (uerstat & S3C2410_UERSTAT_BREAK) {
  214. dbg("break!\n");
  215. port->icount.brk++;
  216. if (uart_handle_break(port))
  217. goto ignore_char;
  218. }
  219. if (uerstat & S3C2410_UERSTAT_FRAME)
  220. port->icount.frame++;
  221. if (uerstat & S3C2410_UERSTAT_OVERRUN)
  222. port->icount.overrun++;
  223. uerstat &= port->read_status_mask;
  224. if (uerstat & S3C2410_UERSTAT_BREAK)
  225. flag = TTY_BREAK;
  226. else if (uerstat & S3C2410_UERSTAT_PARITY)
  227. flag = TTY_PARITY;
  228. else if (uerstat & (S3C2410_UERSTAT_FRAME |
  229. S3C2410_UERSTAT_OVERRUN))
  230. flag = TTY_FRAME;
  231. }
  232. if (uart_handle_sysrq_char(port, ch))
  233. goto ignore_char;
  234. uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
  235. ch, flag);
  236. ignore_char:
  237. continue;
  238. }
  239. tty_flip_buffer_push(tty);
  240. out:
  241. return IRQ_HANDLED;
  242. }
  243. static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
  244. {
  245. struct s3c24xx_uart_port *ourport = id;
  246. struct uart_port *port = &ourport->port;
  247. struct circ_buf *xmit = &port->state->xmit;
  248. int count = 256;
  249. if (port->x_char) {
  250. wr_regb(port, S3C2410_UTXH, port->x_char);
  251. port->icount.tx++;
  252. port->x_char = 0;
  253. goto out;
  254. }
  255. /* if there isn't anything more to transmit, or the uart is now
  256. * stopped, disable the uart and exit
  257. */
  258. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  259. s3c24xx_serial_stop_tx(port);
  260. goto out;
  261. }
  262. /* try and drain the buffer... */
  263. while (!uart_circ_empty(xmit) && count-- > 0) {
  264. if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
  265. break;
  266. wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
  267. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  268. port->icount.tx++;
  269. }
  270. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  271. uart_write_wakeup(port);
  272. if (uart_circ_empty(xmit))
  273. s3c24xx_serial_stop_tx(port);
  274. out:
  275. return IRQ_HANDLED;
  276. }
  277. /* interrupt handler for s3c64xx and later SoC's.*/
  278. static irqreturn_t s3c64xx_serial_handle_irq(int irq, void *id)
  279. {
  280. struct s3c24xx_uart_port *ourport = id;
  281. struct uart_port *port = &ourport->port;
  282. unsigned int pend = rd_regl(port, S3C64XX_UINTP);
  283. unsigned long flags;
  284. irqreturn_t ret = IRQ_HANDLED;
  285. spin_lock_irqsave(&port->lock, flags);
  286. if (pend & S3C64XX_UINTM_RXD_MSK) {
  287. ret = s3c24xx_serial_rx_chars(irq, id);
  288. wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_RXD_MSK);
  289. }
  290. if (pend & S3C64XX_UINTM_TXD_MSK) {
  291. ret = s3c24xx_serial_tx_chars(irq, id);
  292. wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_TXD_MSK);
  293. }
  294. spin_unlock_irqrestore(&port->lock, flags);
  295. return ret;
  296. }
  297. static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
  298. {
  299. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  300. unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
  301. unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
  302. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  303. if ((ufstat & info->tx_fifomask) != 0 ||
  304. (ufstat & info->tx_fifofull))
  305. return 0;
  306. return 1;
  307. }
  308. return s3c24xx_serial_txempty_nofifo(port);
  309. }
  310. /* no modem control lines */
  311. static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
  312. {
  313. unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
  314. if (umstat & S3C2410_UMSTAT_CTS)
  315. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  316. else
  317. return TIOCM_CAR | TIOCM_DSR;
  318. }
  319. static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  320. {
  321. /* todo - possibly remove AFC and do manual CTS */
  322. }
  323. static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
  324. {
  325. unsigned long flags;
  326. unsigned int ucon;
  327. spin_lock_irqsave(&port->lock, flags);
  328. ucon = rd_regl(port, S3C2410_UCON);
  329. if (break_state)
  330. ucon |= S3C2410_UCON_SBREAK;
  331. else
  332. ucon &= ~S3C2410_UCON_SBREAK;
  333. wr_regl(port, S3C2410_UCON, ucon);
  334. spin_unlock_irqrestore(&port->lock, flags);
  335. }
  336. static void s3c24xx_serial_shutdown(struct uart_port *port)
  337. {
  338. struct s3c24xx_uart_port *ourport = to_ourport(port);
  339. if (ourport->tx_claimed) {
  340. if (!s3c24xx_serial_has_interrupt_mask(port))
  341. free_irq(ourport->tx_irq, ourport);
  342. tx_enabled(port) = 0;
  343. ourport->tx_claimed = 0;
  344. }
  345. if (ourport->rx_claimed) {
  346. if (!s3c24xx_serial_has_interrupt_mask(port))
  347. free_irq(ourport->rx_irq, ourport);
  348. ourport->rx_claimed = 0;
  349. rx_enabled(port) = 0;
  350. }
  351. /* Clear pending interrupts and mask all interrupts */
  352. if (s3c24xx_serial_has_interrupt_mask(port)) {
  353. wr_regl(port, S3C64XX_UINTP, 0xf);
  354. wr_regl(port, S3C64XX_UINTM, 0xf);
  355. }
  356. }
  357. static int s3c24xx_serial_startup(struct uart_port *port)
  358. {
  359. struct s3c24xx_uart_port *ourport = to_ourport(port);
  360. int ret;
  361. dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
  362. port->mapbase, port->membase);
  363. rx_enabled(port) = 1;
  364. ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
  365. s3c24xx_serial_portname(port), ourport);
  366. if (ret != 0) {
  367. printk(KERN_ERR "cannot get irq %d\n", ourport->rx_irq);
  368. return ret;
  369. }
  370. ourport->rx_claimed = 1;
  371. dbg("requesting tx irq...\n");
  372. tx_enabled(port) = 1;
  373. ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
  374. s3c24xx_serial_portname(port), ourport);
  375. if (ret) {
  376. printk(KERN_ERR "cannot get irq %d\n", ourport->tx_irq);
  377. goto err;
  378. }
  379. ourport->tx_claimed = 1;
  380. dbg("s3c24xx_serial_startup ok\n");
  381. /* the port reset code should have done the correct
  382. * register setup for the port controls */
  383. return ret;
  384. err:
  385. s3c24xx_serial_shutdown(port);
  386. return ret;
  387. }
  388. static int s3c64xx_serial_startup(struct uart_port *port)
  389. {
  390. struct s3c24xx_uart_port *ourport = to_ourport(port);
  391. int ret;
  392. dbg("s3c64xx_serial_startup: port=%p (%08lx,%p)\n",
  393. port->mapbase, port->membase);
  394. ret = request_irq(port->irq, s3c64xx_serial_handle_irq, IRQF_SHARED,
  395. s3c24xx_serial_portname(port), ourport);
  396. if (ret) {
  397. printk(KERN_ERR "cannot get irq %d\n", port->irq);
  398. return ret;
  399. }
  400. /* For compatibility with s3c24xx Soc's */
  401. rx_enabled(port) = 1;
  402. ourport->rx_claimed = 1;
  403. tx_enabled(port) = 0;
  404. ourport->tx_claimed = 1;
  405. /* Enable Rx Interrupt */
  406. __clear_bit(S3C64XX_UINTM_RXD, portaddrl(port, S3C64XX_UINTM));
  407. dbg("s3c64xx_serial_startup ok\n");
  408. return ret;
  409. }
  410. /* power power management control */
  411. static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
  412. unsigned int old)
  413. {
  414. struct s3c24xx_uart_port *ourport = to_ourport(port);
  415. ourport->pm_level = level;
  416. switch (level) {
  417. case 3:
  418. if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
  419. clk_disable(ourport->baudclk);
  420. clk_disable(ourport->clk);
  421. break;
  422. case 0:
  423. clk_enable(ourport->clk);
  424. if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
  425. clk_enable(ourport->baudclk);
  426. break;
  427. default:
  428. printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
  429. }
  430. }
  431. /* baud rate calculation
  432. *
  433. * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
  434. * of different sources, including the peripheral clock ("pclk") and an
  435. * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
  436. * with a programmable extra divisor.
  437. *
  438. * The following code goes through the clock sources, and calculates the
  439. * baud clocks (and the resultant actual baud rates) and then tries to
  440. * pick the closest one and select that.
  441. *
  442. */
  443. #define MAX_CLK_NAME_LENGTH 15
  444. static inline int s3c24xx_serial_getsource(struct uart_port *port)
  445. {
  446. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  447. unsigned int ucon;
  448. if (info->num_clks == 1)
  449. return 0;
  450. ucon = rd_regl(port, S3C2410_UCON);
  451. ucon &= info->clksel_mask;
  452. return ucon >> info->clksel_shift;
  453. }
  454. static void s3c24xx_serial_setsource(struct uart_port *port,
  455. unsigned int clk_sel)
  456. {
  457. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  458. unsigned int ucon;
  459. if (info->num_clks == 1)
  460. return;
  461. ucon = rd_regl(port, S3C2410_UCON);
  462. if ((ucon & info->clksel_mask) >> info->clksel_shift == clk_sel)
  463. return;
  464. ucon &= ~info->clksel_mask;
  465. ucon |= clk_sel << info->clksel_shift;
  466. wr_regl(port, S3C2410_UCON, ucon);
  467. }
  468. static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
  469. unsigned int req_baud, struct clk **best_clk,
  470. unsigned int *clk_num)
  471. {
  472. struct s3c24xx_uart_info *info = ourport->info;
  473. struct clk *clk;
  474. unsigned long rate;
  475. unsigned int cnt, baud, quot, clk_sel, best_quot = 0;
  476. char clkname[MAX_CLK_NAME_LENGTH];
  477. int calc_deviation, deviation = (1 << 30) - 1;
  478. *best_clk = NULL;
  479. clk_sel = (ourport->cfg->clk_sel) ? ourport->cfg->clk_sel :
  480. ourport->info->def_clk_sel;
  481. for (cnt = 0; cnt < info->num_clks; cnt++) {
  482. if (!(clk_sel & (1 << cnt)))
  483. continue;
  484. sprintf(clkname, "clk_uart_baud%d", cnt);
  485. clk = clk_get(ourport->port.dev, clkname);
  486. if (IS_ERR_OR_NULL(clk))
  487. continue;
  488. rate = clk_get_rate(clk);
  489. if (!rate)
  490. continue;
  491. if (ourport->info->has_divslot) {
  492. unsigned long div = rate / req_baud;
  493. /* The UDIVSLOT register on the newer UARTs allows us to
  494. * get a divisor adjustment of 1/16th on the baud clock.
  495. *
  496. * We don't keep the UDIVSLOT value (the 16ths we
  497. * calculated by not multiplying the baud by 16) as it
  498. * is easy enough to recalculate.
  499. */
  500. quot = div / 16;
  501. baud = rate / div;
  502. } else {
  503. quot = (rate + (8 * req_baud)) / (16 * req_baud);
  504. baud = rate / (quot * 16);
  505. }
  506. quot--;
  507. calc_deviation = req_baud - baud;
  508. if (calc_deviation < 0)
  509. calc_deviation = -calc_deviation;
  510. if (calc_deviation < deviation) {
  511. *best_clk = clk;
  512. best_quot = quot;
  513. *clk_num = cnt;
  514. deviation = calc_deviation;
  515. }
  516. }
  517. return best_quot;
  518. }
  519. /* udivslot_table[]
  520. *
  521. * This table takes the fractional value of the baud divisor and gives
  522. * the recommended setting for the UDIVSLOT register.
  523. */
  524. static u16 udivslot_table[16] = {
  525. [0] = 0x0000,
  526. [1] = 0x0080,
  527. [2] = 0x0808,
  528. [3] = 0x0888,
  529. [4] = 0x2222,
  530. [5] = 0x4924,
  531. [6] = 0x4A52,
  532. [7] = 0x54AA,
  533. [8] = 0x5555,
  534. [9] = 0xD555,
  535. [10] = 0xD5D5,
  536. [11] = 0xDDD5,
  537. [12] = 0xDDDD,
  538. [13] = 0xDFDD,
  539. [14] = 0xDFDF,
  540. [15] = 0xFFDF,
  541. };
  542. static void s3c24xx_serial_set_termios(struct uart_port *port,
  543. struct ktermios *termios,
  544. struct ktermios *old)
  545. {
  546. struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
  547. struct s3c24xx_uart_port *ourport = to_ourport(port);
  548. struct clk *clk = NULL;
  549. unsigned long flags;
  550. unsigned int baud, quot, clk_sel = 0;
  551. unsigned int ulcon;
  552. unsigned int umcon;
  553. unsigned int udivslot = 0;
  554. /*
  555. * We don't support modem control lines.
  556. */
  557. termios->c_cflag &= ~(HUPCL | CMSPAR);
  558. termios->c_cflag |= CLOCAL;
  559. /*
  560. * Ask the core to calculate the divisor for us.
  561. */
  562. baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
  563. quot = s3c24xx_serial_getclk(ourport, baud, &clk, &clk_sel);
  564. if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
  565. quot = port->custom_divisor;
  566. if (!clk)
  567. return;
  568. /* check to see if we need to change clock source */
  569. if (ourport->baudclk != clk) {
  570. s3c24xx_serial_setsource(port, clk_sel);
  571. if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
  572. clk_disable(ourport->baudclk);
  573. ourport->baudclk = NULL;
  574. }
  575. clk_enable(clk);
  576. ourport->baudclk = clk;
  577. ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
  578. }
  579. if (ourport->info->has_divslot) {
  580. unsigned int div = ourport->baudclk_rate / baud;
  581. if (cfg->has_fracval) {
  582. udivslot = (div & 15);
  583. dbg("fracval = %04x\n", udivslot);
  584. } else {
  585. udivslot = udivslot_table[div & 15];
  586. dbg("udivslot = %04x (div %d)\n", udivslot, div & 15);
  587. }
  588. }
  589. switch (termios->c_cflag & CSIZE) {
  590. case CS5:
  591. dbg("config: 5bits/char\n");
  592. ulcon = S3C2410_LCON_CS5;
  593. break;
  594. case CS6:
  595. dbg("config: 6bits/char\n");
  596. ulcon = S3C2410_LCON_CS6;
  597. break;
  598. case CS7:
  599. dbg("config: 7bits/char\n");
  600. ulcon = S3C2410_LCON_CS7;
  601. break;
  602. case CS8:
  603. default:
  604. dbg("config: 8bits/char\n");
  605. ulcon = S3C2410_LCON_CS8;
  606. break;
  607. }
  608. /* preserve original lcon IR settings */
  609. ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
  610. if (termios->c_cflag & CSTOPB)
  611. ulcon |= S3C2410_LCON_STOPB;
  612. umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
  613. if (termios->c_cflag & PARENB) {
  614. if (termios->c_cflag & PARODD)
  615. ulcon |= S3C2410_LCON_PODD;
  616. else
  617. ulcon |= S3C2410_LCON_PEVEN;
  618. } else {
  619. ulcon |= S3C2410_LCON_PNONE;
  620. }
  621. spin_lock_irqsave(&port->lock, flags);
  622. dbg("setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
  623. ulcon, quot, udivslot);
  624. wr_regl(port, S3C2410_ULCON, ulcon);
  625. wr_regl(port, S3C2410_UBRDIV, quot);
  626. wr_regl(port, S3C2410_UMCON, umcon);
  627. if (ourport->info->has_divslot)
  628. wr_regl(port, S3C2443_DIVSLOT, udivslot);
  629. dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
  630. rd_regl(port, S3C2410_ULCON),
  631. rd_regl(port, S3C2410_UCON),
  632. rd_regl(port, S3C2410_UFCON));
  633. /*
  634. * Update the per-port timeout.
  635. */
  636. uart_update_timeout(port, termios->c_cflag, baud);
  637. /*
  638. * Which character status flags are we interested in?
  639. */
  640. port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
  641. if (termios->c_iflag & INPCK)
  642. port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
  643. /*
  644. * Which character status flags should we ignore?
  645. */
  646. port->ignore_status_mask = 0;
  647. if (termios->c_iflag & IGNPAR)
  648. port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
  649. if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
  650. port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
  651. /*
  652. * Ignore all characters if CREAD is not set.
  653. */
  654. if ((termios->c_cflag & CREAD) == 0)
  655. port->ignore_status_mask |= RXSTAT_DUMMY_READ;
  656. spin_unlock_irqrestore(&port->lock, flags);
  657. }
  658. static const char *s3c24xx_serial_type(struct uart_port *port)
  659. {
  660. switch (port->type) {
  661. case PORT_S3C2410:
  662. return "S3C2410";
  663. case PORT_S3C2440:
  664. return "S3C2440";
  665. case PORT_S3C2412:
  666. return "S3C2412";
  667. case PORT_S3C6400:
  668. return "S3C6400/10";
  669. default:
  670. return NULL;
  671. }
  672. }
  673. #define MAP_SIZE (0x100)
  674. static void s3c24xx_serial_release_port(struct uart_port *port)
  675. {
  676. release_mem_region(port->mapbase, MAP_SIZE);
  677. }
  678. static int s3c24xx_serial_request_port(struct uart_port *port)
  679. {
  680. const char *name = s3c24xx_serial_portname(port);
  681. return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
  682. }
  683. static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
  684. {
  685. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  686. if (flags & UART_CONFIG_TYPE &&
  687. s3c24xx_serial_request_port(port) == 0)
  688. port->type = info->type;
  689. }
  690. /*
  691. * verify the new serial_struct (for TIOCSSERIAL).
  692. */
  693. static int
  694. s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  695. {
  696. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  697. if (ser->type != PORT_UNKNOWN && ser->type != info->type)
  698. return -EINVAL;
  699. return 0;
  700. }
  701. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  702. static struct console s3c24xx_serial_console;
  703. #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
  704. #else
  705. #define S3C24XX_SERIAL_CONSOLE NULL
  706. #endif
  707. static struct uart_ops s3c24xx_serial_ops = {
  708. .pm = s3c24xx_serial_pm,
  709. .tx_empty = s3c24xx_serial_tx_empty,
  710. .get_mctrl = s3c24xx_serial_get_mctrl,
  711. .set_mctrl = s3c24xx_serial_set_mctrl,
  712. .stop_tx = s3c24xx_serial_stop_tx,
  713. .start_tx = s3c24xx_serial_start_tx,
  714. .stop_rx = s3c24xx_serial_stop_rx,
  715. .enable_ms = s3c24xx_serial_enable_ms,
  716. .break_ctl = s3c24xx_serial_break_ctl,
  717. .startup = s3c24xx_serial_startup,
  718. .shutdown = s3c24xx_serial_shutdown,
  719. .set_termios = s3c24xx_serial_set_termios,
  720. .type = s3c24xx_serial_type,
  721. .release_port = s3c24xx_serial_release_port,
  722. .request_port = s3c24xx_serial_request_port,
  723. .config_port = s3c24xx_serial_config_port,
  724. .verify_port = s3c24xx_serial_verify_port,
  725. };
  726. static struct uart_driver s3c24xx_uart_drv = {
  727. .owner = THIS_MODULE,
  728. .driver_name = "s3c2410_serial",
  729. .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
  730. .cons = S3C24XX_SERIAL_CONSOLE,
  731. .dev_name = S3C24XX_SERIAL_NAME,
  732. .major = S3C24XX_SERIAL_MAJOR,
  733. .minor = S3C24XX_SERIAL_MINOR,
  734. };
  735. static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
  736. [0] = {
  737. .port = {
  738. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
  739. .iotype = UPIO_MEM,
  740. .uartclk = 0,
  741. .fifosize = 16,
  742. .ops = &s3c24xx_serial_ops,
  743. .flags = UPF_BOOT_AUTOCONF,
  744. .line = 0,
  745. }
  746. },
  747. [1] = {
  748. .port = {
  749. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
  750. .iotype = UPIO_MEM,
  751. .uartclk = 0,
  752. .fifosize = 16,
  753. .ops = &s3c24xx_serial_ops,
  754. .flags = UPF_BOOT_AUTOCONF,
  755. .line = 1,
  756. }
  757. },
  758. #if CONFIG_SERIAL_SAMSUNG_UARTS > 2
  759. [2] = {
  760. .port = {
  761. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
  762. .iotype = UPIO_MEM,
  763. .uartclk = 0,
  764. .fifosize = 16,
  765. .ops = &s3c24xx_serial_ops,
  766. .flags = UPF_BOOT_AUTOCONF,
  767. .line = 2,
  768. }
  769. },
  770. #endif
  771. #if CONFIG_SERIAL_SAMSUNG_UARTS > 3
  772. [3] = {
  773. .port = {
  774. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[3].port.lock),
  775. .iotype = UPIO_MEM,
  776. .uartclk = 0,
  777. .fifosize = 16,
  778. .ops = &s3c24xx_serial_ops,
  779. .flags = UPF_BOOT_AUTOCONF,
  780. .line = 3,
  781. }
  782. }
  783. #endif
  784. };
  785. /* s3c24xx_serial_resetport
  786. *
  787. * reset the fifos and other the settings.
  788. */
  789. static void s3c24xx_serial_resetport(struct uart_port *port,
  790. struct s3c2410_uartcfg *cfg)
  791. {
  792. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  793. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  794. unsigned int ucon_mask;
  795. ucon_mask = info->clksel_mask;
  796. if (info->type == PORT_S3C2440)
  797. ucon_mask |= S3C2440_UCON0_DIVMASK;
  798. ucon &= ucon_mask;
  799. wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
  800. wr_regl(port, S3C2410_ULCON, cfg->ulcon);
  801. /* reset both fifos */
  802. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  803. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  804. /* some delay is required after fifo reset */
  805. udelay(1);
  806. }
  807. #ifdef CONFIG_CPU_FREQ
  808. static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
  809. unsigned long val, void *data)
  810. {
  811. struct s3c24xx_uart_port *port;
  812. struct uart_port *uport;
  813. port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
  814. uport = &port->port;
  815. /* check to see if port is enabled */
  816. if (port->pm_level != 0)
  817. return 0;
  818. /* try and work out if the baudrate is changing, we can detect
  819. * a change in rate, but we do not have support for detecting
  820. * a disturbance in the clock-rate over the change.
  821. */
  822. if (IS_ERR(port->baudclk))
  823. goto exit;
  824. if (port->baudclk_rate == clk_get_rate(port->baudclk))
  825. goto exit;
  826. if (val == CPUFREQ_PRECHANGE) {
  827. /* we should really shut the port down whilst the
  828. * frequency change is in progress. */
  829. } else if (val == CPUFREQ_POSTCHANGE) {
  830. struct ktermios *termios;
  831. struct tty_struct *tty;
  832. if (uport->state == NULL)
  833. goto exit;
  834. tty = uport->state->port.tty;
  835. if (tty == NULL)
  836. goto exit;
  837. termios = tty->termios;
  838. if (termios == NULL) {
  839. printk(KERN_WARNING "%s: no termios?\n", __func__);
  840. goto exit;
  841. }
  842. s3c24xx_serial_set_termios(uport, termios, NULL);
  843. }
  844. exit:
  845. return 0;
  846. }
  847. static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
  848. {
  849. port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
  850. return cpufreq_register_notifier(&port->freq_transition,
  851. CPUFREQ_TRANSITION_NOTIFIER);
  852. }
  853. static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
  854. {
  855. cpufreq_unregister_notifier(&port->freq_transition,
  856. CPUFREQ_TRANSITION_NOTIFIER);
  857. }
  858. #else
  859. static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
  860. {
  861. return 0;
  862. }
  863. static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
  864. {
  865. }
  866. #endif
  867. /* s3c24xx_serial_init_port
  868. *
  869. * initialise a single serial port from the platform device given
  870. */
  871. static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
  872. struct platform_device *platdev)
  873. {
  874. struct uart_port *port = &ourport->port;
  875. struct s3c2410_uartcfg *cfg = ourport->cfg;
  876. struct resource *res;
  877. int ret;
  878. dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
  879. if (platdev == NULL)
  880. return -ENODEV;
  881. if (port->mapbase != 0)
  882. return 0;
  883. /* setup info for port */
  884. port->dev = &platdev->dev;
  885. /* Startup sequence is different for s3c64xx and higher SoC's */
  886. if (s3c24xx_serial_has_interrupt_mask(port))
  887. s3c24xx_serial_ops.startup = s3c64xx_serial_startup;
  888. port->uartclk = 1;
  889. if (cfg->uart_flags & UPF_CONS_FLOW) {
  890. dbg("s3c24xx_serial_init_port: enabling flow control\n");
  891. port->flags |= UPF_CONS_FLOW;
  892. }
  893. /* sort our the physical and virtual addresses for each UART */
  894. res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
  895. if (res == NULL) {
  896. printk(KERN_ERR "failed to find memory resource for uart\n");
  897. return -EINVAL;
  898. }
  899. dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
  900. port->mapbase = res->start;
  901. port->membase = S3C_VA_UART + (res->start & 0xfffff);
  902. ret = platform_get_irq(platdev, 0);
  903. if (ret < 0)
  904. port->irq = 0;
  905. else {
  906. port->irq = ret;
  907. ourport->rx_irq = ret;
  908. ourport->tx_irq = ret + 1;
  909. }
  910. ret = platform_get_irq(platdev, 1);
  911. if (ret > 0)
  912. ourport->tx_irq = ret;
  913. ourport->clk = clk_get(&platdev->dev, "uart");
  914. /* Keep all interrupts masked and cleared */
  915. if (s3c24xx_serial_has_interrupt_mask(port)) {
  916. wr_regl(port, S3C64XX_UINTM, 0xf);
  917. wr_regl(port, S3C64XX_UINTP, 0xf);
  918. wr_regl(port, S3C64XX_UINTSP, 0xf);
  919. }
  920. dbg("port: map=%08x, mem=%08x, irq=%d (%d,%d), clock=%ld\n",
  921. port->mapbase, port->membase, port->irq,
  922. ourport->rx_irq, ourport->tx_irq, port->uartclk);
  923. /* reset the fifos (and setup the uart) */
  924. s3c24xx_serial_resetport(port, cfg);
  925. return 0;
  926. }
  927. static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
  928. struct device_attribute *attr,
  929. char *buf)
  930. {
  931. struct uart_port *port = s3c24xx_dev_to_port(dev);
  932. struct s3c24xx_uart_port *ourport = to_ourport(port);
  933. return snprintf(buf, PAGE_SIZE, "* %s\n", ourport->baudclk->name);
  934. }
  935. static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL);
  936. /* Device driver serial port probe */
  937. static const struct of_device_id s3c24xx_uart_dt_match[];
  938. static int probe_index;
  939. static inline struct s3c24xx_serial_drv_data *s3c24xx_get_driver_data(
  940. struct platform_device *pdev)
  941. {
  942. #ifdef CONFIG_OF
  943. if (pdev->dev.of_node) {
  944. const struct of_device_id *match;
  945. match = of_match_node(s3c24xx_uart_dt_match, pdev->dev.of_node);
  946. return (struct s3c24xx_serial_drv_data *)match->data;
  947. }
  948. #endif
  949. return (struct s3c24xx_serial_drv_data *)
  950. platform_get_device_id(pdev)->driver_data;
  951. }
  952. static int s3c24xx_serial_probe(struct platform_device *pdev)
  953. {
  954. struct s3c24xx_uart_port *ourport;
  955. int ret;
  956. dbg("s3c24xx_serial_probe(%p) %d\n", pdev, probe_index);
  957. ourport = &s3c24xx_serial_ports[probe_index];
  958. ourport->drv_data = s3c24xx_get_driver_data(pdev);
  959. if (!ourport->drv_data) {
  960. dev_err(&pdev->dev, "could not find driver data\n");
  961. return -ENODEV;
  962. }
  963. ourport->info = ourport->drv_data->info;
  964. ourport->cfg = (pdev->dev.platform_data) ?
  965. (struct s3c2410_uartcfg *)pdev->dev.platform_data :
  966. ourport->drv_data->def_cfg;
  967. ourport->port.fifosize = (ourport->info->fifosize) ?
  968. ourport->info->fifosize :
  969. ourport->drv_data->fifosize[probe_index];
  970. probe_index++;
  971. dbg("%s: initialising port %p...\n", __func__, ourport);
  972. ret = s3c24xx_serial_init_port(ourport, pdev);
  973. if (ret < 0)
  974. goto probe_err;
  975. dbg("%s: adding port\n", __func__);
  976. uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
  977. platform_set_drvdata(pdev, &ourport->port);
  978. ret = device_create_file(&pdev->dev, &dev_attr_clock_source);
  979. if (ret < 0)
  980. dev_err(&pdev->dev, "failed to add clock source attr.\n");
  981. ret = s3c24xx_serial_cpufreq_register(ourport);
  982. if (ret < 0)
  983. dev_err(&pdev->dev, "failed to add cpufreq notifier\n");
  984. return 0;
  985. probe_err:
  986. return ret;
  987. }
  988. static int __devexit s3c24xx_serial_remove(struct platform_device *dev)
  989. {
  990. struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
  991. if (port) {
  992. s3c24xx_serial_cpufreq_deregister(to_ourport(port));
  993. device_remove_file(&dev->dev, &dev_attr_clock_source);
  994. uart_remove_one_port(&s3c24xx_uart_drv, port);
  995. }
  996. return 0;
  997. }
  998. /* UART power management code */
  999. #ifdef CONFIG_PM_SLEEP
  1000. static int s3c24xx_serial_suspend(struct device *dev)
  1001. {
  1002. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1003. if (port)
  1004. uart_suspend_port(&s3c24xx_uart_drv, port);
  1005. return 0;
  1006. }
  1007. static int s3c24xx_serial_resume(struct device *dev)
  1008. {
  1009. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1010. struct s3c24xx_uart_port *ourport = to_ourport(port);
  1011. if (port) {
  1012. clk_enable(ourport->clk);
  1013. s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
  1014. clk_disable(ourport->clk);
  1015. uart_resume_port(&s3c24xx_uart_drv, port);
  1016. }
  1017. return 0;
  1018. }
  1019. static const struct dev_pm_ops s3c24xx_serial_pm_ops = {
  1020. .suspend = s3c24xx_serial_suspend,
  1021. .resume = s3c24xx_serial_resume,
  1022. };
  1023. #define SERIAL_SAMSUNG_PM_OPS (&s3c24xx_serial_pm_ops)
  1024. #else /* !CONFIG_PM_SLEEP */
  1025. #define SERIAL_SAMSUNG_PM_OPS NULL
  1026. #endif /* CONFIG_PM_SLEEP */
  1027. /* Console code */
  1028. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  1029. static struct uart_port *cons_uart;
  1030. static int
  1031. s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
  1032. {
  1033. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1034. unsigned long ufstat, utrstat;
  1035. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  1036. /* fifo mode - check amount of data in fifo registers... */
  1037. ufstat = rd_regl(port, S3C2410_UFSTAT);
  1038. return (ufstat & info->tx_fifofull) ? 0 : 1;
  1039. }
  1040. /* in non-fifo mode, we go and use the tx buffer empty */
  1041. utrstat = rd_regl(port, S3C2410_UTRSTAT);
  1042. return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
  1043. }
  1044. static void
  1045. s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
  1046. {
  1047. unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
  1048. while (!s3c24xx_serial_console_txrdy(port, ufcon))
  1049. barrier();
  1050. wr_regb(cons_uart, S3C2410_UTXH, ch);
  1051. }
  1052. static void
  1053. s3c24xx_serial_console_write(struct console *co, const char *s,
  1054. unsigned int count)
  1055. {
  1056. uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
  1057. }
  1058. static void __init
  1059. s3c24xx_serial_get_options(struct uart_port *port, int *baud,
  1060. int *parity, int *bits)
  1061. {
  1062. struct clk *clk;
  1063. unsigned int ulcon;
  1064. unsigned int ucon;
  1065. unsigned int ubrdiv;
  1066. unsigned long rate;
  1067. unsigned int clk_sel;
  1068. char clk_name[MAX_CLK_NAME_LENGTH];
  1069. ulcon = rd_regl(port, S3C2410_ULCON);
  1070. ucon = rd_regl(port, S3C2410_UCON);
  1071. ubrdiv = rd_regl(port, S3C2410_UBRDIV);
  1072. dbg("s3c24xx_serial_get_options: port=%p\n"
  1073. "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
  1074. port, ulcon, ucon, ubrdiv);
  1075. if ((ucon & 0xf) != 0) {
  1076. /* consider the serial port configured if the tx/rx mode set */
  1077. switch (ulcon & S3C2410_LCON_CSMASK) {
  1078. case S3C2410_LCON_CS5:
  1079. *bits = 5;
  1080. break;
  1081. case S3C2410_LCON_CS6:
  1082. *bits = 6;
  1083. break;
  1084. case S3C2410_LCON_CS7:
  1085. *bits = 7;
  1086. break;
  1087. default:
  1088. case S3C2410_LCON_CS8:
  1089. *bits = 8;
  1090. break;
  1091. }
  1092. switch (ulcon & S3C2410_LCON_PMASK) {
  1093. case S3C2410_LCON_PEVEN:
  1094. *parity = 'e';
  1095. break;
  1096. case S3C2410_LCON_PODD:
  1097. *parity = 'o';
  1098. break;
  1099. case S3C2410_LCON_PNONE:
  1100. default:
  1101. *parity = 'n';
  1102. }
  1103. /* now calculate the baud rate */
  1104. clk_sel = s3c24xx_serial_getsource(port);
  1105. sprintf(clk_name, "clk_uart_baud%d", clk_sel);
  1106. clk = clk_get(port->dev, clk_name);
  1107. if (!IS_ERR(clk) && clk != NULL)
  1108. rate = clk_get_rate(clk);
  1109. else
  1110. rate = 1;
  1111. *baud = rate / (16 * (ubrdiv + 1));
  1112. dbg("calculated baud %d\n", *baud);
  1113. }
  1114. }
  1115. static int __init
  1116. s3c24xx_serial_console_setup(struct console *co, char *options)
  1117. {
  1118. struct uart_port *port;
  1119. int baud = 9600;
  1120. int bits = 8;
  1121. int parity = 'n';
  1122. int flow = 'n';
  1123. dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
  1124. co, co->index, options);
  1125. /* is this a valid port */
  1126. if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
  1127. co->index = 0;
  1128. port = &s3c24xx_serial_ports[co->index].port;
  1129. /* is the port configured? */
  1130. if (port->mapbase == 0x0)
  1131. return -ENODEV;
  1132. cons_uart = port;
  1133. dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
  1134. /*
  1135. * Check whether an invalid uart number has been specified, and
  1136. * if so, search for the first available port that does have
  1137. * console support.
  1138. */
  1139. if (options)
  1140. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1141. else
  1142. s3c24xx_serial_get_options(port, &baud, &parity, &bits);
  1143. dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
  1144. return uart_set_options(port, co, baud, parity, bits, flow);
  1145. }
  1146. static struct console s3c24xx_serial_console = {
  1147. .name = S3C24XX_SERIAL_NAME,
  1148. .device = uart_console_device,
  1149. .flags = CON_PRINTBUFFER,
  1150. .index = -1,
  1151. .write = s3c24xx_serial_console_write,
  1152. .setup = s3c24xx_serial_console_setup,
  1153. .data = &s3c24xx_uart_drv,
  1154. };
  1155. #endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
  1156. #ifdef CONFIG_CPU_S3C2410
  1157. static struct s3c24xx_serial_drv_data s3c2410_serial_drv_data = {
  1158. .info = &(struct s3c24xx_uart_info) {
  1159. .name = "Samsung S3C2410 UART",
  1160. .type = PORT_S3C2410,
  1161. .fifosize = 16,
  1162. .rx_fifomask = S3C2410_UFSTAT_RXMASK,
  1163. .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
  1164. .rx_fifofull = S3C2410_UFSTAT_RXFULL,
  1165. .tx_fifofull = S3C2410_UFSTAT_TXFULL,
  1166. .tx_fifomask = S3C2410_UFSTAT_TXMASK,
  1167. .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
  1168. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  1169. .num_clks = 2,
  1170. .clksel_mask = S3C2410_UCON_CLKMASK,
  1171. .clksel_shift = S3C2410_UCON_CLKSHIFT,
  1172. },
  1173. .def_cfg = &(struct s3c2410_uartcfg) {
  1174. .ucon = S3C2410_UCON_DEFAULT,
  1175. .ufcon = S3C2410_UFCON_DEFAULT,
  1176. },
  1177. };
  1178. #define S3C2410_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2410_serial_drv_data)
  1179. #else
  1180. #define S3C2410_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1181. #endif
  1182. #ifdef CONFIG_CPU_S3C2412
  1183. static struct s3c24xx_serial_drv_data s3c2412_serial_drv_data = {
  1184. .info = &(struct s3c24xx_uart_info) {
  1185. .name = "Samsung S3C2412 UART",
  1186. .type = PORT_S3C2412,
  1187. .fifosize = 64,
  1188. .has_divslot = 1,
  1189. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1190. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1191. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1192. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1193. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1194. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1195. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1196. .num_clks = 4,
  1197. .clksel_mask = S3C2412_UCON_CLKMASK,
  1198. .clksel_shift = S3C2412_UCON_CLKSHIFT,
  1199. },
  1200. .def_cfg = &(struct s3c2410_uartcfg) {
  1201. .ucon = S3C2410_UCON_DEFAULT,
  1202. .ufcon = S3C2410_UFCON_DEFAULT,
  1203. },
  1204. };
  1205. #define S3C2412_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2412_serial_drv_data)
  1206. #else
  1207. #define S3C2412_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1208. #endif
  1209. #if defined(CONFIG_CPU_S3C2440) || defined(CONFIG_CPU_S3C2416) || \
  1210. defined(CONFIG_CPU_S3C2443) || defined(CONFIG_CPU_S3C2442)
  1211. static struct s3c24xx_serial_drv_data s3c2440_serial_drv_data = {
  1212. .info = &(struct s3c24xx_uart_info) {
  1213. .name = "Samsung S3C2440 UART",
  1214. .type = PORT_S3C2440,
  1215. .fifosize = 64,
  1216. .has_divslot = 1,
  1217. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1218. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1219. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1220. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1221. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1222. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1223. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1224. .num_clks = 4,
  1225. .clksel_mask = S3C2412_UCON_CLKMASK,
  1226. .clksel_shift = S3C2412_UCON_CLKSHIFT,
  1227. },
  1228. .def_cfg = &(struct s3c2410_uartcfg) {
  1229. .ucon = S3C2410_UCON_DEFAULT,
  1230. .ufcon = S3C2410_UFCON_DEFAULT,
  1231. },
  1232. };
  1233. #define S3C2440_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2440_serial_drv_data)
  1234. #else
  1235. #define S3C2440_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1236. #endif
  1237. #if defined(CONFIG_CPU_S3C6400) || defined(CONFIG_CPU_S3C6410) || \
  1238. defined(CONFIG_CPU_S5P6440) || defined(CONFIG_CPU_S5P6450) || \
  1239. defined(CONFIG_CPU_S5PC100)
  1240. static struct s3c24xx_serial_drv_data s3c6400_serial_drv_data = {
  1241. .info = &(struct s3c24xx_uart_info) {
  1242. .name = "Samsung S3C6400 UART",
  1243. .type = PORT_S3C6400,
  1244. .fifosize = 64,
  1245. .has_divslot = 1,
  1246. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1247. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1248. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1249. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1250. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1251. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1252. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1253. .num_clks = 4,
  1254. .clksel_mask = S3C6400_UCON_CLKMASK,
  1255. .clksel_shift = S3C6400_UCON_CLKSHIFT,
  1256. },
  1257. .def_cfg = &(struct s3c2410_uartcfg) {
  1258. .ucon = S3C2410_UCON_DEFAULT,
  1259. .ufcon = S3C2410_UFCON_DEFAULT,
  1260. },
  1261. };
  1262. #define S3C6400_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c6400_serial_drv_data)
  1263. #else
  1264. #define S3C6400_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1265. #endif
  1266. #ifdef CONFIG_CPU_S5PV210
  1267. static struct s3c24xx_serial_drv_data s5pv210_serial_drv_data = {
  1268. .info = &(struct s3c24xx_uart_info) {
  1269. .name = "Samsung S5PV210 UART",
  1270. .type = PORT_S3C6400,
  1271. .has_divslot = 1,
  1272. .rx_fifomask = S5PV210_UFSTAT_RXMASK,
  1273. .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
  1274. .rx_fifofull = S5PV210_UFSTAT_RXFULL,
  1275. .tx_fifofull = S5PV210_UFSTAT_TXFULL,
  1276. .tx_fifomask = S5PV210_UFSTAT_TXMASK,
  1277. .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
  1278. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  1279. .num_clks = 2,
  1280. .clksel_mask = S5PV210_UCON_CLKMASK,
  1281. .clksel_shift = S5PV210_UCON_CLKSHIFT,
  1282. },
  1283. .def_cfg = &(struct s3c2410_uartcfg) {
  1284. .ucon = S5PV210_UCON_DEFAULT,
  1285. .ufcon = S5PV210_UFCON_DEFAULT,
  1286. },
  1287. .fifosize = { 256, 64, 16, 16 },
  1288. };
  1289. #define S5PV210_SERIAL_DRV_DATA ((kernel_ulong_t)&s5pv210_serial_drv_data)
  1290. #else
  1291. #define S5PV210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1292. #endif
  1293. #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212) || \
  1294. defined(CONFIG_SOC_EXYNOS4412) || defined(CONFIG_SOC_EXYNOS5250)
  1295. static struct s3c24xx_serial_drv_data exynos4210_serial_drv_data = {
  1296. .info = &(struct s3c24xx_uart_info) {
  1297. .name = "Samsung Exynos4 UART",
  1298. .type = PORT_S3C6400,
  1299. .has_divslot = 1,
  1300. .rx_fifomask = S5PV210_UFSTAT_RXMASK,
  1301. .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
  1302. .rx_fifofull = S5PV210_UFSTAT_RXFULL,
  1303. .tx_fifofull = S5PV210_UFSTAT_TXFULL,
  1304. .tx_fifomask = S5PV210_UFSTAT_TXMASK,
  1305. .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
  1306. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  1307. .num_clks = 1,
  1308. .clksel_mask = 0,
  1309. .clksel_shift = 0,
  1310. },
  1311. .def_cfg = &(struct s3c2410_uartcfg) {
  1312. .ucon = S5PV210_UCON_DEFAULT,
  1313. .ufcon = S5PV210_UFCON_DEFAULT,
  1314. .has_fracval = 1,
  1315. },
  1316. .fifosize = { 256, 64, 16, 16 },
  1317. };
  1318. #define EXYNOS4210_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos4210_serial_drv_data)
  1319. #else
  1320. #define EXYNOS4210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1321. #endif
  1322. static struct platform_device_id s3c24xx_serial_driver_ids[] = {
  1323. {
  1324. .name = "s3c2410-uart",
  1325. .driver_data = S3C2410_SERIAL_DRV_DATA,
  1326. }, {
  1327. .name = "s3c2412-uart",
  1328. .driver_data = S3C2412_SERIAL_DRV_DATA,
  1329. }, {
  1330. .name = "s3c2440-uart",
  1331. .driver_data = S3C2440_SERIAL_DRV_DATA,
  1332. }, {
  1333. .name = "s3c6400-uart",
  1334. .driver_data = S3C6400_SERIAL_DRV_DATA,
  1335. }, {
  1336. .name = "s5pv210-uart",
  1337. .driver_data = S5PV210_SERIAL_DRV_DATA,
  1338. }, {
  1339. .name = "exynos4210-uart",
  1340. .driver_data = EXYNOS4210_SERIAL_DRV_DATA,
  1341. },
  1342. { },
  1343. };
  1344. MODULE_DEVICE_TABLE(platform, s3c24xx_serial_driver_ids);
  1345. #ifdef CONFIG_OF
  1346. static const struct of_device_id s3c24xx_uart_dt_match[] = {
  1347. { .compatible = "samsung,exynos4210-uart",
  1348. .data = (void *)EXYNOS4210_SERIAL_DRV_DATA },
  1349. {},
  1350. };
  1351. MODULE_DEVICE_TABLE(of, s3c24xx_uart_dt_match);
  1352. #else
  1353. #define s3c24xx_uart_dt_match NULL
  1354. #endif
  1355. static struct platform_driver samsung_serial_driver = {
  1356. .probe = s3c24xx_serial_probe,
  1357. .remove = __devexit_p(s3c24xx_serial_remove),
  1358. .id_table = s3c24xx_serial_driver_ids,
  1359. .driver = {
  1360. .name = "samsung-uart",
  1361. .owner = THIS_MODULE,
  1362. .pm = SERIAL_SAMSUNG_PM_OPS,
  1363. .of_match_table = s3c24xx_uart_dt_match,
  1364. },
  1365. };
  1366. /* module initialisation code */
  1367. static int __init s3c24xx_serial_modinit(void)
  1368. {
  1369. int ret;
  1370. ret = uart_register_driver(&s3c24xx_uart_drv);
  1371. if (ret < 0) {
  1372. printk(KERN_ERR "failed to register UART driver\n");
  1373. return -1;
  1374. }
  1375. return platform_driver_register(&samsung_serial_driver);
  1376. }
  1377. static void __exit s3c24xx_serial_modexit(void)
  1378. {
  1379. uart_unregister_driver(&s3c24xx_uart_drv);
  1380. }
  1381. module_init(s3c24xx_serial_modinit);
  1382. module_exit(s3c24xx_serial_modexit);
  1383. MODULE_ALIAS("platform:samsung-uart");
  1384. MODULE_DESCRIPTION("Samsung SoC Serial port driver");
  1385. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  1386. MODULE_LICENSE("GPL v2");