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