s3c2410.c 42 KB

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