s3c2410.c 41 KB

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