s3c2410.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944
  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. int ret;
  809. dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
  810. if (platdev == NULL)
  811. return -ENODEV;
  812. cfg = s3c24xx_dev_to_cfg(&platdev->dev);
  813. if (port->mapbase != 0)
  814. return 0;
  815. if (cfg->hwport > 3)
  816. return -EINVAL;
  817. /* setup info for port */
  818. port->dev = &platdev->dev;
  819. ourport->info = info;
  820. /* copy the info in from provided structure */
  821. ourport->port.fifosize = info->fifosize;
  822. dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
  823. port->uartclk = 1;
  824. if (cfg->uart_flags & UPF_CONS_FLOW) {
  825. dbg("s3c24xx_serial_init_port: enabling flow control\n");
  826. port->flags |= UPF_CONS_FLOW;
  827. }
  828. /* sort our the physical and virtual addresses for each UART */
  829. res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
  830. if (res == NULL) {
  831. printk(KERN_ERR "failed to find memory resource for uart\n");
  832. return -EINVAL;
  833. }
  834. dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
  835. port->mapbase = res->start;
  836. port->membase = S3C24XX_VA_UART + (res->start - S3C24XX_PA_UART);
  837. ret = platform_get_irq(platdev, 0);
  838. if (ret < 0)
  839. port->irq = 0;
  840. else
  841. port->irq = ret;
  842. ourport->clk = clk_get(&platdev->dev, "uart");
  843. dbg("port: map=%08x, mem=%08x, irq=%d, clock=%ld\n",
  844. port->mapbase, port->membase, port->irq, port->uartclk);
  845. /* reset the fifos (and setup the uart) */
  846. s3c24xx_serial_resetport(port, cfg);
  847. return 0;
  848. }
  849. /* Device driver serial port probe */
  850. static int probe_index = 0;
  851. static int s3c24xx_serial_probe(struct platform_device *dev,
  852. struct s3c24xx_uart_info *info)
  853. {
  854. struct s3c24xx_uart_port *ourport;
  855. int ret;
  856. dbg("s3c24xx_serial_probe(%p, %p) %d\n", dev, info, probe_index);
  857. ourport = &s3c24xx_serial_ports[probe_index];
  858. probe_index++;
  859. dbg("%s: initialising port %p...\n", __func__, ourport);
  860. ret = s3c24xx_serial_init_port(ourport, info, dev);
  861. if (ret < 0)
  862. goto probe_err;
  863. dbg("%s: adding port\n", __func__);
  864. uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
  865. platform_set_drvdata(dev, &ourport->port);
  866. return 0;
  867. probe_err:
  868. return ret;
  869. }
  870. static int s3c24xx_serial_remove(struct platform_device *dev)
  871. {
  872. struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
  873. if (port)
  874. uart_remove_one_port(&s3c24xx_uart_drv, port);
  875. return 0;
  876. }
  877. /* UART power management code */
  878. #ifdef CONFIG_PM
  879. static int s3c24xx_serial_suspend(struct platform_device *dev, pm_message_t state)
  880. {
  881. struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
  882. if (port)
  883. uart_suspend_port(&s3c24xx_uart_drv, port);
  884. return 0;
  885. }
  886. static int s3c24xx_serial_resume(struct platform_device *dev)
  887. {
  888. struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
  889. struct s3c24xx_uart_port *ourport = to_ourport(port);
  890. if (port) {
  891. clk_enable(ourport->clk);
  892. s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
  893. clk_disable(ourport->clk);
  894. uart_resume_port(&s3c24xx_uart_drv, port);
  895. }
  896. return 0;
  897. }
  898. #else
  899. #define s3c24xx_serial_suspend NULL
  900. #define s3c24xx_serial_resume NULL
  901. #endif
  902. static int s3c24xx_serial_init(struct platform_driver *drv,
  903. struct s3c24xx_uart_info *info)
  904. {
  905. dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
  906. return platform_driver_register(drv);
  907. }
  908. /* now comes the code to initialise either the s3c2410 or s3c2440 serial
  909. * port information
  910. */
  911. /* cpu specific variations on the serial port support */
  912. #ifdef CONFIG_CPU_S3C2400
  913. static int s3c2400_serial_getsource(struct uart_port *port,
  914. struct s3c24xx_uart_clksrc *clk)
  915. {
  916. clk->divisor = 1;
  917. clk->name = "pclk";
  918. return 0;
  919. }
  920. static int s3c2400_serial_setsource(struct uart_port *port,
  921. struct s3c24xx_uart_clksrc *clk)
  922. {
  923. return 0;
  924. }
  925. static int s3c2400_serial_resetport(struct uart_port *port,
  926. struct s3c2410_uartcfg *cfg)
  927. {
  928. dbg("s3c2400_serial_resetport: port=%p (%08lx), cfg=%p\n",
  929. port, port->mapbase, cfg);
  930. wr_regl(port, S3C2410_UCON, cfg->ucon);
  931. wr_regl(port, S3C2410_ULCON, cfg->ulcon);
  932. /* reset both fifos */
  933. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  934. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  935. return 0;
  936. }
  937. static struct s3c24xx_uart_info s3c2400_uart_inf = {
  938. .name = "Samsung S3C2400 UART",
  939. .type = PORT_S3C2400,
  940. .fifosize = 16,
  941. .rx_fifomask = S3C2410_UFSTAT_RXMASK,
  942. .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
  943. .rx_fifofull = S3C2410_UFSTAT_RXFULL,
  944. .tx_fifofull = S3C2410_UFSTAT_TXFULL,
  945. .tx_fifomask = S3C2410_UFSTAT_TXMASK,
  946. .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
  947. .get_clksrc = s3c2400_serial_getsource,
  948. .set_clksrc = s3c2400_serial_setsource,
  949. .reset_port = s3c2400_serial_resetport,
  950. };
  951. static int s3c2400_serial_probe(struct platform_device *dev)
  952. {
  953. return s3c24xx_serial_probe(dev, &s3c2400_uart_inf);
  954. }
  955. static struct platform_driver s3c2400_serial_drv = {
  956. .probe = s3c2400_serial_probe,
  957. .remove = s3c24xx_serial_remove,
  958. .suspend = s3c24xx_serial_suspend,
  959. .resume = s3c24xx_serial_resume,
  960. .driver = {
  961. .name = "s3c2400-uart",
  962. .owner = THIS_MODULE,
  963. },
  964. };
  965. static inline int s3c2400_serial_init(void)
  966. {
  967. return s3c24xx_serial_init(&s3c2400_serial_drv, &s3c2400_uart_inf);
  968. }
  969. static inline void s3c2400_serial_exit(void)
  970. {
  971. platform_driver_unregister(&s3c2400_serial_drv);
  972. }
  973. #define s3c2400_uart_inf_at &s3c2400_uart_inf
  974. #else
  975. static inline int s3c2400_serial_init(void)
  976. {
  977. return 0;
  978. }
  979. static inline void s3c2400_serial_exit(void)
  980. {
  981. }
  982. #define s3c2400_uart_inf_at NULL
  983. #endif /* CONFIG_CPU_S3C2400 */
  984. /* S3C2410 support */
  985. #ifdef CONFIG_CPU_S3C2410
  986. static int s3c2410_serial_setsource(struct uart_port *port,
  987. struct s3c24xx_uart_clksrc *clk)
  988. {
  989. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  990. if (strcmp(clk->name, "uclk") == 0)
  991. ucon |= S3C2410_UCON_UCLK;
  992. else
  993. ucon &= ~S3C2410_UCON_UCLK;
  994. wr_regl(port, S3C2410_UCON, ucon);
  995. return 0;
  996. }
  997. static int s3c2410_serial_getsource(struct uart_port *port,
  998. struct s3c24xx_uart_clksrc *clk)
  999. {
  1000. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1001. clk->divisor = 1;
  1002. clk->name = (ucon & S3C2410_UCON_UCLK) ? "uclk" : "pclk";
  1003. return 0;
  1004. }
  1005. static int s3c2410_serial_resetport(struct uart_port *port,
  1006. struct s3c2410_uartcfg *cfg)
  1007. {
  1008. dbg("s3c2410_serial_resetport: port=%p (%08lx), cfg=%p\n",
  1009. port, port->mapbase, cfg);
  1010. wr_regl(port, S3C2410_UCON, cfg->ucon);
  1011. wr_regl(port, S3C2410_ULCON, cfg->ulcon);
  1012. /* reset both fifos */
  1013. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  1014. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  1015. return 0;
  1016. }
  1017. static struct s3c24xx_uart_info s3c2410_uart_inf = {
  1018. .name = "Samsung S3C2410 UART",
  1019. .type = PORT_S3C2410,
  1020. .fifosize = 16,
  1021. .rx_fifomask = S3C2410_UFSTAT_RXMASK,
  1022. .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
  1023. .rx_fifofull = S3C2410_UFSTAT_RXFULL,
  1024. .tx_fifofull = S3C2410_UFSTAT_TXFULL,
  1025. .tx_fifomask = S3C2410_UFSTAT_TXMASK,
  1026. .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
  1027. .get_clksrc = s3c2410_serial_getsource,
  1028. .set_clksrc = s3c2410_serial_setsource,
  1029. .reset_port = s3c2410_serial_resetport,
  1030. };
  1031. /* device management */
  1032. static int s3c2410_serial_probe(struct platform_device *dev)
  1033. {
  1034. return s3c24xx_serial_probe(dev, &s3c2410_uart_inf);
  1035. }
  1036. static struct platform_driver s3c2410_serial_drv = {
  1037. .probe = s3c2410_serial_probe,
  1038. .remove = s3c24xx_serial_remove,
  1039. .suspend = s3c24xx_serial_suspend,
  1040. .resume = s3c24xx_serial_resume,
  1041. .driver = {
  1042. .name = "s3c2410-uart",
  1043. .owner = THIS_MODULE,
  1044. },
  1045. };
  1046. static inline int s3c2410_serial_init(void)
  1047. {
  1048. return s3c24xx_serial_init(&s3c2410_serial_drv, &s3c2410_uart_inf);
  1049. }
  1050. static inline void s3c2410_serial_exit(void)
  1051. {
  1052. platform_driver_unregister(&s3c2410_serial_drv);
  1053. }
  1054. #define s3c2410_uart_inf_at &s3c2410_uart_inf
  1055. #else
  1056. static inline int s3c2410_serial_init(void)
  1057. {
  1058. return 0;
  1059. }
  1060. static inline void s3c2410_serial_exit(void)
  1061. {
  1062. }
  1063. #define s3c2410_uart_inf_at NULL
  1064. #endif /* CONFIG_CPU_S3C2410 */
  1065. #if defined(CONFIG_CPU_S3C2440) || defined(CONFIG_CPU_S3C2442)
  1066. static int s3c2440_serial_setsource(struct uart_port *port,
  1067. struct s3c24xx_uart_clksrc *clk)
  1068. {
  1069. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1070. // todo - proper fclk<>nonfclk switch //
  1071. ucon &= ~S3C2440_UCON_CLKMASK;
  1072. if (strcmp(clk->name, "uclk") == 0)
  1073. ucon |= S3C2440_UCON_UCLK;
  1074. else if (strcmp(clk->name, "pclk") == 0)
  1075. ucon |= S3C2440_UCON_PCLK;
  1076. else if (strcmp(clk->name, "fclk") == 0)
  1077. ucon |= S3C2440_UCON_FCLK;
  1078. else {
  1079. printk(KERN_ERR "unknown clock source %s\n", clk->name);
  1080. return -EINVAL;
  1081. }
  1082. wr_regl(port, S3C2410_UCON, ucon);
  1083. return 0;
  1084. }
  1085. static int s3c2440_serial_getsource(struct uart_port *port,
  1086. struct s3c24xx_uart_clksrc *clk)
  1087. {
  1088. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1089. unsigned long ucon0, ucon1, ucon2;
  1090. switch (ucon & S3C2440_UCON_CLKMASK) {
  1091. case S3C2440_UCON_UCLK:
  1092. clk->divisor = 1;
  1093. clk->name = "uclk";
  1094. break;
  1095. case S3C2440_UCON_PCLK:
  1096. case S3C2440_UCON_PCLK2:
  1097. clk->divisor = 1;
  1098. clk->name = "pclk";
  1099. break;
  1100. case S3C2440_UCON_FCLK:
  1101. /* the fun of calculating the uart divisors on
  1102. * the s3c2440 */
  1103. ucon0 = __raw_readl(S3C24XX_VA_UART0 + S3C2410_UCON);
  1104. ucon1 = __raw_readl(S3C24XX_VA_UART1 + S3C2410_UCON);
  1105. ucon2 = __raw_readl(S3C24XX_VA_UART2 + S3C2410_UCON);
  1106. printk("ucons: %08lx, %08lx, %08lx\n", ucon0, ucon1, ucon2);
  1107. ucon0 &= S3C2440_UCON0_DIVMASK;
  1108. ucon1 &= S3C2440_UCON1_DIVMASK;
  1109. ucon2 &= S3C2440_UCON2_DIVMASK;
  1110. if (ucon0 != 0) {
  1111. clk->divisor = ucon0 >> S3C2440_UCON_DIVSHIFT;
  1112. clk->divisor += 6;
  1113. } else if (ucon1 != 0) {
  1114. clk->divisor = ucon1 >> S3C2440_UCON_DIVSHIFT;
  1115. clk->divisor += 21;
  1116. } else if (ucon2 != 0) {
  1117. clk->divisor = ucon2 >> S3C2440_UCON_DIVSHIFT;
  1118. clk->divisor += 36;
  1119. } else {
  1120. /* manual calims 44, seems to be 9 */
  1121. clk->divisor = 9;
  1122. }
  1123. clk->name = "fclk";
  1124. break;
  1125. }
  1126. return 0;
  1127. }
  1128. static int s3c2440_serial_resetport(struct uart_port *port,
  1129. struct s3c2410_uartcfg *cfg)
  1130. {
  1131. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1132. dbg("s3c2440_serial_resetport: port=%p (%08lx), cfg=%p\n",
  1133. port, port->mapbase, cfg);
  1134. /* ensure we don't change the clock settings... */
  1135. ucon &= (S3C2440_UCON0_DIVMASK | (3<<10));
  1136. wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
  1137. wr_regl(port, S3C2410_ULCON, cfg->ulcon);
  1138. /* reset both fifos */
  1139. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  1140. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  1141. return 0;
  1142. }
  1143. static struct s3c24xx_uart_info s3c2440_uart_inf = {
  1144. .name = "Samsung S3C2440 UART",
  1145. .type = PORT_S3C2440,
  1146. .fifosize = 64,
  1147. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1148. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1149. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1150. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1151. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1152. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1153. .get_clksrc = s3c2440_serial_getsource,
  1154. .set_clksrc = s3c2440_serial_setsource,
  1155. .reset_port = s3c2440_serial_resetport,
  1156. };
  1157. /* device management */
  1158. static int s3c2440_serial_probe(struct platform_device *dev)
  1159. {
  1160. dbg("s3c2440_serial_probe: dev=%p\n", dev);
  1161. return s3c24xx_serial_probe(dev, &s3c2440_uart_inf);
  1162. }
  1163. static struct platform_driver s3c2440_serial_drv = {
  1164. .probe = s3c2440_serial_probe,
  1165. .remove = s3c24xx_serial_remove,
  1166. .suspend = s3c24xx_serial_suspend,
  1167. .resume = s3c24xx_serial_resume,
  1168. .driver = {
  1169. .name = "s3c2440-uart",
  1170. .owner = THIS_MODULE,
  1171. },
  1172. };
  1173. static inline int s3c2440_serial_init(void)
  1174. {
  1175. return s3c24xx_serial_init(&s3c2440_serial_drv, &s3c2440_uart_inf);
  1176. }
  1177. static inline void s3c2440_serial_exit(void)
  1178. {
  1179. platform_driver_unregister(&s3c2440_serial_drv);
  1180. }
  1181. #define s3c2440_uart_inf_at &s3c2440_uart_inf
  1182. #else
  1183. static inline int s3c2440_serial_init(void)
  1184. {
  1185. return 0;
  1186. }
  1187. static inline void s3c2440_serial_exit(void)
  1188. {
  1189. }
  1190. #define s3c2440_uart_inf_at NULL
  1191. #endif /* CONFIG_CPU_S3C2440 */
  1192. #if defined(CONFIG_CPU_S3C2412)
  1193. static int s3c2412_serial_setsource(struct uart_port *port,
  1194. struct s3c24xx_uart_clksrc *clk)
  1195. {
  1196. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1197. ucon &= ~S3C2412_UCON_CLKMASK;
  1198. if (strcmp(clk->name, "uclk") == 0)
  1199. ucon |= S3C2440_UCON_UCLK;
  1200. else if (strcmp(clk->name, "pclk") == 0)
  1201. ucon |= S3C2440_UCON_PCLK;
  1202. else if (strcmp(clk->name, "usysclk") == 0)
  1203. ucon |= S3C2412_UCON_USYSCLK;
  1204. else {
  1205. printk(KERN_ERR "unknown clock source %s\n", clk->name);
  1206. return -EINVAL;
  1207. }
  1208. wr_regl(port, S3C2410_UCON, ucon);
  1209. return 0;
  1210. }
  1211. static int s3c2412_serial_getsource(struct uart_port *port,
  1212. struct s3c24xx_uart_clksrc *clk)
  1213. {
  1214. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1215. switch (ucon & S3C2412_UCON_CLKMASK) {
  1216. case S3C2412_UCON_UCLK:
  1217. clk->divisor = 1;
  1218. clk->name = "uclk";
  1219. break;
  1220. case S3C2412_UCON_PCLK:
  1221. case S3C2412_UCON_PCLK2:
  1222. clk->divisor = 1;
  1223. clk->name = "pclk";
  1224. break;
  1225. case S3C2412_UCON_USYSCLK:
  1226. clk->divisor = 1;
  1227. clk->name = "usysclk";
  1228. break;
  1229. }
  1230. return 0;
  1231. }
  1232. static int s3c2412_serial_resetport(struct uart_port *port,
  1233. struct s3c2410_uartcfg *cfg)
  1234. {
  1235. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1236. dbg("%s: port=%p (%08lx), cfg=%p\n",
  1237. __func__, port, port->mapbase, cfg);
  1238. /* ensure we don't change the clock settings... */
  1239. ucon &= S3C2412_UCON_CLKMASK;
  1240. wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
  1241. wr_regl(port, S3C2410_ULCON, cfg->ulcon);
  1242. /* reset both fifos */
  1243. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  1244. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  1245. return 0;
  1246. }
  1247. static struct s3c24xx_uart_info s3c2412_uart_inf = {
  1248. .name = "Samsung S3C2412 UART",
  1249. .type = PORT_S3C2412,
  1250. .fifosize = 64,
  1251. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1252. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1253. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1254. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1255. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1256. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1257. .get_clksrc = s3c2412_serial_getsource,
  1258. .set_clksrc = s3c2412_serial_setsource,
  1259. .reset_port = s3c2412_serial_resetport,
  1260. };
  1261. /* device management */
  1262. static int s3c2412_serial_probe(struct platform_device *dev)
  1263. {
  1264. dbg("s3c2440_serial_probe: dev=%p\n", dev);
  1265. return s3c24xx_serial_probe(dev, &s3c2412_uart_inf);
  1266. }
  1267. static struct platform_driver s3c2412_serial_drv = {
  1268. .probe = s3c2412_serial_probe,
  1269. .remove = s3c24xx_serial_remove,
  1270. .suspend = s3c24xx_serial_suspend,
  1271. .resume = s3c24xx_serial_resume,
  1272. .driver = {
  1273. .name = "s3c2412-uart",
  1274. .owner = THIS_MODULE,
  1275. },
  1276. };
  1277. static inline int s3c2412_serial_init(void)
  1278. {
  1279. return s3c24xx_serial_init(&s3c2412_serial_drv, &s3c2412_uart_inf);
  1280. }
  1281. static inline void s3c2412_serial_exit(void)
  1282. {
  1283. platform_driver_unregister(&s3c2412_serial_drv);
  1284. }
  1285. #define s3c2412_uart_inf_at &s3c2412_uart_inf
  1286. #else
  1287. static inline int s3c2412_serial_init(void)
  1288. {
  1289. return 0;
  1290. }
  1291. static inline void s3c2412_serial_exit(void)
  1292. {
  1293. }
  1294. #define s3c2412_uart_inf_at NULL
  1295. #endif /* CONFIG_CPU_S3C2440 */
  1296. /* module initialisation code */
  1297. static int __init s3c24xx_serial_modinit(void)
  1298. {
  1299. int ret;
  1300. ret = uart_register_driver(&s3c24xx_uart_drv);
  1301. if (ret < 0) {
  1302. printk(KERN_ERR "failed to register UART driver\n");
  1303. return -1;
  1304. }
  1305. s3c2400_serial_init();
  1306. s3c2410_serial_init();
  1307. s3c2412_serial_init();
  1308. s3c2440_serial_init();
  1309. return 0;
  1310. }
  1311. static void __exit s3c24xx_serial_modexit(void)
  1312. {
  1313. s3c2400_serial_exit();
  1314. s3c2410_serial_exit();
  1315. s3c2412_serial_exit();
  1316. s3c2440_serial_exit();
  1317. uart_unregister_driver(&s3c24xx_uart_drv);
  1318. }
  1319. module_init(s3c24xx_serial_modinit);
  1320. module_exit(s3c24xx_serial_modexit);
  1321. /* Console code */
  1322. #ifdef CONFIG_SERIAL_S3C2410_CONSOLE
  1323. static struct uart_port *cons_uart;
  1324. static int
  1325. s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
  1326. {
  1327. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1328. unsigned long ufstat, utrstat;
  1329. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  1330. /* fifo mode - check ammount of data in fifo registers... */
  1331. ufstat = rd_regl(port, S3C2410_UFSTAT);
  1332. return (ufstat & info->tx_fifofull) ? 0 : 1;
  1333. }
  1334. /* in non-fifo mode, we go and use the tx buffer empty */
  1335. utrstat = rd_regl(port, S3C2410_UTRSTAT);
  1336. return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
  1337. }
  1338. static void
  1339. s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
  1340. {
  1341. unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
  1342. while (!s3c24xx_serial_console_txrdy(port, ufcon))
  1343. barrier();
  1344. wr_regb(cons_uart, S3C2410_UTXH, ch);
  1345. }
  1346. static void
  1347. s3c24xx_serial_console_write(struct console *co, const char *s,
  1348. unsigned int count)
  1349. {
  1350. uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
  1351. }
  1352. static void __init
  1353. s3c24xx_serial_get_options(struct uart_port *port, int *baud,
  1354. int *parity, int *bits)
  1355. {
  1356. struct s3c24xx_uart_clksrc clksrc;
  1357. struct clk *clk;
  1358. unsigned int ulcon;
  1359. unsigned int ucon;
  1360. unsigned int ubrdiv;
  1361. unsigned long rate;
  1362. ulcon = rd_regl(port, S3C2410_ULCON);
  1363. ucon = rd_regl(port, S3C2410_UCON);
  1364. ubrdiv = rd_regl(port, S3C2410_UBRDIV);
  1365. dbg("s3c24xx_serial_get_options: port=%p\n"
  1366. "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
  1367. port, ulcon, ucon, ubrdiv);
  1368. if ((ucon & 0xf) != 0) {
  1369. /* consider the serial port configured if the tx/rx mode set */
  1370. switch (ulcon & S3C2410_LCON_CSMASK) {
  1371. case S3C2410_LCON_CS5:
  1372. *bits = 5;
  1373. break;
  1374. case S3C2410_LCON_CS6:
  1375. *bits = 6;
  1376. break;
  1377. case S3C2410_LCON_CS7:
  1378. *bits = 7;
  1379. break;
  1380. default:
  1381. case S3C2410_LCON_CS8:
  1382. *bits = 8;
  1383. break;
  1384. }
  1385. switch (ulcon & S3C2410_LCON_PMASK) {
  1386. case S3C2410_LCON_PEVEN:
  1387. *parity = 'e';
  1388. break;
  1389. case S3C2410_LCON_PODD:
  1390. *parity = 'o';
  1391. break;
  1392. case S3C2410_LCON_PNONE:
  1393. default:
  1394. *parity = 'n';
  1395. }
  1396. /* now calculate the baud rate */
  1397. s3c24xx_serial_getsource(port, &clksrc);
  1398. clk = clk_get(port->dev, clksrc.name);
  1399. if (!IS_ERR(clk) && clk != NULL)
  1400. rate = clk_get_rate(clk) / clksrc.divisor;
  1401. else
  1402. rate = 1;
  1403. *baud = rate / ( 16 * (ubrdiv + 1));
  1404. dbg("calculated baud %d\n", *baud);
  1405. }
  1406. }
  1407. /* s3c24xx_serial_init_ports
  1408. *
  1409. * initialise the serial ports from the machine provided initialisation
  1410. * data.
  1411. */
  1412. static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info *info)
  1413. {
  1414. struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
  1415. struct platform_device **platdev_ptr;
  1416. int i;
  1417. dbg("s3c24xx_serial_init_ports: initialising ports...\n");
  1418. platdev_ptr = s3c24xx_uart_devs;
  1419. for (i = 0; i < NR_PORTS; i++, ptr++, platdev_ptr++) {
  1420. s3c24xx_serial_init_port(ptr, info, *platdev_ptr);
  1421. }
  1422. return 0;
  1423. }
  1424. static int __init
  1425. s3c24xx_serial_console_setup(struct console *co, char *options)
  1426. {
  1427. struct uart_port *port;
  1428. int baud = 9600;
  1429. int bits = 8;
  1430. int parity = 'n';
  1431. int flow = 'n';
  1432. dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
  1433. co, co->index, options);
  1434. /* is this a valid port */
  1435. if (co->index == -1 || co->index >= NR_PORTS)
  1436. co->index = 0;
  1437. port = &s3c24xx_serial_ports[co->index].port;
  1438. /* is the port configured? */
  1439. if (port->mapbase == 0x0) {
  1440. co->index = 0;
  1441. port = &s3c24xx_serial_ports[co->index].port;
  1442. }
  1443. cons_uart = port;
  1444. dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
  1445. /*
  1446. * Check whether an invalid uart number has been specified, and
  1447. * if so, search for the first available port that does have
  1448. * console support.
  1449. */
  1450. if (options)
  1451. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1452. else
  1453. s3c24xx_serial_get_options(port, &baud, &parity, &bits);
  1454. dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
  1455. return uart_set_options(port, co, baud, parity, bits, flow);
  1456. }
  1457. /* s3c24xx_serial_initconsole
  1458. *
  1459. * initialise the console from one of the uart drivers
  1460. */
  1461. static struct console s3c24xx_serial_console =
  1462. {
  1463. .name = S3C24XX_SERIAL_NAME,
  1464. .device = uart_console_device,
  1465. .flags = CON_PRINTBUFFER,
  1466. .index = -1,
  1467. .write = s3c24xx_serial_console_write,
  1468. .setup = s3c24xx_serial_console_setup
  1469. };
  1470. static int s3c24xx_serial_initconsole(void)
  1471. {
  1472. struct s3c24xx_uart_info *info;
  1473. struct platform_device *dev = s3c24xx_uart_devs[0];
  1474. dbg("s3c24xx_serial_initconsole\n");
  1475. /* select driver based on the cpu */
  1476. if (dev == NULL) {
  1477. printk(KERN_ERR "s3c24xx: no devices for console init\n");
  1478. return 0;
  1479. }
  1480. if (strcmp(dev->name, "s3c2400-uart") == 0) {
  1481. info = s3c2400_uart_inf_at;
  1482. } else if (strcmp(dev->name, "s3c2410-uart") == 0) {
  1483. info = s3c2410_uart_inf_at;
  1484. } else if (strcmp(dev->name, "s3c2440-uart") == 0) {
  1485. info = s3c2440_uart_inf_at;
  1486. } else if (strcmp(dev->name, "s3c2412-uart") == 0) {
  1487. info = s3c2412_uart_inf_at;
  1488. } else {
  1489. printk(KERN_ERR "s3c24xx: no driver for %s\n", dev->name);
  1490. return 0;
  1491. }
  1492. if (info == NULL) {
  1493. printk(KERN_ERR "s3c24xx: no driver for console\n");
  1494. return 0;
  1495. }
  1496. s3c24xx_serial_console.data = &s3c24xx_uart_drv;
  1497. s3c24xx_serial_init_ports(info);
  1498. register_console(&s3c24xx_serial_console);
  1499. return 0;
  1500. }
  1501. console_initcall(s3c24xx_serial_initconsole);
  1502. #endif /* CONFIG_SERIAL_S3C2410_CONSOLE */
  1503. MODULE_LICENSE("GPL");
  1504. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  1505. MODULE_DESCRIPTION("Samsung S3C2410/S3C2440/S3C2412 Serial port driver");
  1506. MODULE_ALIAS("platform:s3c2400-uart");
  1507. MODULE_ALIAS("platform:s3c2410-uart");
  1508. MODULE_ALIAS("platform:s3c2412-uart");
  1509. MODULE_ALIAS("platform:s3c2440-uart");