s3c2410.c 43 KB

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