samsung.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  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. else
  343. free_irq(port->irq, ourport);
  344. tx_enabled(port) = 0;
  345. ourport->tx_claimed = 0;
  346. }
  347. if (ourport->rx_claimed) {
  348. if (!s3c24xx_serial_has_interrupt_mask(port))
  349. free_irq(ourport->rx_irq, ourport);
  350. /* else already freed above as the s3c64xx_serial_startup()
  351. * will have set both tx_claimed and rx_claimed */
  352. ourport->rx_claimed = 0;
  353. rx_enabled(port) = 0;
  354. }
  355. /* Clear pending interrupts and mask all interrupts */
  356. if (s3c24xx_serial_has_interrupt_mask(port)) {
  357. wr_regl(port, S3C64XX_UINTP, 0xf);
  358. wr_regl(port, S3C64XX_UINTM, 0xf);
  359. }
  360. }
  361. static int s3c24xx_serial_startup(struct uart_port *port)
  362. {
  363. struct s3c24xx_uart_port *ourport = to_ourport(port);
  364. int ret;
  365. dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
  366. port->mapbase, port->membase);
  367. rx_enabled(port) = 1;
  368. ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
  369. s3c24xx_serial_portname(port), ourport);
  370. if (ret != 0) {
  371. printk(KERN_ERR "cannot get irq %d\n", ourport->rx_irq);
  372. return ret;
  373. }
  374. ourport->rx_claimed = 1;
  375. dbg("requesting tx irq...\n");
  376. tx_enabled(port) = 1;
  377. ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
  378. s3c24xx_serial_portname(port), ourport);
  379. if (ret) {
  380. printk(KERN_ERR "cannot get irq %d\n", ourport->tx_irq);
  381. goto err;
  382. }
  383. ourport->tx_claimed = 1;
  384. dbg("s3c24xx_serial_startup ok\n");
  385. /* the port reset code should have done the correct
  386. * register setup for the port controls */
  387. return ret;
  388. err:
  389. s3c24xx_serial_shutdown(port);
  390. return ret;
  391. }
  392. static int s3c64xx_serial_startup(struct uart_port *port)
  393. {
  394. struct s3c24xx_uart_port *ourport = to_ourport(port);
  395. int ret;
  396. dbg("s3c64xx_serial_startup: port=%p (%08lx,%p)\n",
  397. port->mapbase, port->membase);
  398. ret = request_irq(port->irq, s3c64xx_serial_handle_irq, IRQF_SHARED,
  399. s3c24xx_serial_portname(port), ourport);
  400. if (ret) {
  401. printk(KERN_ERR "cannot get irq %d\n", port->irq);
  402. return ret;
  403. }
  404. /* For compatibility with s3c24xx Soc's */
  405. rx_enabled(port) = 1;
  406. ourport->rx_claimed = 1;
  407. tx_enabled(port) = 0;
  408. ourport->tx_claimed = 1;
  409. /* Enable Rx Interrupt */
  410. __clear_bit(S3C64XX_UINTM_RXD, portaddrl(port, S3C64XX_UINTM));
  411. dbg("s3c64xx_serial_startup ok\n");
  412. return ret;
  413. }
  414. /* power power management control */
  415. static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
  416. unsigned int old)
  417. {
  418. struct s3c24xx_uart_port *ourport = to_ourport(port);
  419. int timeout = 10000;
  420. ourport->pm_level = level;
  421. switch (level) {
  422. case 3:
  423. while (--timeout && !s3c24xx_serial_txempty_nofifo(port))
  424. udelay(100);
  425. if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
  426. clk_disable(ourport->baudclk);
  427. clk_disable(ourport->clk);
  428. break;
  429. case 0:
  430. clk_enable(ourport->clk);
  431. if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
  432. clk_enable(ourport->baudclk);
  433. break;
  434. default:
  435. printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
  436. }
  437. }
  438. /* baud rate calculation
  439. *
  440. * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
  441. * of different sources, including the peripheral clock ("pclk") and an
  442. * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
  443. * with a programmable extra divisor.
  444. *
  445. * The following code goes through the clock sources, and calculates the
  446. * baud clocks (and the resultant actual baud rates) and then tries to
  447. * pick the closest one and select that.
  448. *
  449. */
  450. #define MAX_CLK_NAME_LENGTH 15
  451. static inline int s3c24xx_serial_getsource(struct uart_port *port)
  452. {
  453. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  454. unsigned int ucon;
  455. if (info->num_clks == 1)
  456. return 0;
  457. ucon = rd_regl(port, S3C2410_UCON);
  458. ucon &= info->clksel_mask;
  459. return ucon >> info->clksel_shift;
  460. }
  461. static void s3c24xx_serial_setsource(struct uart_port *port,
  462. unsigned int clk_sel)
  463. {
  464. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  465. unsigned int ucon;
  466. if (info->num_clks == 1)
  467. return;
  468. ucon = rd_regl(port, S3C2410_UCON);
  469. if ((ucon & info->clksel_mask) >> info->clksel_shift == clk_sel)
  470. return;
  471. ucon &= ~info->clksel_mask;
  472. ucon |= clk_sel << info->clksel_shift;
  473. wr_regl(port, S3C2410_UCON, ucon);
  474. }
  475. static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
  476. unsigned int req_baud, struct clk **best_clk,
  477. unsigned int *clk_num)
  478. {
  479. struct s3c24xx_uart_info *info = ourport->info;
  480. struct clk *clk;
  481. unsigned long rate;
  482. unsigned int cnt, baud, quot, clk_sel, best_quot = 0;
  483. char clkname[MAX_CLK_NAME_LENGTH];
  484. int calc_deviation, deviation = (1 << 30) - 1;
  485. *best_clk = NULL;
  486. clk_sel = (ourport->cfg->clk_sel) ? ourport->cfg->clk_sel :
  487. ourport->info->def_clk_sel;
  488. for (cnt = 0; cnt < info->num_clks; cnt++) {
  489. if (!(clk_sel & (1 << cnt)))
  490. continue;
  491. sprintf(clkname, "clk_uart_baud%d", cnt);
  492. clk = clk_get(ourport->port.dev, clkname);
  493. if (IS_ERR_OR_NULL(clk))
  494. continue;
  495. rate = clk_get_rate(clk);
  496. if (!rate)
  497. continue;
  498. if (ourport->info->has_divslot) {
  499. unsigned long div = rate / req_baud;
  500. /* The UDIVSLOT register on the newer UARTs allows us to
  501. * get a divisor adjustment of 1/16th on the baud clock.
  502. *
  503. * We don't keep the UDIVSLOT value (the 16ths we
  504. * calculated by not multiplying the baud by 16) as it
  505. * is easy enough to recalculate.
  506. */
  507. quot = div / 16;
  508. baud = rate / div;
  509. } else {
  510. quot = (rate + (8 * req_baud)) / (16 * req_baud);
  511. baud = rate / (quot * 16);
  512. }
  513. quot--;
  514. calc_deviation = req_baud - baud;
  515. if (calc_deviation < 0)
  516. calc_deviation = -calc_deviation;
  517. if (calc_deviation < deviation) {
  518. *best_clk = clk;
  519. best_quot = quot;
  520. *clk_num = cnt;
  521. deviation = calc_deviation;
  522. }
  523. }
  524. return best_quot;
  525. }
  526. /* udivslot_table[]
  527. *
  528. * This table takes the fractional value of the baud divisor and gives
  529. * the recommended setting for the UDIVSLOT register.
  530. */
  531. static u16 udivslot_table[16] = {
  532. [0] = 0x0000,
  533. [1] = 0x0080,
  534. [2] = 0x0808,
  535. [3] = 0x0888,
  536. [4] = 0x2222,
  537. [5] = 0x4924,
  538. [6] = 0x4A52,
  539. [7] = 0x54AA,
  540. [8] = 0x5555,
  541. [9] = 0xD555,
  542. [10] = 0xD5D5,
  543. [11] = 0xDDD5,
  544. [12] = 0xDDDD,
  545. [13] = 0xDFDD,
  546. [14] = 0xDFDF,
  547. [15] = 0xFFDF,
  548. };
  549. static void s3c24xx_serial_set_termios(struct uart_port *port,
  550. struct ktermios *termios,
  551. struct ktermios *old)
  552. {
  553. struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
  554. struct s3c24xx_uart_port *ourport = to_ourport(port);
  555. struct clk *clk = NULL;
  556. unsigned long flags;
  557. unsigned int baud, quot, clk_sel = 0;
  558. unsigned int ulcon;
  559. unsigned int umcon;
  560. unsigned int udivslot = 0;
  561. /*
  562. * We don't support modem control lines.
  563. */
  564. termios->c_cflag &= ~(HUPCL | CMSPAR);
  565. termios->c_cflag |= CLOCAL;
  566. /*
  567. * Ask the core to calculate the divisor for us.
  568. */
  569. baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
  570. quot = s3c24xx_serial_getclk(ourport, baud, &clk, &clk_sel);
  571. if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
  572. quot = port->custom_divisor;
  573. if (!clk)
  574. return;
  575. /* check to see if we need to change clock source */
  576. if (ourport->baudclk != clk) {
  577. s3c24xx_serial_setsource(port, clk_sel);
  578. if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
  579. clk_disable(ourport->baudclk);
  580. ourport->baudclk = NULL;
  581. }
  582. clk_enable(clk);
  583. ourport->baudclk = clk;
  584. ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
  585. }
  586. if (ourport->info->has_divslot) {
  587. unsigned int div = ourport->baudclk_rate / baud;
  588. if (cfg->has_fracval) {
  589. udivslot = (div & 15);
  590. dbg("fracval = %04x\n", udivslot);
  591. } else {
  592. udivslot = udivslot_table[div & 15];
  593. dbg("udivslot = %04x (div %d)\n", udivslot, div & 15);
  594. }
  595. }
  596. switch (termios->c_cflag & CSIZE) {
  597. case CS5:
  598. dbg("config: 5bits/char\n");
  599. ulcon = S3C2410_LCON_CS5;
  600. break;
  601. case CS6:
  602. dbg("config: 6bits/char\n");
  603. ulcon = S3C2410_LCON_CS6;
  604. break;
  605. case CS7:
  606. dbg("config: 7bits/char\n");
  607. ulcon = S3C2410_LCON_CS7;
  608. break;
  609. case CS8:
  610. default:
  611. dbg("config: 8bits/char\n");
  612. ulcon = S3C2410_LCON_CS8;
  613. break;
  614. }
  615. /* preserve original lcon IR settings */
  616. ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
  617. if (termios->c_cflag & CSTOPB)
  618. ulcon |= S3C2410_LCON_STOPB;
  619. umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
  620. if (termios->c_cflag & PARENB) {
  621. if (termios->c_cflag & PARODD)
  622. ulcon |= S3C2410_LCON_PODD;
  623. else
  624. ulcon |= S3C2410_LCON_PEVEN;
  625. } else {
  626. ulcon |= S3C2410_LCON_PNONE;
  627. }
  628. spin_lock_irqsave(&port->lock, flags);
  629. dbg("setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
  630. ulcon, quot, udivslot);
  631. wr_regl(port, S3C2410_ULCON, ulcon);
  632. wr_regl(port, S3C2410_UBRDIV, quot);
  633. wr_regl(port, S3C2410_UMCON, umcon);
  634. if (ourport->info->has_divslot)
  635. wr_regl(port, S3C2443_DIVSLOT, udivslot);
  636. dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
  637. rd_regl(port, S3C2410_ULCON),
  638. rd_regl(port, S3C2410_UCON),
  639. rd_regl(port, S3C2410_UFCON));
  640. /*
  641. * Update the per-port timeout.
  642. */
  643. uart_update_timeout(port, termios->c_cflag, baud);
  644. /*
  645. * Which character status flags are we interested in?
  646. */
  647. port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
  648. if (termios->c_iflag & INPCK)
  649. port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
  650. /*
  651. * Which character status flags should we ignore?
  652. */
  653. port->ignore_status_mask = 0;
  654. if (termios->c_iflag & IGNPAR)
  655. port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
  656. if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
  657. port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
  658. /*
  659. * Ignore all characters if CREAD is not set.
  660. */
  661. if ((termios->c_cflag & CREAD) == 0)
  662. port->ignore_status_mask |= RXSTAT_DUMMY_READ;
  663. spin_unlock_irqrestore(&port->lock, flags);
  664. }
  665. static const char *s3c24xx_serial_type(struct uart_port *port)
  666. {
  667. switch (port->type) {
  668. case PORT_S3C2410:
  669. return "S3C2410";
  670. case PORT_S3C2440:
  671. return "S3C2440";
  672. case PORT_S3C2412:
  673. return "S3C2412";
  674. case PORT_S3C6400:
  675. return "S3C6400/10";
  676. default:
  677. return NULL;
  678. }
  679. }
  680. #define MAP_SIZE (0x100)
  681. static void s3c24xx_serial_release_port(struct uart_port *port)
  682. {
  683. release_mem_region(port->mapbase, MAP_SIZE);
  684. }
  685. static int s3c24xx_serial_request_port(struct uart_port *port)
  686. {
  687. const char *name = s3c24xx_serial_portname(port);
  688. return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
  689. }
  690. static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
  691. {
  692. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  693. if (flags & UART_CONFIG_TYPE &&
  694. s3c24xx_serial_request_port(port) == 0)
  695. port->type = info->type;
  696. }
  697. /*
  698. * verify the new serial_struct (for TIOCSSERIAL).
  699. */
  700. static int
  701. s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  702. {
  703. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  704. if (ser->type != PORT_UNKNOWN && ser->type != info->type)
  705. return -EINVAL;
  706. return 0;
  707. }
  708. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  709. static struct console s3c24xx_serial_console;
  710. #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
  711. #else
  712. #define S3C24XX_SERIAL_CONSOLE NULL
  713. #endif
  714. static struct uart_ops s3c24xx_serial_ops = {
  715. .pm = s3c24xx_serial_pm,
  716. .tx_empty = s3c24xx_serial_tx_empty,
  717. .get_mctrl = s3c24xx_serial_get_mctrl,
  718. .set_mctrl = s3c24xx_serial_set_mctrl,
  719. .stop_tx = s3c24xx_serial_stop_tx,
  720. .start_tx = s3c24xx_serial_start_tx,
  721. .stop_rx = s3c24xx_serial_stop_rx,
  722. .enable_ms = s3c24xx_serial_enable_ms,
  723. .break_ctl = s3c24xx_serial_break_ctl,
  724. .startup = s3c24xx_serial_startup,
  725. .shutdown = s3c24xx_serial_shutdown,
  726. .set_termios = s3c24xx_serial_set_termios,
  727. .type = s3c24xx_serial_type,
  728. .release_port = s3c24xx_serial_release_port,
  729. .request_port = s3c24xx_serial_request_port,
  730. .config_port = s3c24xx_serial_config_port,
  731. .verify_port = s3c24xx_serial_verify_port,
  732. };
  733. static struct uart_driver s3c24xx_uart_drv = {
  734. .owner = THIS_MODULE,
  735. .driver_name = "s3c2410_serial",
  736. .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
  737. .cons = S3C24XX_SERIAL_CONSOLE,
  738. .dev_name = S3C24XX_SERIAL_NAME,
  739. .major = S3C24XX_SERIAL_MAJOR,
  740. .minor = S3C24XX_SERIAL_MINOR,
  741. };
  742. static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
  743. [0] = {
  744. .port = {
  745. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
  746. .iotype = UPIO_MEM,
  747. .uartclk = 0,
  748. .fifosize = 16,
  749. .ops = &s3c24xx_serial_ops,
  750. .flags = UPF_BOOT_AUTOCONF,
  751. .line = 0,
  752. }
  753. },
  754. [1] = {
  755. .port = {
  756. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
  757. .iotype = UPIO_MEM,
  758. .uartclk = 0,
  759. .fifosize = 16,
  760. .ops = &s3c24xx_serial_ops,
  761. .flags = UPF_BOOT_AUTOCONF,
  762. .line = 1,
  763. }
  764. },
  765. #if CONFIG_SERIAL_SAMSUNG_UARTS > 2
  766. [2] = {
  767. .port = {
  768. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
  769. .iotype = UPIO_MEM,
  770. .uartclk = 0,
  771. .fifosize = 16,
  772. .ops = &s3c24xx_serial_ops,
  773. .flags = UPF_BOOT_AUTOCONF,
  774. .line = 2,
  775. }
  776. },
  777. #endif
  778. #if CONFIG_SERIAL_SAMSUNG_UARTS > 3
  779. [3] = {
  780. .port = {
  781. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[3].port.lock),
  782. .iotype = UPIO_MEM,
  783. .uartclk = 0,
  784. .fifosize = 16,
  785. .ops = &s3c24xx_serial_ops,
  786. .flags = UPF_BOOT_AUTOCONF,
  787. .line = 3,
  788. }
  789. }
  790. #endif
  791. };
  792. /* s3c24xx_serial_resetport
  793. *
  794. * reset the fifos and other the settings.
  795. */
  796. static void s3c24xx_serial_resetport(struct uart_port *port,
  797. struct s3c2410_uartcfg *cfg)
  798. {
  799. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  800. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  801. unsigned int ucon_mask;
  802. ucon_mask = info->clksel_mask;
  803. if (info->type == PORT_S3C2440)
  804. ucon_mask |= S3C2440_UCON0_DIVMASK;
  805. ucon &= ucon_mask;
  806. wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
  807. wr_regl(port, S3C2410_ULCON, cfg->ulcon);
  808. /* reset both fifos */
  809. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  810. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  811. /* some delay is required after fifo reset */
  812. udelay(1);
  813. }
  814. #ifdef CONFIG_CPU_FREQ
  815. static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
  816. unsigned long val, void *data)
  817. {
  818. struct s3c24xx_uart_port *port;
  819. struct uart_port *uport;
  820. port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
  821. uport = &port->port;
  822. /* check to see if port is enabled */
  823. if (port->pm_level != 0)
  824. return 0;
  825. /* try and work out if the baudrate is changing, we can detect
  826. * a change in rate, but we do not have support for detecting
  827. * a disturbance in the clock-rate over the change.
  828. */
  829. if (IS_ERR(port->clk))
  830. goto exit;
  831. if (port->baudclk_rate == clk_get_rate(port->clk))
  832. goto exit;
  833. if (val == CPUFREQ_PRECHANGE) {
  834. /* we should really shut the port down whilst the
  835. * frequency change is in progress. */
  836. } else if (val == CPUFREQ_POSTCHANGE) {
  837. struct ktermios *termios;
  838. struct tty_struct *tty;
  839. if (uport->state == NULL)
  840. goto exit;
  841. tty = uport->state->port.tty;
  842. if (tty == NULL)
  843. goto exit;
  844. termios = tty->termios;
  845. if (termios == NULL) {
  846. printk(KERN_WARNING "%s: no termios?\n", __func__);
  847. goto exit;
  848. }
  849. s3c24xx_serial_set_termios(uport, termios, NULL);
  850. }
  851. exit:
  852. return 0;
  853. }
  854. static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
  855. {
  856. port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
  857. return cpufreq_register_notifier(&port->freq_transition,
  858. CPUFREQ_TRANSITION_NOTIFIER);
  859. }
  860. static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
  861. {
  862. cpufreq_unregister_notifier(&port->freq_transition,
  863. CPUFREQ_TRANSITION_NOTIFIER);
  864. }
  865. #else
  866. static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
  867. {
  868. return 0;
  869. }
  870. static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
  871. {
  872. }
  873. #endif
  874. /* s3c24xx_serial_init_port
  875. *
  876. * initialise a single serial port from the platform device given
  877. */
  878. static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
  879. struct platform_device *platdev)
  880. {
  881. struct uart_port *port = &ourport->port;
  882. struct s3c2410_uartcfg *cfg = ourport->cfg;
  883. struct resource *res;
  884. int ret;
  885. dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
  886. if (platdev == NULL)
  887. return -ENODEV;
  888. if (port->mapbase != 0)
  889. return 0;
  890. /* setup info for port */
  891. port->dev = &platdev->dev;
  892. /* Startup sequence is different for s3c64xx and higher SoC's */
  893. if (s3c24xx_serial_has_interrupt_mask(port))
  894. s3c24xx_serial_ops.startup = s3c64xx_serial_startup;
  895. port->uartclk = 1;
  896. if (cfg->uart_flags & UPF_CONS_FLOW) {
  897. dbg("s3c24xx_serial_init_port: enabling flow control\n");
  898. port->flags |= UPF_CONS_FLOW;
  899. }
  900. /* sort our the physical and virtual addresses for each UART */
  901. res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
  902. if (res == NULL) {
  903. printk(KERN_ERR "failed to find memory resource for uart\n");
  904. return -EINVAL;
  905. }
  906. dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
  907. port->mapbase = res->start;
  908. port->membase = S3C_VA_UART + (res->start & 0xfffff);
  909. ret = platform_get_irq(platdev, 0);
  910. if (ret < 0)
  911. port->irq = 0;
  912. else {
  913. port->irq = ret;
  914. ourport->rx_irq = ret;
  915. ourport->tx_irq = ret + 1;
  916. }
  917. ret = platform_get_irq(platdev, 1);
  918. if (ret > 0)
  919. ourport->tx_irq = ret;
  920. ourport->clk = clk_get(&platdev->dev, "uart");
  921. /* Keep all interrupts masked and cleared */
  922. if (s3c24xx_serial_has_interrupt_mask(port)) {
  923. wr_regl(port, S3C64XX_UINTM, 0xf);
  924. wr_regl(port, S3C64XX_UINTP, 0xf);
  925. wr_regl(port, S3C64XX_UINTSP, 0xf);
  926. }
  927. dbg("port: map=%08x, mem=%08x, irq=%d (%d,%d), clock=%ld\n",
  928. port->mapbase, port->membase, port->irq,
  929. ourport->rx_irq, ourport->tx_irq, port->uartclk);
  930. /* reset the fifos (and setup the uart) */
  931. s3c24xx_serial_resetport(port, cfg);
  932. return 0;
  933. }
  934. static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
  935. struct device_attribute *attr,
  936. char *buf)
  937. {
  938. struct uart_port *port = s3c24xx_dev_to_port(dev);
  939. struct s3c24xx_uart_port *ourport = to_ourport(port);
  940. return snprintf(buf, PAGE_SIZE, "* %s\n", ourport->baudclk->name);
  941. }
  942. static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL);
  943. /* Device driver serial port probe */
  944. static const struct of_device_id s3c24xx_uart_dt_match[];
  945. static int probe_index;
  946. static inline struct s3c24xx_serial_drv_data *s3c24xx_get_driver_data(
  947. struct platform_device *pdev)
  948. {
  949. #ifdef CONFIG_OF
  950. if (pdev->dev.of_node) {
  951. const struct of_device_id *match;
  952. match = of_match_node(s3c24xx_uart_dt_match, pdev->dev.of_node);
  953. return (struct s3c24xx_serial_drv_data *)match->data;
  954. }
  955. #endif
  956. return (struct s3c24xx_serial_drv_data *)
  957. platform_get_device_id(pdev)->driver_data;
  958. }
  959. static int s3c24xx_serial_probe(struct platform_device *pdev)
  960. {
  961. struct s3c24xx_uart_port *ourport;
  962. int ret;
  963. dbg("s3c24xx_serial_probe(%p) %d\n", pdev, probe_index);
  964. ourport = &s3c24xx_serial_ports[probe_index];
  965. ourport->drv_data = s3c24xx_get_driver_data(pdev);
  966. if (!ourport->drv_data) {
  967. dev_err(&pdev->dev, "could not find driver data\n");
  968. return -ENODEV;
  969. }
  970. ourport->info = ourport->drv_data->info;
  971. ourport->cfg = (pdev->dev.platform_data) ?
  972. (struct s3c2410_uartcfg *)pdev->dev.platform_data :
  973. ourport->drv_data->def_cfg;
  974. ourport->port.fifosize = (ourport->info->fifosize) ?
  975. ourport->info->fifosize :
  976. ourport->drv_data->fifosize[probe_index];
  977. probe_index++;
  978. dbg("%s: initialising port %p...\n", __func__, ourport);
  979. ret = s3c24xx_serial_init_port(ourport, pdev);
  980. if (ret < 0)
  981. goto probe_err;
  982. dbg("%s: adding port\n", __func__);
  983. uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
  984. platform_set_drvdata(pdev, &ourport->port);
  985. ret = device_create_file(&pdev->dev, &dev_attr_clock_source);
  986. if (ret < 0)
  987. dev_err(&pdev->dev, "failed to add clock source attr.\n");
  988. ret = s3c24xx_serial_cpufreq_register(ourport);
  989. if (ret < 0)
  990. dev_err(&pdev->dev, "failed to add cpufreq notifier\n");
  991. return 0;
  992. probe_err:
  993. return ret;
  994. }
  995. static int __devexit s3c24xx_serial_remove(struct platform_device *dev)
  996. {
  997. struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
  998. if (port) {
  999. s3c24xx_serial_cpufreq_deregister(to_ourport(port));
  1000. device_remove_file(&dev->dev, &dev_attr_clock_source);
  1001. uart_remove_one_port(&s3c24xx_uart_drv, port);
  1002. }
  1003. return 0;
  1004. }
  1005. /* UART power management code */
  1006. #ifdef CONFIG_PM_SLEEP
  1007. static int s3c24xx_serial_suspend(struct device *dev)
  1008. {
  1009. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1010. if (port)
  1011. uart_suspend_port(&s3c24xx_uart_drv, port);
  1012. return 0;
  1013. }
  1014. static int s3c24xx_serial_resume(struct device *dev)
  1015. {
  1016. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1017. struct s3c24xx_uart_port *ourport = to_ourport(port);
  1018. if (port) {
  1019. clk_enable(ourport->clk);
  1020. s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
  1021. clk_disable(ourport->clk);
  1022. uart_resume_port(&s3c24xx_uart_drv, port);
  1023. }
  1024. return 0;
  1025. }
  1026. static const struct dev_pm_ops s3c24xx_serial_pm_ops = {
  1027. .suspend = s3c24xx_serial_suspend,
  1028. .resume = s3c24xx_serial_resume,
  1029. };
  1030. #define SERIAL_SAMSUNG_PM_OPS (&s3c24xx_serial_pm_ops)
  1031. #else /* !CONFIG_PM_SLEEP */
  1032. #define SERIAL_SAMSUNG_PM_OPS NULL
  1033. #endif /* CONFIG_PM_SLEEP */
  1034. /* Console code */
  1035. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  1036. static struct uart_port *cons_uart;
  1037. static int
  1038. s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
  1039. {
  1040. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1041. unsigned long ufstat, utrstat;
  1042. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  1043. /* fifo mode - check amount of data in fifo registers... */
  1044. ufstat = rd_regl(port, S3C2410_UFSTAT);
  1045. return (ufstat & info->tx_fifofull) ? 0 : 1;
  1046. }
  1047. /* in non-fifo mode, we go and use the tx buffer empty */
  1048. utrstat = rd_regl(port, S3C2410_UTRSTAT);
  1049. return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
  1050. }
  1051. static void
  1052. s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
  1053. {
  1054. unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
  1055. while (!s3c24xx_serial_console_txrdy(port, ufcon))
  1056. barrier();
  1057. wr_regb(cons_uart, S3C2410_UTXH, ch);
  1058. }
  1059. static void
  1060. s3c24xx_serial_console_write(struct console *co, const char *s,
  1061. unsigned int count)
  1062. {
  1063. uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
  1064. }
  1065. static void __init
  1066. s3c24xx_serial_get_options(struct uart_port *port, int *baud,
  1067. int *parity, int *bits)
  1068. {
  1069. struct clk *clk;
  1070. unsigned int ulcon;
  1071. unsigned int ucon;
  1072. unsigned int ubrdiv;
  1073. unsigned long rate;
  1074. unsigned int clk_sel;
  1075. char clk_name[MAX_CLK_NAME_LENGTH];
  1076. ulcon = rd_regl(port, S3C2410_ULCON);
  1077. ucon = rd_regl(port, S3C2410_UCON);
  1078. ubrdiv = rd_regl(port, S3C2410_UBRDIV);
  1079. dbg("s3c24xx_serial_get_options: port=%p\n"
  1080. "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
  1081. port, ulcon, ucon, ubrdiv);
  1082. if ((ucon & 0xf) != 0) {
  1083. /* consider the serial port configured if the tx/rx mode set */
  1084. switch (ulcon & S3C2410_LCON_CSMASK) {
  1085. case S3C2410_LCON_CS5:
  1086. *bits = 5;
  1087. break;
  1088. case S3C2410_LCON_CS6:
  1089. *bits = 6;
  1090. break;
  1091. case S3C2410_LCON_CS7:
  1092. *bits = 7;
  1093. break;
  1094. default:
  1095. case S3C2410_LCON_CS8:
  1096. *bits = 8;
  1097. break;
  1098. }
  1099. switch (ulcon & S3C2410_LCON_PMASK) {
  1100. case S3C2410_LCON_PEVEN:
  1101. *parity = 'e';
  1102. break;
  1103. case S3C2410_LCON_PODD:
  1104. *parity = 'o';
  1105. break;
  1106. case S3C2410_LCON_PNONE:
  1107. default:
  1108. *parity = 'n';
  1109. }
  1110. /* now calculate the baud rate */
  1111. clk_sel = s3c24xx_serial_getsource(port);
  1112. sprintf(clk_name, "clk_uart_baud%d", clk_sel);
  1113. clk = clk_get(port->dev, clk_name);
  1114. if (!IS_ERR(clk) && clk != NULL)
  1115. rate = clk_get_rate(clk);
  1116. else
  1117. rate = 1;
  1118. *baud = rate / (16 * (ubrdiv + 1));
  1119. dbg("calculated baud %d\n", *baud);
  1120. }
  1121. }
  1122. static int __init
  1123. s3c24xx_serial_console_setup(struct console *co, char *options)
  1124. {
  1125. struct uart_port *port;
  1126. int baud = 9600;
  1127. int bits = 8;
  1128. int parity = 'n';
  1129. int flow = 'n';
  1130. dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
  1131. co, co->index, options);
  1132. /* is this a valid port */
  1133. if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
  1134. co->index = 0;
  1135. port = &s3c24xx_serial_ports[co->index].port;
  1136. /* is the port configured? */
  1137. if (port->mapbase == 0x0)
  1138. return -ENODEV;
  1139. cons_uart = port;
  1140. dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
  1141. /*
  1142. * Check whether an invalid uart number has been specified, and
  1143. * if so, search for the first available port that does have
  1144. * console support.
  1145. */
  1146. if (options)
  1147. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1148. else
  1149. s3c24xx_serial_get_options(port, &baud, &parity, &bits);
  1150. dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
  1151. return uart_set_options(port, co, baud, parity, bits, flow);
  1152. }
  1153. static struct console s3c24xx_serial_console = {
  1154. .name = S3C24XX_SERIAL_NAME,
  1155. .device = uart_console_device,
  1156. .flags = CON_PRINTBUFFER,
  1157. .index = -1,
  1158. .write = s3c24xx_serial_console_write,
  1159. .setup = s3c24xx_serial_console_setup,
  1160. .data = &s3c24xx_uart_drv,
  1161. };
  1162. #endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
  1163. #ifdef CONFIG_CPU_S3C2410
  1164. static struct s3c24xx_serial_drv_data s3c2410_serial_drv_data = {
  1165. .info = &(struct s3c24xx_uart_info) {
  1166. .name = "Samsung S3C2410 UART",
  1167. .type = PORT_S3C2410,
  1168. .fifosize = 16,
  1169. .rx_fifomask = S3C2410_UFSTAT_RXMASK,
  1170. .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
  1171. .rx_fifofull = S3C2410_UFSTAT_RXFULL,
  1172. .tx_fifofull = S3C2410_UFSTAT_TXFULL,
  1173. .tx_fifomask = S3C2410_UFSTAT_TXMASK,
  1174. .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
  1175. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  1176. .num_clks = 2,
  1177. .clksel_mask = S3C2410_UCON_CLKMASK,
  1178. .clksel_shift = S3C2410_UCON_CLKSHIFT,
  1179. },
  1180. .def_cfg = &(struct s3c2410_uartcfg) {
  1181. .ucon = S3C2410_UCON_DEFAULT,
  1182. .ufcon = S3C2410_UFCON_DEFAULT,
  1183. },
  1184. };
  1185. #define S3C2410_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2410_serial_drv_data)
  1186. #else
  1187. #define S3C2410_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1188. #endif
  1189. #ifdef CONFIG_CPU_S3C2412
  1190. static struct s3c24xx_serial_drv_data s3c2412_serial_drv_data = {
  1191. .info = &(struct s3c24xx_uart_info) {
  1192. .name = "Samsung S3C2412 UART",
  1193. .type = PORT_S3C2412,
  1194. .fifosize = 64,
  1195. .has_divslot = 1,
  1196. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1197. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1198. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1199. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1200. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1201. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1202. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1203. .num_clks = 4,
  1204. .clksel_mask = S3C2412_UCON_CLKMASK,
  1205. .clksel_shift = S3C2412_UCON_CLKSHIFT,
  1206. },
  1207. .def_cfg = &(struct s3c2410_uartcfg) {
  1208. .ucon = S3C2410_UCON_DEFAULT,
  1209. .ufcon = S3C2410_UFCON_DEFAULT,
  1210. },
  1211. };
  1212. #define S3C2412_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2412_serial_drv_data)
  1213. #else
  1214. #define S3C2412_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1215. #endif
  1216. #if defined(CONFIG_CPU_S3C2440) || defined(CONFIG_CPU_S3C2416) || \
  1217. defined(CONFIG_CPU_S3C2443) || defined(CONFIG_CPU_S3C2442)
  1218. static struct s3c24xx_serial_drv_data s3c2440_serial_drv_data = {
  1219. .info = &(struct s3c24xx_uart_info) {
  1220. .name = "Samsung S3C2440 UART",
  1221. .type = PORT_S3C2440,
  1222. .fifosize = 64,
  1223. .has_divslot = 1,
  1224. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1225. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1226. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1227. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1228. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1229. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1230. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1231. .num_clks = 4,
  1232. .clksel_mask = S3C2412_UCON_CLKMASK,
  1233. .clksel_shift = S3C2412_UCON_CLKSHIFT,
  1234. },
  1235. .def_cfg = &(struct s3c2410_uartcfg) {
  1236. .ucon = S3C2410_UCON_DEFAULT,
  1237. .ufcon = S3C2410_UFCON_DEFAULT,
  1238. },
  1239. };
  1240. #define S3C2440_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2440_serial_drv_data)
  1241. #else
  1242. #define S3C2440_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1243. #endif
  1244. #if defined(CONFIG_CPU_S3C6400) || defined(CONFIG_CPU_S3C6410) || \
  1245. defined(CONFIG_CPU_S5P6440) || defined(CONFIG_CPU_S5P6450) || \
  1246. defined(CONFIG_CPU_S5PC100)
  1247. static struct s3c24xx_serial_drv_data s3c6400_serial_drv_data = {
  1248. .info = &(struct s3c24xx_uart_info) {
  1249. .name = "Samsung S3C6400 UART",
  1250. .type = PORT_S3C6400,
  1251. .fifosize = 64,
  1252. .has_divslot = 1,
  1253. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1254. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1255. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1256. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1257. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1258. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1259. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1260. .num_clks = 4,
  1261. .clksel_mask = S3C6400_UCON_CLKMASK,
  1262. .clksel_shift = S3C6400_UCON_CLKSHIFT,
  1263. },
  1264. .def_cfg = &(struct s3c2410_uartcfg) {
  1265. .ucon = S3C2410_UCON_DEFAULT,
  1266. .ufcon = S3C2410_UFCON_DEFAULT,
  1267. },
  1268. };
  1269. #define S3C6400_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c6400_serial_drv_data)
  1270. #else
  1271. #define S3C6400_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1272. #endif
  1273. #ifdef CONFIG_CPU_S5PV210
  1274. static struct s3c24xx_serial_drv_data s5pv210_serial_drv_data = {
  1275. .info = &(struct s3c24xx_uart_info) {
  1276. .name = "Samsung S5PV210 UART",
  1277. .type = PORT_S3C6400,
  1278. .has_divslot = 1,
  1279. .rx_fifomask = S5PV210_UFSTAT_RXMASK,
  1280. .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
  1281. .rx_fifofull = S5PV210_UFSTAT_RXFULL,
  1282. .tx_fifofull = S5PV210_UFSTAT_TXFULL,
  1283. .tx_fifomask = S5PV210_UFSTAT_TXMASK,
  1284. .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
  1285. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  1286. .num_clks = 2,
  1287. .clksel_mask = S5PV210_UCON_CLKMASK,
  1288. .clksel_shift = S5PV210_UCON_CLKSHIFT,
  1289. },
  1290. .def_cfg = &(struct s3c2410_uartcfg) {
  1291. .ucon = S5PV210_UCON_DEFAULT,
  1292. .ufcon = S5PV210_UFCON_DEFAULT,
  1293. },
  1294. .fifosize = { 256, 64, 16, 16 },
  1295. };
  1296. #define S5PV210_SERIAL_DRV_DATA ((kernel_ulong_t)&s5pv210_serial_drv_data)
  1297. #else
  1298. #define S5PV210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1299. #endif
  1300. #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212) || \
  1301. defined(CONFIG_SOC_EXYNOS4412) || defined(CONFIG_SOC_EXYNOS5250)
  1302. static struct s3c24xx_serial_drv_data exynos4210_serial_drv_data = {
  1303. .info = &(struct s3c24xx_uart_info) {
  1304. .name = "Samsung Exynos4 UART",
  1305. .type = PORT_S3C6400,
  1306. .has_divslot = 1,
  1307. .rx_fifomask = S5PV210_UFSTAT_RXMASK,
  1308. .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
  1309. .rx_fifofull = S5PV210_UFSTAT_RXFULL,
  1310. .tx_fifofull = S5PV210_UFSTAT_TXFULL,
  1311. .tx_fifomask = S5PV210_UFSTAT_TXMASK,
  1312. .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
  1313. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  1314. .num_clks = 1,
  1315. .clksel_mask = 0,
  1316. .clksel_shift = 0,
  1317. },
  1318. .def_cfg = &(struct s3c2410_uartcfg) {
  1319. .ucon = S5PV210_UCON_DEFAULT,
  1320. .ufcon = S5PV210_UFCON_DEFAULT,
  1321. .has_fracval = 1,
  1322. },
  1323. .fifosize = { 256, 64, 16, 16 },
  1324. };
  1325. #define EXYNOS4210_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos4210_serial_drv_data)
  1326. #else
  1327. #define EXYNOS4210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1328. #endif
  1329. static struct platform_device_id s3c24xx_serial_driver_ids[] = {
  1330. {
  1331. .name = "s3c2410-uart",
  1332. .driver_data = S3C2410_SERIAL_DRV_DATA,
  1333. }, {
  1334. .name = "s3c2412-uart",
  1335. .driver_data = S3C2412_SERIAL_DRV_DATA,
  1336. }, {
  1337. .name = "s3c2440-uart",
  1338. .driver_data = S3C2440_SERIAL_DRV_DATA,
  1339. }, {
  1340. .name = "s3c6400-uart",
  1341. .driver_data = S3C6400_SERIAL_DRV_DATA,
  1342. }, {
  1343. .name = "s5pv210-uart",
  1344. .driver_data = S5PV210_SERIAL_DRV_DATA,
  1345. }, {
  1346. .name = "exynos4210-uart",
  1347. .driver_data = EXYNOS4210_SERIAL_DRV_DATA,
  1348. },
  1349. { },
  1350. };
  1351. MODULE_DEVICE_TABLE(platform, s3c24xx_serial_driver_ids);
  1352. #ifdef CONFIG_OF
  1353. static const struct of_device_id s3c24xx_uart_dt_match[] = {
  1354. { .compatible = "samsung,exynos4210-uart",
  1355. .data = (void *)EXYNOS4210_SERIAL_DRV_DATA },
  1356. {},
  1357. };
  1358. MODULE_DEVICE_TABLE(of, s3c24xx_uart_dt_match);
  1359. #else
  1360. #define s3c24xx_uart_dt_match NULL
  1361. #endif
  1362. static struct platform_driver samsung_serial_driver = {
  1363. .probe = s3c24xx_serial_probe,
  1364. .remove = __devexit_p(s3c24xx_serial_remove),
  1365. .id_table = s3c24xx_serial_driver_ids,
  1366. .driver = {
  1367. .name = "samsung-uart",
  1368. .owner = THIS_MODULE,
  1369. .pm = SERIAL_SAMSUNG_PM_OPS,
  1370. .of_match_table = s3c24xx_uart_dt_match,
  1371. },
  1372. };
  1373. /* module initialisation code */
  1374. static int __init s3c24xx_serial_modinit(void)
  1375. {
  1376. int ret;
  1377. ret = uart_register_driver(&s3c24xx_uart_drv);
  1378. if (ret < 0) {
  1379. printk(KERN_ERR "failed to register UART driver\n");
  1380. return -1;
  1381. }
  1382. return platform_driver_register(&samsung_serial_driver);
  1383. }
  1384. static void __exit s3c24xx_serial_modexit(void)
  1385. {
  1386. uart_unregister_driver(&s3c24xx_uart_drv);
  1387. }
  1388. module_init(s3c24xx_serial_modinit);
  1389. module_exit(s3c24xx_serial_modexit);
  1390. MODULE_ALIAS("platform:samsung-uart");
  1391. MODULE_DESCRIPTION("Samsung SoC Serial port driver");
  1392. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  1393. MODULE_LICENSE("GPL v2");