s3c2410.c 44 KB

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