s3c2410.c 41 KB

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