s3c2410.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  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 const 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. uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN, ch, flag);
  311. ignore_char:
  312. continue;
  313. }
  314. tty_flip_buffer_push(tty);
  315. out:
  316. return IRQ_HANDLED;
  317. }
  318. static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id, struct pt_regs *regs)
  319. {
  320. struct s3c24xx_uart_port *ourport = id;
  321. struct uart_port *port = &ourport->port;
  322. struct circ_buf *xmit = &port->info->xmit;
  323. int count = 256;
  324. if (port->x_char) {
  325. wr_regb(port, S3C2410_UTXH, port->x_char);
  326. port->icount.tx++;
  327. port->x_char = 0;
  328. goto out;
  329. }
  330. /* if there isnt anything more to transmit, or the uart is now
  331. * stopped, disable the uart and exit
  332. */
  333. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  334. s3c24xx_serial_stop_tx(port, 0);
  335. goto out;
  336. }
  337. /* try and drain the buffer... */
  338. while (!uart_circ_empty(xmit) && count-- > 0) {
  339. if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
  340. break;
  341. wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
  342. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  343. port->icount.tx++;
  344. }
  345. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  346. uart_write_wakeup(port);
  347. if (uart_circ_empty(xmit))
  348. s3c24xx_serial_stop_tx(port, 0);
  349. out:
  350. return IRQ_HANDLED;
  351. }
  352. static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
  353. {
  354. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  355. unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
  356. unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
  357. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  358. if ((ufstat & info->tx_fifomask) != 0 ||
  359. (ufstat & info->tx_fifofull))
  360. return 0;
  361. return 1;
  362. }
  363. return s3c24xx_serial_txempty_nofifo(port);
  364. }
  365. /* no modem control lines */
  366. static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
  367. {
  368. unsigned int umstat = rd_regb(port,S3C2410_UMSTAT);
  369. if (umstat & S3C2410_UMSTAT_CTS)
  370. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  371. else
  372. return TIOCM_CAR | TIOCM_DSR;
  373. }
  374. static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  375. {
  376. /* todo - possibly remove AFC and do manual CTS */
  377. }
  378. static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
  379. {
  380. unsigned long flags;
  381. unsigned int ucon;
  382. spin_lock_irqsave(&port->lock, flags);
  383. ucon = rd_regl(port, S3C2410_UCON);
  384. if (break_state)
  385. ucon |= S3C2410_UCON_SBREAK;
  386. else
  387. ucon &= ~S3C2410_UCON_SBREAK;
  388. wr_regl(port, S3C2410_UCON, ucon);
  389. spin_unlock_irqrestore(&port->lock, flags);
  390. }
  391. static void s3c24xx_serial_shutdown(struct uart_port *port)
  392. {
  393. struct s3c24xx_uart_port *ourport = to_ourport(port);
  394. if (ourport->tx_claimed) {
  395. free_irq(TX_IRQ(port), ourport);
  396. tx_enabled(port) = 0;
  397. ourport->tx_claimed = 0;
  398. }
  399. if (ourport->rx_claimed) {
  400. free_irq(RX_IRQ(port), ourport);
  401. ourport->rx_claimed = 0;
  402. rx_enabled(port) = 0;
  403. }
  404. }
  405. static int s3c24xx_serial_startup(struct uart_port *port)
  406. {
  407. struct s3c24xx_uart_port *ourport = to_ourport(port);
  408. int ret;
  409. dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
  410. port->mapbase, port->membase);
  411. rx_enabled(port) = 1;
  412. ret = request_irq(RX_IRQ(port),
  413. s3c24xx_serial_rx_chars, 0,
  414. s3c24xx_serial_portname(port), ourport);
  415. if (ret != 0) {
  416. printk(KERN_ERR "cannot get irq %d\n", RX_IRQ(port));
  417. return ret;
  418. }
  419. ourport->rx_claimed = 1;
  420. dbg("requesting tx irq...\n");
  421. tx_enabled(port) = 1;
  422. ret = request_irq(TX_IRQ(port),
  423. s3c24xx_serial_tx_chars, 0,
  424. s3c24xx_serial_portname(port), ourport);
  425. if (ret) {
  426. printk(KERN_ERR "cannot get irq %d\n", TX_IRQ(port));
  427. goto err;
  428. }
  429. ourport->tx_claimed = 1;
  430. dbg("s3c24xx_serial_startup ok\n");
  431. /* the port reset code should have done the correct
  432. * register setup for the port controls */
  433. return ret;
  434. err:
  435. s3c24xx_serial_shutdown(port);
  436. return ret;
  437. }
  438. /* power power management control */
  439. static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
  440. unsigned int old)
  441. {
  442. struct s3c24xx_uart_port *ourport = to_ourport(port);
  443. switch (level) {
  444. case 3:
  445. if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
  446. clk_disable(ourport->baudclk);
  447. clk_disable(ourport->clk);
  448. break;
  449. case 0:
  450. clk_enable(ourport->clk);
  451. if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
  452. clk_enable(ourport->baudclk);
  453. break;
  454. default:
  455. printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
  456. }
  457. }
  458. /* baud rate calculation
  459. *
  460. * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
  461. * of different sources, including the peripheral clock ("pclk") and an
  462. * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
  463. * with a programmable extra divisor.
  464. *
  465. * The following code goes through the clock sources, and calculates the
  466. * baud clocks (and the resultant actual baud rates) and then tries to
  467. * pick the closest one and select that.
  468. *
  469. */
  470. #define MAX_CLKS (8)
  471. static struct s3c24xx_uart_clksrc tmp_clksrc = {
  472. .name = "pclk",
  473. .min_baud = 0,
  474. .max_baud = 0,
  475. .divisor = 1,
  476. };
  477. static inline int
  478. s3c24xx_serial_getsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
  479. {
  480. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  481. return (info->get_clksrc)(port, c);
  482. }
  483. static inline int
  484. s3c24xx_serial_setsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
  485. {
  486. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  487. return (info->set_clksrc)(port, c);
  488. }
  489. struct baud_calc {
  490. struct s3c24xx_uart_clksrc *clksrc;
  491. unsigned int calc;
  492. unsigned int quot;
  493. struct clk *src;
  494. };
  495. static int s3c24xx_serial_calcbaud(struct baud_calc *calc,
  496. struct uart_port *port,
  497. struct s3c24xx_uart_clksrc *clksrc,
  498. unsigned int baud)
  499. {
  500. unsigned long rate;
  501. calc->src = clk_get(port->dev, clksrc->name);
  502. if (calc->src == NULL || IS_ERR(calc->src))
  503. return 0;
  504. rate = clk_get_rate(calc->src);
  505. rate /= clksrc->divisor;
  506. calc->clksrc = clksrc;
  507. calc->quot = (rate + (8 * baud)) / (16 * baud);
  508. calc->calc = (rate / (calc->quot * 16));
  509. calc->quot--;
  510. return 1;
  511. }
  512. static unsigned int s3c24xx_serial_getclk(struct uart_port *port,
  513. struct s3c24xx_uart_clksrc **clksrc,
  514. struct clk **clk,
  515. unsigned int baud)
  516. {
  517. struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
  518. struct s3c24xx_uart_clksrc *clkp;
  519. struct baud_calc res[MAX_CLKS];
  520. struct baud_calc *resptr, *best, *sptr;
  521. int i;
  522. clkp = cfg->clocks;
  523. best = NULL;
  524. if (cfg->clocks_size < 2) {
  525. if (cfg->clocks_size == 0)
  526. clkp = &tmp_clksrc;
  527. /* check to see if we're sourcing fclk, and if so we're
  528. * going to have to update the clock source
  529. */
  530. if (strcmp(clkp->name, "fclk") == 0) {
  531. struct s3c24xx_uart_clksrc src;
  532. s3c24xx_serial_getsource(port, &src);
  533. /* check that the port already using fclk, and if
  534. * not, then re-select fclk
  535. */
  536. if (strcmp(src.name, clkp->name) == 0) {
  537. s3c24xx_serial_setsource(port, clkp);
  538. s3c24xx_serial_getsource(port, &src);
  539. }
  540. clkp->divisor = src.divisor;
  541. }
  542. s3c24xx_serial_calcbaud(res, port, clkp, baud);
  543. best = res;
  544. resptr = best + 1;
  545. } else {
  546. resptr = res;
  547. for (i = 0; i < cfg->clocks_size; i++, clkp++) {
  548. if (s3c24xx_serial_calcbaud(resptr, port, clkp, baud))
  549. resptr++;
  550. }
  551. }
  552. /* ok, we now need to select the best clock we found */
  553. if (!best) {
  554. unsigned int deviation = (1<<30)|((1<<30)-1);
  555. int calc_deviation;
  556. for (sptr = res; sptr < resptr; sptr++) {
  557. printk(KERN_DEBUG
  558. "found clk %p (%s) quot %d, calc %d\n",
  559. sptr->clksrc, sptr->clksrc->name,
  560. sptr->quot, sptr->calc);
  561. calc_deviation = baud - sptr->calc;
  562. if (calc_deviation < 0)
  563. calc_deviation = -calc_deviation;
  564. if (calc_deviation < deviation) {
  565. best = sptr;
  566. deviation = calc_deviation;
  567. }
  568. }
  569. printk(KERN_DEBUG "best %p (deviation %d)\n", best, deviation);
  570. }
  571. printk(KERN_DEBUG "selected clock %p (%s) quot %d, calc %d\n",
  572. best->clksrc, best->clksrc->name, best->quot, best->calc);
  573. /* store results to pass back */
  574. *clksrc = best->clksrc;
  575. *clk = best->src;
  576. return best->quot;
  577. }
  578. static void s3c24xx_serial_set_termios(struct uart_port *port,
  579. struct termios *termios,
  580. struct termios *old)
  581. {
  582. struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
  583. struct s3c24xx_uart_port *ourport = to_ourport(port);
  584. struct s3c24xx_uart_clksrc *clksrc;
  585. struct clk *clk;
  586. unsigned long flags;
  587. unsigned int baud, quot;
  588. unsigned int ulcon;
  589. unsigned int umcon;
  590. /*
  591. * We don't support modem control lines.
  592. */
  593. termios->c_cflag &= ~(HUPCL | CMSPAR);
  594. termios->c_cflag |= CLOCAL;
  595. /*
  596. * Ask the core to calculate the divisor for us.
  597. */
  598. baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
  599. if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
  600. quot = port->custom_divisor;
  601. else
  602. quot = s3c24xx_serial_getclk(port, &clksrc, &clk, baud);
  603. /* check to see if we need to change clock source */
  604. if (ourport->clksrc != clksrc || ourport->baudclk != clk) {
  605. s3c24xx_serial_setsource(port, clksrc);
  606. if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
  607. clk_disable(ourport->baudclk);
  608. clk_unuse(ourport->baudclk);
  609. ourport->baudclk = NULL;
  610. }
  611. clk_use(clk);
  612. clk_enable(clk);
  613. ourport->clksrc = clksrc;
  614. ourport->baudclk = clk;
  615. }
  616. switch (termios->c_cflag & CSIZE) {
  617. case CS5:
  618. dbg("config: 5bits/char\n");
  619. ulcon = S3C2410_LCON_CS5;
  620. break;
  621. case CS6:
  622. dbg("config: 6bits/char\n");
  623. ulcon = S3C2410_LCON_CS6;
  624. break;
  625. case CS7:
  626. dbg("config: 7bits/char\n");
  627. ulcon = S3C2410_LCON_CS7;
  628. break;
  629. case CS8:
  630. default:
  631. dbg("config: 8bits/char\n");
  632. ulcon = S3C2410_LCON_CS8;
  633. break;
  634. }
  635. /* preserve original lcon IR settings */
  636. ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
  637. if (termios->c_cflag & CSTOPB)
  638. ulcon |= S3C2410_LCON_STOPB;
  639. umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
  640. if (termios->c_cflag & PARENB) {
  641. if (termios->c_cflag & PARODD)
  642. ulcon |= S3C2410_LCON_PODD;
  643. else
  644. ulcon |= S3C2410_LCON_PEVEN;
  645. } else {
  646. ulcon |= S3C2410_LCON_PNONE;
  647. }
  648. spin_lock_irqsave(&port->lock, flags);
  649. dbg("setting ulcon to %08x, brddiv to %d\n", ulcon, quot);
  650. wr_regl(port, S3C2410_ULCON, ulcon);
  651. wr_regl(port, S3C2410_UBRDIV, quot);
  652. wr_regl(port, S3C2410_UMCON, umcon);
  653. dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
  654. rd_regl(port, S3C2410_ULCON),
  655. rd_regl(port, S3C2410_UCON),
  656. rd_regl(port, S3C2410_UFCON));
  657. /*
  658. * Update the per-port timeout.
  659. */
  660. uart_update_timeout(port, termios->c_cflag, baud);
  661. /*
  662. * Which character status flags are we interested in?
  663. */
  664. port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
  665. if (termios->c_iflag & INPCK)
  666. port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
  667. /*
  668. * Which character status flags should we ignore?
  669. */
  670. port->ignore_status_mask = 0;
  671. if (termios->c_iflag & IGNPAR)
  672. port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
  673. if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
  674. port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
  675. /*
  676. * Ignore all characters if CREAD is not set.
  677. */
  678. if ((termios->c_cflag & CREAD) == 0)
  679. port->ignore_status_mask |= RXSTAT_DUMMY_READ;
  680. spin_unlock_irqrestore(&port->lock, flags);
  681. }
  682. static const char *s3c24xx_serial_type(struct uart_port *port)
  683. {
  684. switch (port->type) {
  685. case PORT_S3C2410:
  686. return "S3C2410";
  687. case PORT_S3C2440:
  688. return "S3C2440";
  689. default:
  690. return NULL;
  691. }
  692. }
  693. #define MAP_SIZE (0x100)
  694. static void s3c24xx_serial_release_port(struct uart_port *port)
  695. {
  696. release_mem_region(port->mapbase, MAP_SIZE);
  697. }
  698. static int s3c24xx_serial_request_port(struct uart_port *port)
  699. {
  700. const char *name = s3c24xx_serial_portname(port);
  701. return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
  702. }
  703. static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
  704. {
  705. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  706. if (flags & UART_CONFIG_TYPE &&
  707. s3c24xx_serial_request_port(port) == 0)
  708. port->type = info->type;
  709. }
  710. /*
  711. * verify the new serial_struct (for TIOCSSERIAL).
  712. */
  713. static int
  714. s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  715. {
  716. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  717. if (ser->type != PORT_UNKNOWN && ser->type != info->type)
  718. return -EINVAL;
  719. return 0;
  720. }
  721. #ifdef CONFIG_SERIAL_S3C2410_CONSOLE
  722. static struct console s3c24xx_serial_console;
  723. #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
  724. #else
  725. #define S3C24XX_SERIAL_CONSOLE NULL
  726. #endif
  727. static struct uart_ops s3c24xx_serial_ops = {
  728. .pm = s3c24xx_serial_pm,
  729. .tx_empty = s3c24xx_serial_tx_empty,
  730. .get_mctrl = s3c24xx_serial_get_mctrl,
  731. .set_mctrl = s3c24xx_serial_set_mctrl,
  732. .stop_tx = s3c24xx_serial_stop_tx,
  733. .start_tx = s3c24xx_serial_start_tx,
  734. .stop_rx = s3c24xx_serial_stop_rx,
  735. .enable_ms = s3c24xx_serial_enable_ms,
  736. .break_ctl = s3c24xx_serial_break_ctl,
  737. .startup = s3c24xx_serial_startup,
  738. .shutdown = s3c24xx_serial_shutdown,
  739. .set_termios = s3c24xx_serial_set_termios,
  740. .type = s3c24xx_serial_type,
  741. .release_port = s3c24xx_serial_release_port,
  742. .request_port = s3c24xx_serial_request_port,
  743. .config_port = s3c24xx_serial_config_port,
  744. .verify_port = s3c24xx_serial_verify_port,
  745. };
  746. static struct uart_driver s3c24xx_uart_drv = {
  747. .owner = THIS_MODULE,
  748. .dev_name = "s3c2410_serial",
  749. .nr = 3,
  750. .cons = S3C24XX_SERIAL_CONSOLE,
  751. .driver_name = S3C24XX_SERIAL_NAME,
  752. .devfs_name = S3C24XX_SERIAL_DEVFS,
  753. .major = S3C24XX_SERIAL_MAJOR,
  754. .minor = S3C24XX_SERIAL_MINOR,
  755. };
  756. static struct s3c24xx_uart_port s3c24xx_serial_ports[NR_PORTS] = {
  757. [0] = {
  758. .port = {
  759. .lock = SPIN_LOCK_UNLOCKED,
  760. .iotype = UPIO_MEM,
  761. .irq = IRQ_S3CUART_RX0,
  762. .uartclk = 0,
  763. .fifosize = 16,
  764. .ops = &s3c24xx_serial_ops,
  765. .flags = UPF_BOOT_AUTOCONF,
  766. .line = 0,
  767. }
  768. },
  769. [1] = {
  770. .port = {
  771. .lock = SPIN_LOCK_UNLOCKED,
  772. .iotype = UPIO_MEM,
  773. .irq = IRQ_S3CUART_RX1,
  774. .uartclk = 0,
  775. .fifosize = 16,
  776. .ops = &s3c24xx_serial_ops,
  777. .flags = UPF_BOOT_AUTOCONF,
  778. .line = 1,
  779. }
  780. },
  781. #if NR_PORTS > 2
  782. [2] = {
  783. .port = {
  784. .lock = SPIN_LOCK_UNLOCKED,
  785. .iotype = UPIO_MEM,
  786. .irq = IRQ_S3CUART_RX2,
  787. .uartclk = 0,
  788. .fifosize = 16,
  789. .ops = &s3c24xx_serial_ops,
  790. .flags = UPF_BOOT_AUTOCONF,
  791. .line = 2,
  792. }
  793. }
  794. #endif
  795. };
  796. /* s3c24xx_serial_resetport
  797. *
  798. * wrapper to call the specific reset for this port (reset the fifos
  799. * and the settings)
  800. */
  801. static inline int s3c24xx_serial_resetport(struct uart_port * port,
  802. struct s3c2410_uartcfg *cfg)
  803. {
  804. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  805. return (info->reset_port)(port, cfg);
  806. }
  807. /* s3c24xx_serial_init_port
  808. *
  809. * initialise a single serial port from the platform device given
  810. */
  811. static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
  812. struct s3c24xx_uart_info *info,
  813. struct platform_device *platdev)
  814. {
  815. struct uart_port *port = &ourport->port;
  816. struct s3c2410_uartcfg *cfg;
  817. struct resource *res;
  818. dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
  819. if (platdev == NULL)
  820. return -ENODEV;
  821. cfg = s3c24xx_dev_to_cfg(&platdev->dev);
  822. if (port->mapbase != 0)
  823. return 0;
  824. if (cfg->hwport > 3)
  825. return -EINVAL;
  826. /* setup info for port */
  827. port->dev = &platdev->dev;
  828. ourport->info = info;
  829. /* copy the info in from provided structure */
  830. ourport->port.fifosize = info->fifosize;
  831. dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
  832. port->uartclk = 1;
  833. if (cfg->uart_flags & UPF_CONS_FLOW) {
  834. dbg("s3c24xx_serial_init_port: enabling flow control\n");
  835. port->flags |= UPF_CONS_FLOW;
  836. }
  837. /* sort our the physical and virtual addresses for each UART */
  838. res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
  839. if (res == NULL) {
  840. printk(KERN_ERR "failed to find memory resource for uart\n");
  841. return -EINVAL;
  842. }
  843. dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
  844. port->mapbase = res->start;
  845. port->membase = S3C24XX_VA_UART + (res->start - S3C2410_PA_UART);
  846. port->irq = platform_get_irq(platdev, 0);
  847. ourport->clk = clk_get(&platdev->dev, "uart");
  848. if (ourport->clk != NULL && !IS_ERR(ourport->clk))
  849. clk_use(ourport->clk);
  850. dbg("port: map=%08x, mem=%08x, irq=%d, clock=%ld\n",
  851. port->mapbase, port->membase, port->irq, port->uartclk);
  852. /* reset the fifos (and setup the uart) */
  853. s3c24xx_serial_resetport(port, cfg);
  854. return 0;
  855. }
  856. /* Device driver serial port probe */
  857. static int probe_index = 0;
  858. int s3c24xx_serial_probe(struct device *_dev,
  859. struct s3c24xx_uart_info *info)
  860. {
  861. struct s3c24xx_uart_port *ourport;
  862. struct platform_device *dev = to_platform_device(_dev);
  863. int ret;
  864. dbg("s3c24xx_serial_probe(%p, %p) %d\n", _dev, info, probe_index);
  865. ourport = &s3c24xx_serial_ports[probe_index];
  866. probe_index++;
  867. dbg("%s: initialising port %p...\n", __FUNCTION__, ourport);
  868. ret = s3c24xx_serial_init_port(ourport, info, dev);
  869. if (ret < 0)
  870. goto probe_err;
  871. dbg("%s: adding port\n", __FUNCTION__);
  872. uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
  873. dev_set_drvdata(_dev, &ourport->port);
  874. return 0;
  875. probe_err:
  876. return ret;
  877. }
  878. int s3c24xx_serial_remove(struct device *_dev)
  879. {
  880. struct uart_port *port = s3c24xx_dev_to_port(_dev);
  881. if (port)
  882. uart_remove_one_port(&s3c24xx_uart_drv, port);
  883. return 0;
  884. }
  885. /* UART power management code */
  886. #ifdef CONFIG_PM
  887. int s3c24xx_serial_suspend(struct device *dev, pm_message_t state, u32 level)
  888. {
  889. struct uart_port *port = s3c24xx_dev_to_port(dev);
  890. if (port && level == SUSPEND_DISABLE)
  891. uart_suspend_port(&s3c24xx_uart_drv, port);
  892. return 0;
  893. }
  894. int s3c24xx_serial_resume(struct device *dev, u32 level)
  895. {
  896. struct uart_port *port = s3c24xx_dev_to_port(dev);
  897. struct s3c24xx_uart_port *ourport = to_ourport(port);
  898. if (port && level == RESUME_ENABLE) {
  899. clk_enable(ourport->clk);
  900. s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
  901. clk_disable(ourport->clk);
  902. uart_resume_port(&s3c24xx_uart_drv, port);
  903. }
  904. return 0;
  905. }
  906. #else
  907. #define s3c24xx_serial_suspend NULL
  908. #define s3c24xx_serial_resume NULL
  909. #endif
  910. int s3c24xx_serial_init(struct device_driver *drv,
  911. struct s3c24xx_uart_info *info)
  912. {
  913. dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
  914. return driver_register(drv);
  915. }
  916. /* now comes the code to initialise either the s3c2410 or s3c2440 serial
  917. * port information
  918. */
  919. /* cpu specific variations on the serial port support */
  920. #ifdef CONFIG_CPU_S3C2400
  921. static int s3c2400_serial_getsource(struct uart_port *port,
  922. struct s3c24xx_uart_clksrc *clk)
  923. {
  924. clk->divisor = 1;
  925. clk->name = "pclk";
  926. return 0;
  927. }
  928. static int s3c2400_serial_setsource(struct uart_port *port,
  929. struct s3c24xx_uart_clksrc *clk)
  930. {
  931. return 0;
  932. }
  933. static int s3c2400_serial_resetport(struct uart_port *port,
  934. struct s3c2410_uartcfg *cfg)
  935. {
  936. dbg("s3c2400_serial_resetport: port=%p (%08lx), cfg=%p\n",
  937. port, port->mapbase, cfg);
  938. wr_regl(port, S3C2410_UCON, cfg->ucon);
  939. wr_regl(port, S3C2410_ULCON, cfg->ulcon);
  940. /* reset both fifos */
  941. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  942. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  943. return 0;
  944. }
  945. static struct s3c24xx_uart_info s3c2400_uart_inf = {
  946. .name = "Samsung S3C2400 UART",
  947. .type = PORT_S3C2400,
  948. .fifosize = 16,
  949. .rx_fifomask = S3C2410_UFSTAT_RXMASK,
  950. .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
  951. .rx_fifofull = S3C2410_UFSTAT_RXFULL,
  952. .tx_fifofull = S3C2410_UFSTAT_TXFULL,
  953. .tx_fifomask = S3C2410_UFSTAT_TXMASK,
  954. .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
  955. .get_clksrc = s3c2400_serial_getsource,
  956. .set_clksrc = s3c2400_serial_setsource,
  957. .reset_port = s3c2400_serial_resetport,
  958. };
  959. static int s3c2400_serial_probe(struct device *dev)
  960. {
  961. return s3c24xx_serial_probe(dev, &s3c2400_uart_inf);
  962. }
  963. static struct device_driver s3c2400_serial_drv = {
  964. .name = "s3c2400-uart",
  965. .bus = &platform_bus_type,
  966. .probe = s3c2400_serial_probe,
  967. .remove = s3c24xx_serial_remove,
  968. .suspend = s3c24xx_serial_suspend,
  969. .resume = s3c24xx_serial_resume,
  970. };
  971. static inline int s3c2400_serial_init(void)
  972. {
  973. return s3c24xx_serial_init(&s3c2400_serial_drv, &s3c2400_uart_inf);
  974. }
  975. static inline void s3c2400_serial_exit(void)
  976. {
  977. driver_unregister(&s3c2400_serial_drv);
  978. }
  979. #define s3c2400_uart_inf_at &s3c2400_uart_inf
  980. #else
  981. static inline int s3c2400_serial_init(void)
  982. {
  983. return 0;
  984. }
  985. static inline void s3c2400_serial_exit(void)
  986. {
  987. }
  988. #define s3c2400_uart_inf_at NULL
  989. #endif /* CONFIG_CPU_S3C2400 */
  990. /* S3C2410 support */
  991. #ifdef CONFIG_CPU_S3C2410
  992. static int s3c2410_serial_setsource(struct uart_port *port,
  993. struct s3c24xx_uart_clksrc *clk)
  994. {
  995. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  996. if (strcmp(clk->name, "uclk") == 0)
  997. ucon |= S3C2410_UCON_UCLK;
  998. else
  999. ucon &= ~S3C2410_UCON_UCLK;
  1000. wr_regl(port, S3C2410_UCON, ucon);
  1001. return 0;
  1002. }
  1003. static int s3c2410_serial_getsource(struct uart_port *port,
  1004. struct s3c24xx_uart_clksrc *clk)
  1005. {
  1006. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1007. clk->divisor = 1;
  1008. clk->name = (ucon & S3C2410_UCON_UCLK) ? "uclk" : "pclk";
  1009. return 0;
  1010. }
  1011. static int s3c2410_serial_resetport(struct uart_port *port,
  1012. struct s3c2410_uartcfg *cfg)
  1013. {
  1014. dbg("s3c2410_serial_resetport: port=%p (%08lx), cfg=%p\n",
  1015. port, port->mapbase, cfg);
  1016. wr_regl(port, S3C2410_UCON, cfg->ucon);
  1017. wr_regl(port, S3C2410_ULCON, cfg->ulcon);
  1018. /* reset both fifos */
  1019. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  1020. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  1021. return 0;
  1022. }
  1023. static struct s3c24xx_uart_info s3c2410_uart_inf = {
  1024. .name = "Samsung S3C2410 UART",
  1025. .type = PORT_S3C2410,
  1026. .fifosize = 16,
  1027. .rx_fifomask = S3C2410_UFSTAT_RXMASK,
  1028. .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
  1029. .rx_fifofull = S3C2410_UFSTAT_RXFULL,
  1030. .tx_fifofull = S3C2410_UFSTAT_TXFULL,
  1031. .tx_fifomask = S3C2410_UFSTAT_TXMASK,
  1032. .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
  1033. .get_clksrc = s3c2410_serial_getsource,
  1034. .set_clksrc = s3c2410_serial_setsource,
  1035. .reset_port = s3c2410_serial_resetport,
  1036. };
  1037. /* device management */
  1038. static int s3c2410_serial_probe(struct device *dev)
  1039. {
  1040. return s3c24xx_serial_probe(dev, &s3c2410_uart_inf);
  1041. }
  1042. static struct device_driver s3c2410_serial_drv = {
  1043. .name = "s3c2410-uart",
  1044. .bus = &platform_bus_type,
  1045. .probe = s3c2410_serial_probe,
  1046. .remove = s3c24xx_serial_remove,
  1047. .suspend = s3c24xx_serial_suspend,
  1048. .resume = s3c24xx_serial_resume,
  1049. };
  1050. static inline int s3c2410_serial_init(void)
  1051. {
  1052. return s3c24xx_serial_init(&s3c2410_serial_drv, &s3c2410_uart_inf);
  1053. }
  1054. static inline void s3c2410_serial_exit(void)
  1055. {
  1056. driver_unregister(&s3c2410_serial_drv);
  1057. }
  1058. #define s3c2410_uart_inf_at &s3c2410_uart_inf
  1059. #else
  1060. static inline int s3c2410_serial_init(void)
  1061. {
  1062. return 0;
  1063. }
  1064. static inline void s3c2410_serial_exit(void)
  1065. {
  1066. }
  1067. #define s3c2410_uart_inf_at NULL
  1068. #endif /* CONFIG_CPU_S3C2410 */
  1069. #ifdef CONFIG_CPU_S3C2440
  1070. static int s3c2440_serial_setsource(struct uart_port *port,
  1071. struct s3c24xx_uart_clksrc *clk)
  1072. {
  1073. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1074. // todo - proper fclk<>nonfclk switch //
  1075. ucon &= ~S3C2440_UCON_CLKMASK;
  1076. if (strcmp(clk->name, "uclk") == 0)
  1077. ucon |= S3C2440_UCON_UCLK;
  1078. else if (strcmp(clk->name, "pclk") == 0)
  1079. ucon |= S3C2440_UCON_PCLK;
  1080. else if (strcmp(clk->name, "fclk") == 0)
  1081. ucon |= S3C2440_UCON_FCLK;
  1082. else {
  1083. printk(KERN_ERR "unknown clock source %s\n", clk->name);
  1084. return -EINVAL;
  1085. }
  1086. wr_regl(port, S3C2410_UCON, ucon);
  1087. return 0;
  1088. }
  1089. static int s3c2440_serial_getsource(struct uart_port *port,
  1090. struct s3c24xx_uart_clksrc *clk)
  1091. {
  1092. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1093. unsigned long ucon0, ucon1, ucon2;
  1094. switch (ucon & S3C2440_UCON_CLKMASK) {
  1095. case S3C2440_UCON_UCLK:
  1096. clk->divisor = 1;
  1097. clk->name = "uclk";
  1098. break;
  1099. case S3C2440_UCON_PCLK:
  1100. case S3C2440_UCON_PCLK2:
  1101. clk->divisor = 1;
  1102. clk->name = "pclk";
  1103. break;
  1104. case S3C2440_UCON_FCLK:
  1105. /* the fun of calculating the uart divisors on
  1106. * the s3c2440 */
  1107. ucon0 = __raw_readl(S3C24XX_VA_UART0 + S3C2410_UCON);
  1108. ucon1 = __raw_readl(S3C24XX_VA_UART1 + S3C2410_UCON);
  1109. ucon2 = __raw_readl(S3C24XX_VA_UART2 + S3C2410_UCON);
  1110. printk("ucons: %08lx, %08lx, %08lx\n", ucon0, ucon1, ucon2);
  1111. ucon0 &= S3C2440_UCON0_DIVMASK;
  1112. ucon1 &= S3C2440_UCON1_DIVMASK;
  1113. ucon2 &= S3C2440_UCON2_DIVMASK;
  1114. if (ucon0 != 0) {
  1115. clk->divisor = ucon0 >> S3C2440_UCON_DIVSHIFT;
  1116. clk->divisor += 6;
  1117. } else if (ucon1 != 0) {
  1118. clk->divisor = ucon1 >> S3C2440_UCON_DIVSHIFT;
  1119. clk->divisor += 21;
  1120. } else if (ucon2 != 0) {
  1121. clk->divisor = ucon2 >> S3C2440_UCON_DIVSHIFT;
  1122. clk->divisor += 36;
  1123. } else {
  1124. /* manual calims 44, seems to be 9 */
  1125. clk->divisor = 9;
  1126. }
  1127. clk->name = "fclk";
  1128. break;
  1129. }
  1130. return 0;
  1131. }
  1132. static int s3c2440_serial_resetport(struct uart_port *port,
  1133. struct s3c2410_uartcfg *cfg)
  1134. {
  1135. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1136. dbg("s3c2440_serial_resetport: port=%p (%08lx), cfg=%p\n",
  1137. port, port->mapbase, cfg);
  1138. /* ensure we don't change the clock settings... */
  1139. ucon &= (S3C2440_UCON0_DIVMASK | (3<<10));
  1140. wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
  1141. wr_regl(port, S3C2410_ULCON, cfg->ulcon);
  1142. /* reset both fifos */
  1143. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  1144. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  1145. return 0;
  1146. }
  1147. static struct s3c24xx_uart_info s3c2440_uart_inf = {
  1148. .name = "Samsung S3C2440 UART",
  1149. .type = PORT_S3C2440,
  1150. .fifosize = 64,
  1151. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1152. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1153. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1154. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1155. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1156. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1157. .get_clksrc = s3c2440_serial_getsource,
  1158. .set_clksrc = s3c2440_serial_setsource,
  1159. .reset_port = s3c2440_serial_resetport,
  1160. };
  1161. /* device management */
  1162. static int s3c2440_serial_probe(struct device *dev)
  1163. {
  1164. dbg("s3c2440_serial_probe: dev=%p\n", dev);
  1165. return s3c24xx_serial_probe(dev, &s3c2440_uart_inf);
  1166. }
  1167. static struct device_driver s3c2440_serial_drv = {
  1168. .name = "s3c2440-uart",
  1169. .bus = &platform_bus_type,
  1170. .probe = s3c2440_serial_probe,
  1171. .remove = s3c24xx_serial_remove,
  1172. .suspend = s3c24xx_serial_suspend,
  1173. .resume = s3c24xx_serial_resume,
  1174. };
  1175. static inline int s3c2440_serial_init(void)
  1176. {
  1177. return s3c24xx_serial_init(&s3c2440_serial_drv, &s3c2440_uart_inf);
  1178. }
  1179. static inline void s3c2440_serial_exit(void)
  1180. {
  1181. driver_unregister(&s3c2440_serial_drv);
  1182. }
  1183. #define s3c2440_uart_inf_at &s3c2440_uart_inf
  1184. #else
  1185. static inline int s3c2440_serial_init(void)
  1186. {
  1187. return 0;
  1188. }
  1189. static inline void s3c2440_serial_exit(void)
  1190. {
  1191. }
  1192. #define s3c2440_uart_inf_at NULL
  1193. #endif /* CONFIG_CPU_S3C2440 */
  1194. /* module initialisation code */
  1195. static int __init s3c24xx_serial_modinit(void)
  1196. {
  1197. int ret;
  1198. ret = uart_register_driver(&s3c24xx_uart_drv);
  1199. if (ret < 0) {
  1200. printk(KERN_ERR "failed to register UART driver\n");
  1201. return -1;
  1202. }
  1203. s3c2400_serial_init();
  1204. s3c2410_serial_init();
  1205. s3c2440_serial_init();
  1206. return 0;
  1207. }
  1208. static void __exit s3c24xx_serial_modexit(void)
  1209. {
  1210. s3c2400_serial_exit();
  1211. s3c2410_serial_exit();
  1212. s3c2440_serial_exit();
  1213. uart_unregister_driver(&s3c24xx_uart_drv);
  1214. }
  1215. module_init(s3c24xx_serial_modinit);
  1216. module_exit(s3c24xx_serial_modexit);
  1217. /* Console code */
  1218. #ifdef CONFIG_SERIAL_S3C2410_CONSOLE
  1219. static struct uart_port *cons_uart;
  1220. static int
  1221. s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
  1222. {
  1223. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1224. unsigned long ufstat, utrstat;
  1225. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  1226. /* fifo mode - check ammount of data in fifo registers... */
  1227. ufstat = rd_regl(port, S3C2410_UFSTAT);
  1228. return (ufstat & info->tx_fifofull) ? 0 : 1;
  1229. }
  1230. /* in non-fifo mode, we go and use the tx buffer empty */
  1231. utrstat = rd_regl(port, S3C2410_UTRSTAT);
  1232. return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
  1233. }
  1234. static void
  1235. s3c24xx_serial_console_write(struct console *co, const char *s,
  1236. unsigned int count)
  1237. {
  1238. int i;
  1239. unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
  1240. for (i = 0; i < count; i++) {
  1241. while (!s3c24xx_serial_console_txrdy(cons_uart, ufcon))
  1242. barrier();
  1243. wr_regb(cons_uart, S3C2410_UTXH, s[i]);
  1244. if (s[i] == '\n') {
  1245. while (!s3c24xx_serial_console_txrdy(cons_uart, ufcon))
  1246. barrier();
  1247. wr_regb(cons_uart, S3C2410_UTXH, '\r');
  1248. }
  1249. }
  1250. }
  1251. static void __init
  1252. s3c24xx_serial_get_options(struct uart_port *port, int *baud,
  1253. int *parity, int *bits)
  1254. {
  1255. struct s3c24xx_uart_clksrc clksrc;
  1256. struct clk *clk;
  1257. unsigned int ulcon;
  1258. unsigned int ucon;
  1259. unsigned int ubrdiv;
  1260. unsigned long rate;
  1261. ulcon = rd_regl(port, S3C2410_ULCON);
  1262. ucon = rd_regl(port, S3C2410_UCON);
  1263. ubrdiv = rd_regl(port, S3C2410_UBRDIV);
  1264. dbg("s3c24xx_serial_get_options: port=%p\n"
  1265. "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
  1266. port, ulcon, ucon, ubrdiv);
  1267. if ((ucon & 0xf) != 0) {
  1268. /* consider the serial port configured if the tx/rx mode set */
  1269. switch (ulcon & S3C2410_LCON_CSMASK) {
  1270. case S3C2410_LCON_CS5:
  1271. *bits = 5;
  1272. break;
  1273. case S3C2410_LCON_CS6:
  1274. *bits = 6;
  1275. break;
  1276. case S3C2410_LCON_CS7:
  1277. *bits = 7;
  1278. break;
  1279. default:
  1280. case S3C2410_LCON_CS8:
  1281. *bits = 8;
  1282. break;
  1283. }
  1284. switch (ulcon & S3C2410_LCON_PMASK) {
  1285. case S3C2410_LCON_PEVEN:
  1286. *parity = 'e';
  1287. break;
  1288. case S3C2410_LCON_PODD:
  1289. *parity = 'o';
  1290. break;
  1291. case S3C2410_LCON_PNONE:
  1292. default:
  1293. *parity = 'n';
  1294. }
  1295. /* now calculate the baud rate */
  1296. s3c24xx_serial_getsource(port, &clksrc);
  1297. clk = clk_get(port->dev, clksrc.name);
  1298. if (!IS_ERR(clk) && clk != NULL)
  1299. rate = clk_get_rate(clk) / clksrc.divisor;
  1300. else
  1301. rate = 1;
  1302. *baud = rate / ( 16 * (ubrdiv + 1));
  1303. dbg("calculated baud %d\n", *baud);
  1304. }
  1305. }
  1306. /* s3c24xx_serial_init_ports
  1307. *
  1308. * initialise the serial ports from the machine provided initialisation
  1309. * data.
  1310. */
  1311. static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info *info)
  1312. {
  1313. struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
  1314. struct platform_device **platdev_ptr;
  1315. int i;
  1316. dbg("s3c24xx_serial_init_ports: initialising ports...\n");
  1317. platdev_ptr = s3c24xx_uart_devs;
  1318. for (i = 0; i < NR_PORTS; i++, ptr++, platdev_ptr++) {
  1319. s3c24xx_serial_init_port(ptr, info, *platdev_ptr);
  1320. }
  1321. return 0;
  1322. }
  1323. static int __init
  1324. s3c24xx_serial_console_setup(struct console *co, char *options)
  1325. {
  1326. struct uart_port *port;
  1327. int baud = 9600;
  1328. int bits = 8;
  1329. int parity = 'n';
  1330. int flow = 'n';
  1331. dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
  1332. co, co->index, options);
  1333. /* is this a valid port */
  1334. if (co->index == -1 || co->index >= NR_PORTS)
  1335. co->index = 0;
  1336. port = &s3c24xx_serial_ports[co->index].port;
  1337. /* is the port configured? */
  1338. if (port->mapbase == 0x0) {
  1339. co->index = 0;
  1340. port = &s3c24xx_serial_ports[co->index].port;
  1341. }
  1342. cons_uart = port;
  1343. dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
  1344. /*
  1345. * Check whether an invalid uart number has been specified, and
  1346. * if so, search for the first available port that does have
  1347. * console support.
  1348. */
  1349. if (options)
  1350. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1351. else
  1352. s3c24xx_serial_get_options(port, &baud, &parity, &bits);
  1353. dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
  1354. return uart_set_options(port, co, baud, parity, bits, flow);
  1355. }
  1356. /* s3c24xx_serial_initconsole
  1357. *
  1358. * initialise the console from one of the uart drivers
  1359. */
  1360. static struct console s3c24xx_serial_console =
  1361. {
  1362. .name = S3C24XX_SERIAL_NAME,
  1363. .device = uart_console_device,
  1364. .flags = CON_PRINTBUFFER,
  1365. .index = -1,
  1366. .write = s3c24xx_serial_console_write,
  1367. .setup = s3c24xx_serial_console_setup
  1368. };
  1369. static int s3c24xx_serial_initconsole(void)
  1370. {
  1371. struct s3c24xx_uart_info *info;
  1372. struct platform_device *dev = s3c24xx_uart_devs[0];
  1373. dbg("s3c24xx_serial_initconsole\n");
  1374. /* select driver based on the cpu */
  1375. if (dev == NULL) {
  1376. printk(KERN_ERR "s3c24xx: no devices for console init\n");
  1377. return 0;
  1378. }
  1379. if (strcmp(dev->name, "s3c2400-uart") == 0) {
  1380. info = s3c2400_uart_inf_at;
  1381. } else if (strcmp(dev->name, "s3c2410-uart") == 0) {
  1382. info = s3c2410_uart_inf_at;
  1383. } else if (strcmp(dev->name, "s3c2440-uart") == 0) {
  1384. info = s3c2440_uart_inf_at;
  1385. } else {
  1386. printk(KERN_ERR "s3c24xx: no driver for %s\n", dev->name);
  1387. return 0;
  1388. }
  1389. if (info == NULL) {
  1390. printk(KERN_ERR "s3c24xx: no driver for console\n");
  1391. return 0;
  1392. }
  1393. s3c24xx_serial_console.data = &s3c24xx_uart_drv;
  1394. s3c24xx_serial_init_ports(info);
  1395. register_console(&s3c24xx_serial_console);
  1396. return 0;
  1397. }
  1398. console_initcall(s3c24xx_serial_initconsole);
  1399. #endif /* CONFIG_SERIAL_S3C2410_CONSOLE */
  1400. MODULE_LICENSE("GPL");
  1401. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  1402. MODULE_DESCRIPTION("Samsung S3C2410/S3C2440 Serial port driver");