samsung.c 42 KB

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