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