s3c2410.c 41 KB

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