s3c2410.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  1. /*
  2. * linux/drivers/serial/s3c2410.c
  3. *
  4. * Driver for onboard UARTs on the Samsung S3C24XX
  5. *
  6. * Based on drivers/char/serial.c and drivers/char/21285.c
  7. *
  8. * Ben Dooks, (c) 2003-2005 Simtec Electronics
  9. * http://www.simtec.co.uk/products/SWLINUX/
  10. *
  11. * Changelog:
  12. *
  13. * 22-Jul-2004 BJD Finished off device rewrite
  14. *
  15. * 21-Jul-2004 BJD Thanks to <herbet@13thfloor.at> for pointing out
  16. * problems with baud rate and loss of IR settings. Update
  17. * to add configuration via platform_device structure
  18. *
  19. * 28-Sep-2004 BJD Re-write for the following items
  20. * - S3C2410 and S3C2440 serial support
  21. * - Power Management support
  22. * - Fix console via IrDA devices
  23. * - SysReq (Herbert Pötzl)
  24. * - Break character handling (Herbert Pötzl)
  25. * - spin-lock initialisation (Dimitry Andric)
  26. * - added clock control
  27. * - updated init code to use platform_device info
  28. *
  29. * 06-Mar-2005 BJD Add s3c2440 fclk clock source
  30. *
  31. * 09-Mar-2005 BJD Add s3c2400 support
  32. *
  33. * 10-Mar-2005 LCVR Changed S3C2410_VA_UART to S3C24XX_VA_UART
  34. */
  35. /* Note on 2440 fclk clock source handling
  36. *
  37. * Whilst it is possible to use the fclk as clock source, the method
  38. * of properly switching too/from this is currently un-implemented, so
  39. * whichever way is configured at startup is the one that will be used.
  40. */
  41. /* Hote on 2410 error handling
  42. *
  43. * The s3c2410 manual has a love/hate affair with the contents of the
  44. * UERSTAT register in the UART blocks, and keeps marking some of the
  45. * error bits as reserved. Having checked with the s3c2410x01,
  46. * it copes with BREAKs properly, so I am happy to ignore the RESERVED
  47. * feature from the latter versions of the manual.
  48. *
  49. * If it becomes aparrent that latter versions of the 2410 remove these
  50. * bits, then action will have to be taken to differentiate the versions
  51. * and change the policy on BREAK
  52. *
  53. * BJD, 04-Nov-2004
  54. */
  55. #include <linux/config.h>
  56. #if defined(CONFIG_SERIAL_S3C2410_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  57. #define SUPPORT_SYSRQ
  58. #endif
  59. #include <linux/module.h>
  60. #include <linux/ioport.h>
  61. #include <linux/device.h>
  62. #include <linux/init.h>
  63. #include <linux/sysrq.h>
  64. #include <linux/console.h>
  65. #include <linux/tty.h>
  66. #include <linux/tty_flip.h>
  67. #include <linux/serial_core.h>
  68. #include <linux/serial.h>
  69. #include <linux/delay.h>
  70. #include <asm/io.h>
  71. #include <asm/irq.h>
  72. #include <asm/hardware.h>
  73. #include <asm/hardware/clock.h>
  74. #include <asm/arch/regs-serial.h>
  75. #include <asm/arch/regs-gpio.h>
  76. #include <asm/mach-types.h>
  77. /* structures */
  78. struct s3c24xx_uart_info {
  79. char *name;
  80. unsigned int type;
  81. unsigned int fifosize;
  82. unsigned long rx_fifomask;
  83. unsigned long rx_fifoshift;
  84. unsigned long rx_fifofull;
  85. unsigned long tx_fifomask;
  86. unsigned long tx_fifoshift;
  87. unsigned long tx_fifofull;
  88. /* clock source control */
  89. int (*get_clksrc)(struct uart_port *, struct s3c24xx_uart_clksrc *clk);
  90. int (*set_clksrc)(struct uart_port *, struct s3c24xx_uart_clksrc *clk);
  91. /* uart controls */
  92. int (*reset_port)(struct uart_port *, struct s3c2410_uartcfg *);
  93. };
  94. struct s3c24xx_uart_port {
  95. unsigned char rx_claimed;
  96. unsigned char tx_claimed;
  97. struct s3c24xx_uart_info *info;
  98. struct s3c24xx_uart_clksrc *clksrc;
  99. struct clk *clk;
  100. struct clk *baudclk;
  101. struct uart_port port;
  102. };
  103. /* configuration defines */
  104. #if 0
  105. #if 1
  106. /* send debug to the low-level output routines */
  107. extern void printascii(const char *);
  108. static void
  109. s3c24xx_serial_dbg(const char *fmt, ...)
  110. {
  111. va_list va;
  112. char buff[256];
  113. va_start(va, fmt);
  114. vsprintf(buff, fmt, va);
  115. va_end(va);
  116. printascii(buff);
  117. }
  118. #define dbg(x...) s3c24xx_serial_dbg(x)
  119. #else
  120. #define dbg(x...) printk(KERN_DEBUG "s3c24xx: ");
  121. #endif
  122. #else /* no debug */
  123. #define dbg(x...) do {} while(0)
  124. #endif
  125. /* UART name and device definitions */
  126. #define S3C24XX_SERIAL_NAME "ttySAC"
  127. #define S3C24XX_SERIAL_DEVFS "tts/"
  128. #define S3C24XX_SERIAL_MAJOR 204
  129. #define S3C24XX_SERIAL_MINOR 64
  130. /* conversion functions */
  131. #define s3c24xx_dev_to_port(__dev) (struct uart_port *)dev_get_drvdata(__dev)
  132. #define s3c24xx_dev_to_cfg(__dev) (struct s3c2410_uartcfg *)((__dev)->platform_data)
  133. /* we can support 3 uarts, but not always use them */
  134. #define NR_PORTS (3)
  135. /* port irq numbers */
  136. #define TX_IRQ(port) ((port)->irq + 1)
  137. #define RX_IRQ(port) ((port)->irq)
  138. /* register access controls */
  139. #define portaddr(port, reg) ((port)->membase + (reg))
  140. #define rd_regb(port, reg) (__raw_readb(portaddr(port, reg)))
  141. #define rd_regl(port, reg) (__raw_readl(portaddr(port, reg)))
  142. #define wr_regb(port, reg, val) \
  143. do { __raw_writeb(val, portaddr(port, reg)); } while(0)
  144. #define wr_regl(port, reg, val) \
  145. do { __raw_writel(val, portaddr(port, reg)); } while(0)
  146. /* macros to change one thing to another */
  147. #define tx_enabled(port) ((port)->unused[0])
  148. #define rx_enabled(port) ((port)->unused[1])
  149. /* flag to ignore all characters comming in */
  150. #define RXSTAT_DUMMY_READ (0x10000000)
  151. static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
  152. {
  153. return container_of(port, struct s3c24xx_uart_port, port);
  154. }
  155. /* translate a port to the device name */
  156. static inline char *s3c24xx_serial_portname(struct uart_port *port)
  157. {
  158. return to_platform_device(port->dev)->name;
  159. }
  160. static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
  161. {
  162. return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);
  163. }
  164. static void s3c24xx_serial_rx_enable(struct uart_port *port)
  165. {
  166. unsigned long flags;
  167. unsigned int ucon, ufcon;
  168. int count = 10000;
  169. spin_lock_irqsave(&port->lock, flags);
  170. while (--count && !s3c24xx_serial_txempty_nofifo(port))
  171. udelay(100);
  172. ufcon = rd_regl(port, S3C2410_UFCON);
  173. ufcon |= S3C2410_UFCON_RESETRX;
  174. wr_regl(port, S3C2410_UFCON, ufcon);
  175. ucon = rd_regl(port, S3C2410_UCON);
  176. ucon |= S3C2410_UCON_RXIRQMODE;
  177. wr_regl(port, S3C2410_UCON, ucon);
  178. rx_enabled(port) = 1;
  179. spin_unlock_irqrestore(&port->lock, flags);
  180. }
  181. static void s3c24xx_serial_rx_disable(struct uart_port *port)
  182. {
  183. unsigned long flags;
  184. unsigned int ucon;
  185. spin_lock_irqsave(&port->lock, flags);
  186. ucon = rd_regl(port, S3C2410_UCON);
  187. ucon &= ~S3C2410_UCON_RXIRQMODE;
  188. wr_regl(port, S3C2410_UCON, ucon);
  189. rx_enabled(port) = 0;
  190. spin_unlock_irqrestore(&port->lock, flags);
  191. }
  192. static void
  193. s3c24xx_serial_stop_tx(struct uart_port *port, unsigned int tty_stop)
  194. {
  195. if (tx_enabled(port)) {
  196. disable_irq(TX_IRQ(port));
  197. tx_enabled(port) = 0;
  198. if (port->flags & UPF_CONS_FLOW)
  199. s3c24xx_serial_rx_enable(port);
  200. }
  201. }
  202. static void
  203. s3c24xx_serial_start_tx(struct uart_port *port, unsigned int tty_start)
  204. {
  205. if (!tx_enabled(port)) {
  206. if (port->flags & UPF_CONS_FLOW)
  207. s3c24xx_serial_rx_disable(port);
  208. enable_irq(TX_IRQ(port));
  209. tx_enabled(port) = 1;
  210. }
  211. }
  212. static void s3c24xx_serial_stop_rx(struct uart_port *port)
  213. {
  214. if (rx_enabled(port)) {
  215. dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
  216. disable_irq(RX_IRQ(port));
  217. rx_enabled(port) = 0;
  218. }
  219. }
  220. static void s3c24xx_serial_enable_ms(struct uart_port *port)
  221. {
  222. }
  223. static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
  224. {
  225. return to_ourport(port)->info;
  226. }
  227. static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
  228. {
  229. if (port->dev == NULL)
  230. return NULL;
  231. return (struct s3c2410_uartcfg *)port->dev->platform_data;
  232. }
  233. static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
  234. unsigned long ufstat)
  235. {
  236. struct s3c24xx_uart_info *info = ourport->info;
  237. if (ufstat & info->rx_fifofull)
  238. return info->fifosize;
  239. return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
  240. }
  241. /* ? - where has parity gone?? */
  242. #define S3C2410_UERSTAT_PARITY (0x1000)
  243. static irqreturn_t
  244. s3c24xx_serial_rx_chars(int irq, void *dev_id, struct pt_regs *regs)
  245. {
  246. struct s3c24xx_uart_port *ourport = dev_id;
  247. struct uart_port *port = &ourport->port;
  248. struct tty_struct *tty = port->info->tty;
  249. unsigned int ufcon, ch, flag, ufstat, uerstat;
  250. int max_count = 64;
  251. while (max_count-- > 0) {
  252. ufcon = rd_regl(port, S3C2410_UFCON);
  253. ufstat = rd_regl(port, S3C2410_UFSTAT);
  254. if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
  255. break;
  256. if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
  257. if (tty->low_latency)
  258. tty_flip_buffer_push(tty);
  259. /*
  260. * If this failed then we will throw away the
  261. * bytes but must do so to clear interrupts
  262. */
  263. }
  264. uerstat = rd_regl(port, S3C2410_UERSTAT);
  265. ch = rd_regb(port, S3C2410_URXH);
  266. if (port->flags & UPF_CONS_FLOW) {
  267. int txe = s3c24xx_serial_txempty_nofifo(port);
  268. if (rx_enabled(port)) {
  269. if (!txe) {
  270. rx_enabled(port) = 0;
  271. continue;
  272. }
  273. } else {
  274. if (txe) {
  275. ufcon |= S3C2410_UFCON_RESETRX;
  276. wr_regl(port, S3C2410_UFCON, ufcon);
  277. rx_enabled(port) = 1;
  278. goto out;
  279. }
  280. continue;
  281. }
  282. }
  283. /* insert the character into the buffer */
  284. flag = TTY_NORMAL;
  285. port->icount.rx++;
  286. if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
  287. dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
  288. ch, uerstat);
  289. /* check for break */
  290. if (uerstat & S3C2410_UERSTAT_BREAK) {
  291. dbg("break!\n");
  292. port->icount.brk++;
  293. if (uart_handle_break(port))
  294. goto ignore_char;
  295. }
  296. if (uerstat & S3C2410_UERSTAT_FRAME)
  297. port->icount.frame++;
  298. if (uerstat & S3C2410_UERSTAT_OVERRUN)
  299. port->icount.overrun++;
  300. uerstat &= port->read_status_mask;
  301. if (uerstat & S3C2410_UERSTAT_BREAK)
  302. flag = TTY_BREAK;
  303. else if (uerstat & S3C2410_UERSTAT_PARITY)
  304. flag = TTY_PARITY;
  305. else if (uerstat & ( S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_OVERRUN))
  306. flag = TTY_FRAME;
  307. }
  308. if (uart_handle_sysrq_char(port, ch, regs))
  309. goto ignore_char;
  310. uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN, ch, flag);
  311. ignore_char:
  312. continue;
  313. }
  314. tty_flip_buffer_push(tty);
  315. out:
  316. return IRQ_HANDLED;
  317. }
  318. static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id, struct pt_regs *regs)
  319. {
  320. struct s3c24xx_uart_port *ourport = id;
  321. struct uart_port *port = &ourport->port;
  322. struct circ_buf *xmit = &port->info->xmit;
  323. int count = 256;
  324. if (port->x_char) {
  325. wr_regb(port, S3C2410_UTXH, port->x_char);
  326. port->icount.tx++;
  327. port->x_char = 0;
  328. goto out;
  329. }
  330. /* if there isnt anything more to transmit, or the uart is now
  331. * stopped, disable the uart and exit
  332. */
  333. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  334. s3c24xx_serial_stop_tx(port, 0);
  335. goto out;
  336. }
  337. /* try and drain the buffer... */
  338. while (!uart_circ_empty(xmit) && count-- > 0) {
  339. if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
  340. break;
  341. wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
  342. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  343. port->icount.tx++;
  344. }
  345. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  346. uart_write_wakeup(port);
  347. if (uart_circ_empty(xmit))
  348. s3c24xx_serial_stop_tx(port, 0);
  349. out:
  350. return IRQ_HANDLED;
  351. }
  352. static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
  353. {
  354. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  355. unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
  356. unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
  357. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  358. if ((ufstat & info->tx_fifomask) != 0 ||
  359. (ufstat & info->tx_fifofull))
  360. return 0;
  361. return 1;
  362. }
  363. return s3c24xx_serial_txempty_nofifo(port);
  364. }
  365. /* no modem control lines */
  366. static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
  367. {
  368. unsigned int umstat = rd_regb(port,S3C2410_UMSTAT);
  369. if (umstat & S3C2410_UMSTAT_CTS)
  370. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  371. else
  372. return TIOCM_CAR | TIOCM_DSR;
  373. }
  374. static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  375. {
  376. /* todo - possibly remove AFC and do manual CTS */
  377. }
  378. static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
  379. {
  380. unsigned long flags;
  381. unsigned int ucon;
  382. spin_lock_irqsave(&port->lock, flags);
  383. ucon = rd_regl(port, S3C2410_UCON);
  384. if (break_state)
  385. ucon |= S3C2410_UCON_SBREAK;
  386. else
  387. ucon &= ~S3C2410_UCON_SBREAK;
  388. wr_regl(port, S3C2410_UCON, ucon);
  389. spin_unlock_irqrestore(&port->lock, flags);
  390. }
  391. static void s3c24xx_serial_shutdown(struct uart_port *port)
  392. {
  393. struct s3c24xx_uart_port *ourport = to_ourport(port);
  394. if (ourport->tx_claimed) {
  395. free_irq(TX_IRQ(port), ourport);
  396. tx_enabled(port) = 0;
  397. ourport->tx_claimed = 0;
  398. }
  399. if (ourport->rx_claimed) {
  400. free_irq(RX_IRQ(port), ourport);
  401. ourport->rx_claimed = 0;
  402. rx_enabled(port) = 0;
  403. }
  404. }
  405. static int s3c24xx_serial_startup(struct uart_port *port)
  406. {
  407. struct s3c24xx_uart_port *ourport = to_ourport(port);
  408. unsigned long flags;
  409. int ret;
  410. dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
  411. port->mapbase, port->membase);
  412. local_irq_save(flags);
  413. rx_enabled(port) = 1;
  414. ret = request_irq(RX_IRQ(port),
  415. s3c24xx_serial_rx_chars, 0,
  416. s3c24xx_serial_portname(port), ourport);
  417. if (ret != 0) {
  418. printk(KERN_ERR "cannot get irq %d\n", RX_IRQ(port));
  419. return ret;
  420. }
  421. ourport->rx_claimed = 1;
  422. dbg("requesting tx irq...\n");
  423. tx_enabled(port) = 1;
  424. ret = request_irq(TX_IRQ(port),
  425. s3c24xx_serial_tx_chars, 0,
  426. s3c24xx_serial_portname(port), ourport);
  427. if (ret) {
  428. printk(KERN_ERR "cannot get irq %d\n", TX_IRQ(port));
  429. goto err;
  430. }
  431. ourport->tx_claimed = 1;
  432. dbg("s3c24xx_serial_startup ok\n");
  433. /* the port reset code should have done the correct
  434. * register setup for the port controls */
  435. local_irq_restore(flags);
  436. return ret;
  437. err:
  438. s3c24xx_serial_shutdown(port);
  439. local_irq_restore(flags);
  440. return ret;
  441. }
  442. /* power power management control */
  443. static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
  444. unsigned int old)
  445. {
  446. struct s3c24xx_uart_port *ourport = to_ourport(port);
  447. switch (level) {
  448. case 3:
  449. if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
  450. clk_disable(ourport->baudclk);
  451. clk_disable(ourport->clk);
  452. break;
  453. case 0:
  454. clk_enable(ourport->clk);
  455. if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
  456. clk_enable(ourport->baudclk);
  457. break;
  458. default:
  459. printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
  460. }
  461. }
  462. /* baud rate calculation
  463. *
  464. * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
  465. * of different sources, including the peripheral clock ("pclk") and an
  466. * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
  467. * with a programmable extra divisor.
  468. *
  469. * The following code goes through the clock sources, and calculates the
  470. * baud clocks (and the resultant actual baud rates) and then tries to
  471. * pick the closest one and select that.
  472. *
  473. */
  474. #define MAX_CLKS (8)
  475. static struct s3c24xx_uart_clksrc tmp_clksrc = {
  476. .name = "pclk",
  477. .min_baud = 0,
  478. .max_baud = 0,
  479. .divisor = 1,
  480. };
  481. static inline int
  482. s3c24xx_serial_getsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
  483. {
  484. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  485. return (info->get_clksrc)(port, c);
  486. }
  487. static inline int
  488. s3c24xx_serial_setsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
  489. {
  490. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  491. return (info->set_clksrc)(port, c);
  492. }
  493. struct baud_calc {
  494. struct s3c24xx_uart_clksrc *clksrc;
  495. unsigned int calc;
  496. unsigned int quot;
  497. struct clk *src;
  498. };
  499. static int s3c24xx_serial_calcbaud(struct baud_calc *calc,
  500. struct uart_port *port,
  501. struct s3c24xx_uart_clksrc *clksrc,
  502. unsigned int baud)
  503. {
  504. unsigned long rate;
  505. calc->src = clk_get(port->dev, clksrc->name);
  506. if (calc->src == NULL || IS_ERR(calc->src))
  507. return 0;
  508. rate = clk_get_rate(calc->src);
  509. rate /= clksrc->divisor;
  510. calc->clksrc = clksrc;
  511. calc->quot = (rate + (8 * baud)) / (16 * baud);
  512. calc->calc = (rate / (calc->quot * 16));
  513. calc->quot--;
  514. return 1;
  515. }
  516. static unsigned int s3c24xx_serial_getclk(struct uart_port *port,
  517. struct s3c24xx_uart_clksrc **clksrc,
  518. struct clk **clk,
  519. unsigned int baud)
  520. {
  521. struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
  522. struct s3c24xx_uart_clksrc *clkp;
  523. struct baud_calc res[MAX_CLKS];
  524. struct baud_calc *resptr, *best, *sptr;
  525. int i;
  526. clkp = cfg->clocks;
  527. best = NULL;
  528. if (cfg->clocks_size < 2) {
  529. if (cfg->clocks_size == 0)
  530. clkp = &tmp_clksrc;
  531. /* check to see if we're sourcing fclk, and if so we're
  532. * going to have to update the clock source
  533. */
  534. if (strcmp(clkp->name, "fclk") == 0) {
  535. struct s3c24xx_uart_clksrc src;
  536. s3c24xx_serial_getsource(port, &src);
  537. /* check that the port already using fclk, and if
  538. * not, then re-select fclk
  539. */
  540. if (strcmp(src.name, clkp->name) == 0) {
  541. s3c24xx_serial_setsource(port, clkp);
  542. s3c24xx_serial_getsource(port, &src);
  543. }
  544. clkp->divisor = src.divisor;
  545. }
  546. s3c24xx_serial_calcbaud(res, port, clkp, baud);
  547. best = res;
  548. resptr = best + 1;
  549. } else {
  550. resptr = res;
  551. for (i = 0; i < cfg->clocks_size; i++, clkp++) {
  552. if (s3c24xx_serial_calcbaud(resptr, port, clkp, baud))
  553. resptr++;
  554. }
  555. }
  556. /* ok, we now need to select the best clock we found */
  557. if (!best) {
  558. unsigned int deviation = (1<<30)|((1<<30)-1);
  559. int calc_deviation;
  560. for (sptr = res; sptr < resptr; sptr++) {
  561. printk(KERN_DEBUG
  562. "found clk %p (%s) quot %d, calc %d\n",
  563. sptr->clksrc, sptr->clksrc->name,
  564. sptr->quot, sptr->calc);
  565. calc_deviation = baud - sptr->calc;
  566. if (calc_deviation < 0)
  567. calc_deviation = -calc_deviation;
  568. if (calc_deviation < deviation) {
  569. best = sptr;
  570. deviation = calc_deviation;
  571. }
  572. }
  573. printk(KERN_DEBUG "best %p (deviation %d)\n", best, deviation);
  574. }
  575. printk(KERN_DEBUG "selected clock %p (%s) quot %d, calc %d\n",
  576. best->clksrc, best->clksrc->name, best->quot, best->calc);
  577. /* store results to pass back */
  578. *clksrc = best->clksrc;
  579. *clk = best->src;
  580. return best->quot;
  581. }
  582. static void s3c24xx_serial_set_termios(struct uart_port *port,
  583. struct termios *termios,
  584. struct termios *old)
  585. {
  586. struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
  587. struct s3c24xx_uart_port *ourport = to_ourport(port);
  588. struct s3c24xx_uart_clksrc *clksrc;
  589. struct clk *clk;
  590. unsigned long flags;
  591. unsigned int baud, quot;
  592. unsigned int ulcon;
  593. unsigned int umcon;
  594. /*
  595. * We don't support modem control lines.
  596. */
  597. termios->c_cflag &= ~(HUPCL | CMSPAR);
  598. termios->c_cflag |= CLOCAL;
  599. /*
  600. * Ask the core to calculate the divisor for us.
  601. */
  602. baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
  603. if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
  604. quot = port->custom_divisor;
  605. else
  606. quot = s3c24xx_serial_getclk(port, &clksrc, &clk, baud);
  607. /* check to see if we need to change clock source */
  608. if (ourport->clksrc != clksrc || ourport->baudclk != clk) {
  609. s3c24xx_serial_setsource(port, clksrc);
  610. if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
  611. clk_disable(ourport->baudclk);
  612. clk_unuse(ourport->baudclk);
  613. ourport->baudclk = NULL;
  614. }
  615. clk_use(clk);
  616. clk_enable(clk);
  617. ourport->clksrc = clksrc;
  618. ourport->baudclk = clk;
  619. }
  620. switch (termios->c_cflag & CSIZE) {
  621. case CS5:
  622. dbg("config: 5bits/char\n");
  623. ulcon = S3C2410_LCON_CS5;
  624. break;
  625. case CS6:
  626. dbg("config: 6bits/char\n");
  627. ulcon = S3C2410_LCON_CS6;
  628. break;
  629. case CS7:
  630. dbg("config: 7bits/char\n");
  631. ulcon = S3C2410_LCON_CS7;
  632. break;
  633. case CS8:
  634. default:
  635. dbg("config: 8bits/char\n");
  636. ulcon = S3C2410_LCON_CS8;
  637. break;
  638. }
  639. /* preserve original lcon IR settings */
  640. ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
  641. if (termios->c_cflag & CSTOPB)
  642. ulcon |= S3C2410_LCON_STOPB;
  643. umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
  644. if (termios->c_cflag & PARENB) {
  645. if (termios->c_cflag & PARODD)
  646. ulcon |= S3C2410_LCON_PODD;
  647. else
  648. ulcon |= S3C2410_LCON_PEVEN;
  649. } else {
  650. ulcon |= S3C2410_LCON_PNONE;
  651. }
  652. spin_lock_irqsave(&port->lock, flags);
  653. dbg("setting ulcon to %08x, brddiv to %d\n", ulcon, quot);
  654. wr_regl(port, S3C2410_ULCON, ulcon);
  655. wr_regl(port, S3C2410_UBRDIV, quot);
  656. wr_regl(port, S3C2410_UMCON, umcon);
  657. dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
  658. rd_regl(port, S3C2410_ULCON),
  659. rd_regl(port, S3C2410_UCON),
  660. rd_regl(port, S3C2410_UFCON));
  661. /*
  662. * Update the per-port timeout.
  663. */
  664. uart_update_timeout(port, termios->c_cflag, baud);
  665. /*
  666. * Which character status flags are we interested in?
  667. */
  668. port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
  669. if (termios->c_iflag & INPCK)
  670. port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
  671. /*
  672. * Which character status flags should we ignore?
  673. */
  674. port->ignore_status_mask = 0;
  675. if (termios->c_iflag & IGNPAR)
  676. port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
  677. if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
  678. port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
  679. /*
  680. * Ignore all characters if CREAD is not set.
  681. */
  682. if ((termios->c_cflag & CREAD) == 0)
  683. port->ignore_status_mask |= RXSTAT_DUMMY_READ;
  684. spin_unlock_irqrestore(&port->lock, flags);
  685. }
  686. static const char *s3c24xx_serial_type(struct uart_port *port)
  687. {
  688. switch (port->type) {
  689. case PORT_S3C2410:
  690. return "S3C2410";
  691. case PORT_S3C2440:
  692. return "S3C2440";
  693. default:
  694. return NULL;
  695. }
  696. }
  697. #define MAP_SIZE (0x100)
  698. static void s3c24xx_serial_release_port(struct uart_port *port)
  699. {
  700. release_mem_region(port->mapbase, MAP_SIZE);
  701. }
  702. static int s3c24xx_serial_request_port(struct uart_port *port)
  703. {
  704. char *name = s3c24xx_serial_portname(port);
  705. return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
  706. }
  707. static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
  708. {
  709. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  710. if (flags & UART_CONFIG_TYPE &&
  711. s3c24xx_serial_request_port(port) == 0)
  712. port->type = info->type;
  713. }
  714. /*
  715. * verify the new serial_struct (for TIOCSSERIAL).
  716. */
  717. static int
  718. s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  719. {
  720. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  721. if (ser->type != PORT_UNKNOWN && ser->type != info->type)
  722. return -EINVAL;
  723. return 0;
  724. }
  725. #ifdef CONFIG_SERIAL_S3C2410_CONSOLE
  726. static struct console s3c24xx_serial_console;
  727. #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
  728. #else
  729. #define S3C24XX_SERIAL_CONSOLE NULL
  730. #endif
  731. static struct uart_ops s3c24xx_serial_ops = {
  732. .pm = s3c24xx_serial_pm,
  733. .tx_empty = s3c24xx_serial_tx_empty,
  734. .get_mctrl = s3c24xx_serial_get_mctrl,
  735. .set_mctrl = s3c24xx_serial_set_mctrl,
  736. .stop_tx = s3c24xx_serial_stop_tx,
  737. .start_tx = s3c24xx_serial_start_tx,
  738. .stop_rx = s3c24xx_serial_stop_rx,
  739. .enable_ms = s3c24xx_serial_enable_ms,
  740. .break_ctl = s3c24xx_serial_break_ctl,
  741. .startup = s3c24xx_serial_startup,
  742. .shutdown = s3c24xx_serial_shutdown,
  743. .set_termios = s3c24xx_serial_set_termios,
  744. .type = s3c24xx_serial_type,
  745. .release_port = s3c24xx_serial_release_port,
  746. .request_port = s3c24xx_serial_request_port,
  747. .config_port = s3c24xx_serial_config_port,
  748. .verify_port = s3c24xx_serial_verify_port,
  749. };
  750. static struct uart_driver s3c24xx_uart_drv = {
  751. .owner = THIS_MODULE,
  752. .dev_name = "s3c2410_serial",
  753. .nr = 3,
  754. .cons = S3C24XX_SERIAL_CONSOLE,
  755. .driver_name = S3C24XX_SERIAL_NAME,
  756. .devfs_name = S3C24XX_SERIAL_DEVFS,
  757. .major = S3C24XX_SERIAL_MAJOR,
  758. .minor = S3C24XX_SERIAL_MINOR,
  759. };
  760. static struct s3c24xx_uart_port s3c24xx_serial_ports[NR_PORTS] = {
  761. [0] = {
  762. .port = {
  763. .lock = SPIN_LOCK_UNLOCKED,
  764. .iotype = UPIO_MEM,
  765. .irq = IRQ_S3CUART_RX0,
  766. .uartclk = 0,
  767. .fifosize = 16,
  768. .ops = &s3c24xx_serial_ops,
  769. .flags = UPF_BOOT_AUTOCONF,
  770. .line = 0,
  771. }
  772. },
  773. [1] = {
  774. .port = {
  775. .lock = SPIN_LOCK_UNLOCKED,
  776. .iotype = UPIO_MEM,
  777. .irq = IRQ_S3CUART_RX1,
  778. .uartclk = 0,
  779. .fifosize = 16,
  780. .ops = &s3c24xx_serial_ops,
  781. .flags = UPF_BOOT_AUTOCONF,
  782. .line = 1,
  783. }
  784. },
  785. #if NR_PORTS > 2
  786. [2] = {
  787. .port = {
  788. .lock = SPIN_LOCK_UNLOCKED,
  789. .iotype = UPIO_MEM,
  790. .irq = IRQ_S3CUART_RX2,
  791. .uartclk = 0,
  792. .fifosize = 16,
  793. .ops = &s3c24xx_serial_ops,
  794. .flags = UPF_BOOT_AUTOCONF,
  795. .line = 2,
  796. }
  797. }
  798. #endif
  799. };
  800. /* s3c24xx_serial_resetport
  801. *
  802. * wrapper to call the specific reset for this port (reset the fifos
  803. * and the settings)
  804. */
  805. static inline int s3c24xx_serial_resetport(struct uart_port * port,
  806. struct s3c2410_uartcfg *cfg)
  807. {
  808. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  809. return (info->reset_port)(port, cfg);
  810. }
  811. /* s3c24xx_serial_init_port
  812. *
  813. * initialise a single serial port from the platform device given
  814. */
  815. static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
  816. struct s3c24xx_uart_info *info,
  817. struct platform_device *platdev)
  818. {
  819. struct uart_port *port = &ourport->port;
  820. struct s3c2410_uartcfg *cfg;
  821. struct resource *res;
  822. dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
  823. if (platdev == NULL)
  824. return -ENODEV;
  825. cfg = s3c24xx_dev_to_cfg(&platdev->dev);
  826. if (port->mapbase != 0)
  827. return 0;
  828. if (cfg->hwport > 3)
  829. return -EINVAL;
  830. /* setup info for port */
  831. port->dev = &platdev->dev;
  832. ourport->info = info;
  833. /* copy the info in from provided structure */
  834. ourport->port.fifosize = info->fifosize;
  835. dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
  836. port->uartclk = 1;
  837. if (cfg->uart_flags & UPF_CONS_FLOW) {
  838. dbg("s3c24xx_serial_init_port: enabling flow control\n");
  839. port->flags |= UPF_CONS_FLOW;
  840. }
  841. /* sort our the physical and virtual addresses for each UART */
  842. res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
  843. if (res == NULL) {
  844. printk(KERN_ERR "failed to find memory resource for uart\n");
  845. return -EINVAL;
  846. }
  847. dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
  848. port->mapbase = res->start;
  849. port->membase = S3C24XX_VA_UART + (res->start - S3C2410_PA_UART);
  850. port->irq = platform_get_irq(platdev, 0);
  851. ourport->clk = clk_get(&platdev->dev, "uart");
  852. if (ourport->clk != NULL && !IS_ERR(ourport->clk))
  853. clk_use(ourport->clk);
  854. dbg("port: map=%08x, mem=%08x, irq=%d, clock=%ld\n",
  855. port->mapbase, port->membase, port->irq, port->uartclk);
  856. /* reset the fifos (and setup the uart) */
  857. s3c24xx_serial_resetport(port, cfg);
  858. return 0;
  859. }
  860. /* Device driver serial port probe */
  861. static int probe_index = 0;
  862. int s3c24xx_serial_probe(struct device *_dev,
  863. struct s3c24xx_uart_info *info)
  864. {
  865. struct s3c24xx_uart_port *ourport;
  866. struct platform_device *dev = to_platform_device(_dev);
  867. int ret;
  868. dbg("s3c24xx_serial_probe(%p, %p) %d\n", _dev, info, probe_index);
  869. ourport = &s3c24xx_serial_ports[probe_index];
  870. probe_index++;
  871. dbg("%s: initialising port %p...\n", __FUNCTION__, ourport);
  872. ret = s3c24xx_serial_init_port(ourport, info, dev);
  873. if (ret < 0)
  874. goto probe_err;
  875. dbg("%s: adding port\n", __FUNCTION__);
  876. uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
  877. dev_set_drvdata(_dev, &ourport->port);
  878. return 0;
  879. probe_err:
  880. return ret;
  881. }
  882. int s3c24xx_serial_remove(struct device *_dev)
  883. {
  884. struct uart_port *port = s3c24xx_dev_to_port(_dev);
  885. if (port)
  886. uart_remove_one_port(&s3c24xx_uart_drv, port);
  887. return 0;
  888. }
  889. /* UART power management code */
  890. #ifdef CONFIG_PM
  891. int s3c24xx_serial_suspend(struct device *dev, pm_message_t state, u32 level)
  892. {
  893. struct uart_port *port = s3c24xx_dev_to_port(dev);
  894. if (port && level == SUSPEND_DISABLE)
  895. uart_suspend_port(&s3c24xx_uart_drv, port);
  896. return 0;
  897. }
  898. int s3c24xx_serial_resume(struct device *dev, u32 level)
  899. {
  900. struct uart_port *port = s3c24xx_dev_to_port(dev);
  901. struct s3c24xx_uart_port *ourport = to_ourport(port);
  902. if (port && level == RESUME_ENABLE) {
  903. clk_enable(ourport->clk);
  904. s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
  905. clk_disable(ourport->clk);
  906. uart_resume_port(&s3c24xx_uart_drv, port);
  907. }
  908. return 0;
  909. }
  910. #else
  911. #define s3c24xx_serial_suspend NULL
  912. #define s3c24xx_serial_resume NULL
  913. #endif
  914. int s3c24xx_serial_init(struct device_driver *drv,
  915. struct s3c24xx_uart_info *info)
  916. {
  917. dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
  918. return driver_register(drv);
  919. }
  920. /* now comes the code to initialise either the s3c2410 or s3c2440 serial
  921. * port information
  922. */
  923. /* cpu specific variations on the serial port support */
  924. #ifdef CONFIG_CPU_S3C2400
  925. static int s3c2400_serial_getsource(struct uart_port *port,
  926. struct s3c24xx_uart_clksrc *clk)
  927. {
  928. clk->divisor = 1;
  929. clk->name = "pclk";
  930. return 0;
  931. }
  932. static int s3c2400_serial_setsource(struct uart_port *port,
  933. struct s3c24xx_uart_clksrc *clk)
  934. {
  935. return 0;
  936. }
  937. static int s3c2400_serial_resetport(struct uart_port *port,
  938. struct s3c2410_uartcfg *cfg)
  939. {
  940. dbg("s3c2400_serial_resetport: port=%p (%08lx), cfg=%p\n",
  941. port, port->mapbase, cfg);
  942. wr_regl(port, S3C2410_UCON, cfg->ucon);
  943. wr_regl(port, S3C2410_ULCON, cfg->ulcon);
  944. /* reset both fifos */
  945. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  946. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  947. return 0;
  948. }
  949. static struct s3c24xx_uart_info s3c2400_uart_inf = {
  950. .name = "Samsung S3C2400 UART",
  951. .type = PORT_S3C2400,
  952. .fifosize = 16,
  953. .rx_fifomask = S3C2410_UFSTAT_RXMASK,
  954. .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
  955. .rx_fifofull = S3C2410_UFSTAT_RXFULL,
  956. .tx_fifofull = S3C2410_UFSTAT_TXFULL,
  957. .tx_fifomask = S3C2410_UFSTAT_TXMASK,
  958. .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
  959. .get_clksrc = s3c2400_serial_getsource,
  960. .set_clksrc = s3c2400_serial_setsource,
  961. .reset_port = s3c2400_serial_resetport,
  962. };
  963. static int s3c2400_serial_probe(struct device *dev)
  964. {
  965. return s3c24xx_serial_probe(dev, &s3c2400_uart_inf);
  966. }
  967. static struct device_driver s3c2400_serial_drv = {
  968. .name = "s3c2400-uart",
  969. .bus = &platform_bus_type,
  970. .probe = s3c2400_serial_probe,
  971. .remove = s3c24xx_serial_remove,
  972. .suspend = s3c24xx_serial_suspend,
  973. .resume = s3c24xx_serial_resume,
  974. };
  975. static inline int s3c2400_serial_init(void)
  976. {
  977. return s3c24xx_serial_init(&s3c2400_serial_drv, &s3c2400_uart_inf);
  978. }
  979. static inline void s3c2400_serial_exit(void)
  980. {
  981. driver_unregister(&s3c2400_serial_drv);
  982. }
  983. #define s3c2400_uart_inf_at &s3c2400_uart_inf
  984. #else
  985. static inline int s3c2400_serial_init(void)
  986. {
  987. return 0;
  988. }
  989. static inline void s3c2400_serial_exit(void)
  990. {
  991. }
  992. #define s3c2400_uart_inf_at NULL
  993. #endif /* CONFIG_CPU_S3C2400 */
  994. /* S3C2410 support */
  995. #ifdef CONFIG_CPU_S3C2410
  996. static int s3c2410_serial_setsource(struct uart_port *port,
  997. struct s3c24xx_uart_clksrc *clk)
  998. {
  999. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1000. if (strcmp(clk->name, "uclk") == 0)
  1001. ucon |= S3C2410_UCON_UCLK;
  1002. else
  1003. ucon &= ~S3C2410_UCON_UCLK;
  1004. wr_regl(port, S3C2410_UCON, ucon);
  1005. return 0;
  1006. }
  1007. static int s3c2410_serial_getsource(struct uart_port *port,
  1008. struct s3c24xx_uart_clksrc *clk)
  1009. {
  1010. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1011. clk->divisor = 1;
  1012. clk->name = (ucon & S3C2410_UCON_UCLK) ? "uclk" : "pclk";
  1013. return 0;
  1014. }
  1015. static int s3c2410_serial_resetport(struct uart_port *port,
  1016. struct s3c2410_uartcfg *cfg)
  1017. {
  1018. dbg("s3c2410_serial_resetport: port=%p (%08lx), cfg=%p\n",
  1019. port, port->mapbase, cfg);
  1020. wr_regl(port, S3C2410_UCON, cfg->ucon);
  1021. wr_regl(port, S3C2410_ULCON, cfg->ulcon);
  1022. /* reset both fifos */
  1023. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  1024. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  1025. return 0;
  1026. }
  1027. static struct s3c24xx_uart_info s3c2410_uart_inf = {
  1028. .name = "Samsung S3C2410 UART",
  1029. .type = PORT_S3C2410,
  1030. .fifosize = 16,
  1031. .rx_fifomask = S3C2410_UFSTAT_RXMASK,
  1032. .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
  1033. .rx_fifofull = S3C2410_UFSTAT_RXFULL,
  1034. .tx_fifofull = S3C2410_UFSTAT_TXFULL,
  1035. .tx_fifomask = S3C2410_UFSTAT_TXMASK,
  1036. .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
  1037. .get_clksrc = s3c2410_serial_getsource,
  1038. .set_clksrc = s3c2410_serial_setsource,
  1039. .reset_port = s3c2410_serial_resetport,
  1040. };
  1041. /* device management */
  1042. static int s3c2410_serial_probe(struct device *dev)
  1043. {
  1044. return s3c24xx_serial_probe(dev, &s3c2410_uart_inf);
  1045. }
  1046. static struct device_driver s3c2410_serial_drv = {
  1047. .name = "s3c2410-uart",
  1048. .bus = &platform_bus_type,
  1049. .probe = s3c2410_serial_probe,
  1050. .remove = s3c24xx_serial_remove,
  1051. .suspend = s3c24xx_serial_suspend,
  1052. .resume = s3c24xx_serial_resume,
  1053. };
  1054. static inline int s3c2410_serial_init(void)
  1055. {
  1056. return s3c24xx_serial_init(&s3c2410_serial_drv, &s3c2410_uart_inf);
  1057. }
  1058. static inline void s3c2410_serial_exit(void)
  1059. {
  1060. driver_unregister(&s3c2410_serial_drv);
  1061. }
  1062. #define s3c2410_uart_inf_at &s3c2410_uart_inf
  1063. #else
  1064. static inline int s3c2410_serial_init(void)
  1065. {
  1066. return 0;
  1067. }
  1068. static inline void s3c2410_serial_exit(void)
  1069. {
  1070. }
  1071. #define s3c2410_uart_inf_at NULL
  1072. #endif /* CONFIG_CPU_S3C2410 */
  1073. #ifdef CONFIG_CPU_S3C2440
  1074. static int s3c2440_serial_setsource(struct uart_port *port,
  1075. struct s3c24xx_uart_clksrc *clk)
  1076. {
  1077. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1078. // todo - proper fclk<>nonfclk switch //
  1079. ucon &= ~S3C2440_UCON_CLKMASK;
  1080. if (strcmp(clk->name, "uclk") == 0)
  1081. ucon |= S3C2440_UCON_UCLK;
  1082. else if (strcmp(clk->name, "pclk") == 0)
  1083. ucon |= S3C2440_UCON_PCLK;
  1084. else if (strcmp(clk->name, "fclk") == 0)
  1085. ucon |= S3C2440_UCON_FCLK;
  1086. else {
  1087. printk(KERN_ERR "unknown clock source %s\n", clk->name);
  1088. return -EINVAL;
  1089. }
  1090. wr_regl(port, S3C2410_UCON, ucon);
  1091. return 0;
  1092. }
  1093. static int s3c2440_serial_getsource(struct uart_port *port,
  1094. struct s3c24xx_uart_clksrc *clk)
  1095. {
  1096. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1097. unsigned long ucon0, ucon1, ucon2;
  1098. switch (ucon & S3C2440_UCON_CLKMASK) {
  1099. case S3C2440_UCON_UCLK:
  1100. clk->divisor = 1;
  1101. clk->name = "uclk";
  1102. break;
  1103. case S3C2440_UCON_PCLK:
  1104. case S3C2440_UCON_PCLK2:
  1105. clk->divisor = 1;
  1106. clk->name = "pclk";
  1107. break;
  1108. case S3C2440_UCON_FCLK:
  1109. /* the fun of calculating the uart divisors on
  1110. * the s3c2440 */
  1111. ucon0 = __raw_readl(S3C24XX_VA_UART0 + S3C2410_UCON);
  1112. ucon1 = __raw_readl(S3C24XX_VA_UART1 + S3C2410_UCON);
  1113. ucon2 = __raw_readl(S3C24XX_VA_UART2 + S3C2410_UCON);
  1114. printk("ucons: %08lx, %08lx, %08lx\n", ucon0, ucon1, ucon2);
  1115. ucon0 &= S3C2440_UCON0_DIVMASK;
  1116. ucon1 &= S3C2440_UCON1_DIVMASK;
  1117. ucon2 &= S3C2440_UCON2_DIVMASK;
  1118. if (ucon0 != 0) {
  1119. clk->divisor = ucon0 >> S3C2440_UCON_DIVSHIFT;
  1120. clk->divisor += 6;
  1121. } else if (ucon1 != 0) {
  1122. clk->divisor = ucon1 >> S3C2440_UCON_DIVSHIFT;
  1123. clk->divisor += 21;
  1124. } else if (ucon2 != 0) {
  1125. clk->divisor = ucon2 >> S3C2440_UCON_DIVSHIFT;
  1126. clk->divisor += 36;
  1127. } else {
  1128. /* manual calims 44, seems to be 9 */
  1129. clk->divisor = 9;
  1130. }
  1131. clk->name = "fclk";
  1132. break;
  1133. }
  1134. return 0;
  1135. }
  1136. static int s3c2440_serial_resetport(struct uart_port *port,
  1137. struct s3c2410_uartcfg *cfg)
  1138. {
  1139. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1140. dbg("s3c2440_serial_resetport: port=%p (%08lx), cfg=%p\n",
  1141. port, port->mapbase, cfg);
  1142. /* ensure we don't change the clock settings... */
  1143. ucon &= (S3C2440_UCON0_DIVMASK | (3<<10));
  1144. wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
  1145. wr_regl(port, S3C2410_ULCON, cfg->ulcon);
  1146. /* reset both fifos */
  1147. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  1148. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  1149. return 0;
  1150. }
  1151. static struct s3c24xx_uart_info s3c2440_uart_inf = {
  1152. .name = "Samsung S3C2440 UART",
  1153. .type = PORT_S3C2440,
  1154. .fifosize = 64,
  1155. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1156. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1157. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1158. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1159. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1160. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1161. .get_clksrc = s3c2440_serial_getsource,
  1162. .set_clksrc = s3c2440_serial_setsource,
  1163. .reset_port = s3c2440_serial_resetport,
  1164. };
  1165. /* device management */
  1166. static int s3c2440_serial_probe(struct device *dev)
  1167. {
  1168. dbg("s3c2440_serial_probe: dev=%p\n", dev);
  1169. return s3c24xx_serial_probe(dev, &s3c2440_uart_inf);
  1170. }
  1171. static struct device_driver s3c2440_serial_drv = {
  1172. .name = "s3c2440-uart",
  1173. .bus = &platform_bus_type,
  1174. .probe = s3c2440_serial_probe,
  1175. .remove = s3c24xx_serial_remove,
  1176. .suspend = s3c24xx_serial_suspend,
  1177. .resume = s3c24xx_serial_resume,
  1178. };
  1179. static inline int s3c2440_serial_init(void)
  1180. {
  1181. return s3c24xx_serial_init(&s3c2440_serial_drv, &s3c2440_uart_inf);
  1182. }
  1183. static inline void s3c2440_serial_exit(void)
  1184. {
  1185. driver_unregister(&s3c2440_serial_drv);
  1186. }
  1187. #define s3c2440_uart_inf_at &s3c2440_uart_inf
  1188. #else
  1189. static inline int s3c2440_serial_init(void)
  1190. {
  1191. return 0;
  1192. }
  1193. static inline void s3c2440_serial_exit(void)
  1194. {
  1195. }
  1196. #define s3c2440_uart_inf_at NULL
  1197. #endif /* CONFIG_CPU_S3C2440 */
  1198. /* module initialisation code */
  1199. static int __init s3c24xx_serial_modinit(void)
  1200. {
  1201. int ret;
  1202. ret = uart_register_driver(&s3c24xx_uart_drv);
  1203. if (ret < 0) {
  1204. printk(KERN_ERR "failed to register UART driver\n");
  1205. return -1;
  1206. }
  1207. s3c2400_serial_init();
  1208. s3c2410_serial_init();
  1209. s3c2440_serial_init();
  1210. return 0;
  1211. }
  1212. static void __exit s3c24xx_serial_modexit(void)
  1213. {
  1214. s3c2400_serial_exit();
  1215. s3c2410_serial_exit();
  1216. s3c2440_serial_exit();
  1217. uart_unregister_driver(&s3c24xx_uart_drv);
  1218. }
  1219. module_init(s3c24xx_serial_modinit);
  1220. module_exit(s3c24xx_serial_modexit);
  1221. /* Console code */
  1222. #ifdef CONFIG_SERIAL_S3C2410_CONSOLE
  1223. static struct uart_port *cons_uart;
  1224. static int
  1225. s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
  1226. {
  1227. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1228. unsigned long ufstat, utrstat;
  1229. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  1230. /* fifo mode - check ammount of data in fifo registers... */
  1231. ufstat = rd_regl(port, S3C2410_UFSTAT);
  1232. return (ufstat & info->tx_fifofull) ? 0 : 1;
  1233. }
  1234. /* in non-fifo mode, we go and use the tx buffer empty */
  1235. utrstat = rd_regl(port, S3C2410_UTRSTAT);
  1236. return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
  1237. }
  1238. static void
  1239. s3c24xx_serial_console_write(struct console *co, const char *s,
  1240. unsigned int count)
  1241. {
  1242. int i;
  1243. unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
  1244. for (i = 0; i < count; i++) {
  1245. while (!s3c24xx_serial_console_txrdy(cons_uart, ufcon))
  1246. barrier();
  1247. wr_regb(cons_uart, S3C2410_UTXH, s[i]);
  1248. if (s[i] == '\n') {
  1249. while (!s3c24xx_serial_console_txrdy(cons_uart, ufcon))
  1250. barrier();
  1251. wr_regb(cons_uart, S3C2410_UTXH, '\r');
  1252. }
  1253. }
  1254. }
  1255. static void __init
  1256. s3c24xx_serial_get_options(struct uart_port *port, int *baud,
  1257. int *parity, int *bits)
  1258. {
  1259. struct s3c24xx_uart_clksrc clksrc;
  1260. struct clk *clk;
  1261. unsigned int ulcon;
  1262. unsigned int ucon;
  1263. unsigned int ubrdiv;
  1264. unsigned long rate;
  1265. ulcon = rd_regl(port, S3C2410_ULCON);
  1266. ucon = rd_regl(port, S3C2410_UCON);
  1267. ubrdiv = rd_regl(port, S3C2410_UBRDIV);
  1268. dbg("s3c24xx_serial_get_options: port=%p\n"
  1269. "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
  1270. port, ulcon, ucon, ubrdiv);
  1271. if ((ucon & 0xf) != 0) {
  1272. /* consider the serial port configured if the tx/rx mode set */
  1273. switch (ulcon & S3C2410_LCON_CSMASK) {
  1274. case S3C2410_LCON_CS5:
  1275. *bits = 5;
  1276. break;
  1277. case S3C2410_LCON_CS6:
  1278. *bits = 6;
  1279. break;
  1280. case S3C2410_LCON_CS7:
  1281. *bits = 7;
  1282. break;
  1283. default:
  1284. case S3C2410_LCON_CS8:
  1285. *bits = 8;
  1286. break;
  1287. }
  1288. switch (ulcon & S3C2410_LCON_PMASK) {
  1289. case S3C2410_LCON_PEVEN:
  1290. *parity = 'e';
  1291. break;
  1292. case S3C2410_LCON_PODD:
  1293. *parity = 'o';
  1294. break;
  1295. case S3C2410_LCON_PNONE:
  1296. default:
  1297. *parity = 'n';
  1298. }
  1299. /* now calculate the baud rate */
  1300. s3c24xx_serial_getsource(port, &clksrc);
  1301. clk = clk_get(port->dev, clksrc.name);
  1302. if (!IS_ERR(clk) && clk != NULL)
  1303. rate = clk_get_rate(clk) / clksrc.divisor;
  1304. else
  1305. rate = 1;
  1306. *baud = rate / ( 16 * (ubrdiv + 1));
  1307. dbg("calculated baud %d\n", *baud);
  1308. }
  1309. }
  1310. /* s3c24xx_serial_init_ports
  1311. *
  1312. * initialise the serial ports from the machine provided initialisation
  1313. * data.
  1314. */
  1315. static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info *info)
  1316. {
  1317. struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
  1318. struct platform_device **platdev_ptr;
  1319. int i;
  1320. dbg("s3c24xx_serial_init_ports: initialising ports...\n");
  1321. platdev_ptr = s3c24xx_uart_devs;
  1322. for (i = 0; i < NR_PORTS; i++, ptr++, platdev_ptr++) {
  1323. s3c24xx_serial_init_port(ptr, info, *platdev_ptr);
  1324. }
  1325. return 0;
  1326. }
  1327. static int __init
  1328. s3c24xx_serial_console_setup(struct console *co, char *options)
  1329. {
  1330. struct uart_port *port;
  1331. int baud = 9600;
  1332. int bits = 8;
  1333. int parity = 'n';
  1334. int flow = 'n';
  1335. dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
  1336. co, co->index, options);
  1337. /* is this a valid port */
  1338. if (co->index == -1 || co->index >= NR_PORTS)
  1339. co->index = 0;
  1340. port = &s3c24xx_serial_ports[co->index].port;
  1341. /* is the port configured? */
  1342. if (port->mapbase == 0x0) {
  1343. co->index = 0;
  1344. port = &s3c24xx_serial_ports[co->index].port;
  1345. }
  1346. cons_uart = port;
  1347. dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
  1348. /*
  1349. * Check whether an invalid uart number has been specified, and
  1350. * if so, search for the first available port that does have
  1351. * console support.
  1352. */
  1353. if (options)
  1354. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1355. else
  1356. s3c24xx_serial_get_options(port, &baud, &parity, &bits);
  1357. dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
  1358. return uart_set_options(port, co, baud, parity, bits, flow);
  1359. }
  1360. /* s3c24xx_serial_initconsole
  1361. *
  1362. * initialise the console from one of the uart drivers
  1363. */
  1364. static struct console s3c24xx_serial_console =
  1365. {
  1366. .name = S3C24XX_SERIAL_NAME,
  1367. .device = uart_console_device,
  1368. .flags = CON_PRINTBUFFER,
  1369. .index = -1,
  1370. .write = s3c24xx_serial_console_write,
  1371. .setup = s3c24xx_serial_console_setup
  1372. };
  1373. static int s3c24xx_serial_initconsole(void)
  1374. {
  1375. struct s3c24xx_uart_info *info;
  1376. struct platform_device *dev = s3c24xx_uart_devs[0];
  1377. dbg("s3c24xx_serial_initconsole\n");
  1378. /* select driver based on the cpu */
  1379. if (dev == NULL) {
  1380. printk(KERN_ERR "s3c24xx: no devices for console init\n");
  1381. return 0;
  1382. }
  1383. if (strcmp(dev->name, "s3c2400-uart") == 0) {
  1384. info = s3c2400_uart_inf_at;
  1385. } else if (strcmp(dev->name, "s3c2410-uart") == 0) {
  1386. info = s3c2410_uart_inf_at;
  1387. } else if (strcmp(dev->name, "s3c2440-uart") == 0) {
  1388. info = s3c2440_uart_inf_at;
  1389. } else {
  1390. printk(KERN_ERR "s3c24xx: no driver for %s\n", dev->name);
  1391. return 0;
  1392. }
  1393. if (info == NULL) {
  1394. printk(KERN_ERR "s3c24xx: no driver for console\n");
  1395. return 0;
  1396. }
  1397. s3c24xx_serial_console.data = &s3c24xx_uart_drv;
  1398. s3c24xx_serial_init_ports(info);
  1399. register_console(&s3c24xx_serial_console);
  1400. return 0;
  1401. }
  1402. console_initcall(s3c24xx_serial_initconsole);
  1403. #endif /* CONFIG_SERIAL_S3C2410_CONSOLE */
  1404. MODULE_LICENSE("GPL");
  1405. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  1406. MODULE_DESCRIPTION("Samsung S3C2410/S3C2440 Serial port driver");