samsung.c 37 KB

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