s3c2410.c 43 KB

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