samsung.c 43 KB

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