s3c2410.c 44 KB

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