samsung.c 44 KB

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