samsung_tty.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver core for Samsung SoC onboard UARTs.
  4. *
  5. * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
  6. * http://armlinux.simtec.co.uk/
  7. */
  8. /* Note on 2410 error handling
  9. *
  10. * The s3c2410 manual has a love/hate affair with the contents of the
  11. * UERSTAT register in the UART blocks, and keeps marking some of the
  12. * error bits as reserved. Having checked with the s3c2410x01,
  13. * it copes with BREAKs properly, so I am happy to ignore the RESERVED
  14. * feature from the latter versions of the manual.
  15. *
  16. * If it becomes aparrent that latter versions of the 2410 remove these
  17. * bits, then action will have to be taken to differentiate the versions
  18. * and change the policy on BREAK
  19. *
  20. * BJD, 04-Nov-2004
  21. */
  22. #include <linux/console.h>
  23. #include <linux/clk.h>
  24. #include <linux/cpufreq.h>
  25. #include <linux/delay.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/dmaengine.h>
  28. #include <linux/init.h>
  29. #include <linux/io.h>
  30. #include <linux/ioport.h>
  31. #include <linux/math.h>
  32. #include <linux/module.h>
  33. #include <linux/of.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/serial.h>
  36. #include <linux/serial_core.h>
  37. #include <linux/serial_s3c.h>
  38. #include <linux/slab.h>
  39. #include <linux/sysrq.h>
  40. #include <linux/tty.h>
  41. #include <linux/tty_flip.h>
  42. #include <linux/types.h>
  43. #include <asm/irq.h>
  44. /* UART name and device definitions */
  45. #define S3C24XX_SERIAL_NAME "ttySAC"
  46. #define S3C24XX_SERIAL_MAJOR 204
  47. #define S3C24XX_SERIAL_MINOR 64
  48. #ifdef CONFIG_ARM64
  49. #define UART_NR 12
  50. #else
  51. #define UART_NR CONFIG_SERIAL_SAMSUNG_UARTS
  52. #endif
  53. #define S3C24XX_TX_PIO 1
  54. #define S3C24XX_TX_DMA 2
  55. #define S3C24XX_RX_PIO 1
  56. #define S3C24XX_RX_DMA 2
  57. /* flag to ignore all characters coming in */
  58. #define RXSTAT_DUMMY_READ (0x10000000)
  59. enum s3c24xx_port_type {
  60. TYPE_S3C6400,
  61. TYPE_APPLE_S5L,
  62. };
  63. struct s3c24xx_uart_info {
  64. const char *name;
  65. enum s3c24xx_port_type type;
  66. unsigned int port_type;
  67. unsigned int fifosize;
  68. u32 rx_fifomask;
  69. u32 rx_fifoshift;
  70. u32 rx_fifofull;
  71. u32 tx_fifomask;
  72. u32 tx_fifoshift;
  73. u32 tx_fifofull;
  74. u32 clksel_mask;
  75. u32 clksel_shift;
  76. u32 ucon_mask;
  77. u8 def_clk_sel;
  78. u8 num_clks;
  79. u8 iotype;
  80. /* uart port features */
  81. bool has_divslot;
  82. };
  83. struct s3c24xx_serial_drv_data {
  84. const struct s3c24xx_uart_info info;
  85. const struct s3c2410_uartcfg def_cfg;
  86. const unsigned int fifosize[UART_NR];
  87. };
  88. struct s3c24xx_uart_dma {
  89. unsigned int rx_chan_id;
  90. unsigned int tx_chan_id;
  91. struct dma_slave_config rx_conf;
  92. struct dma_slave_config tx_conf;
  93. struct dma_chan *rx_chan;
  94. struct dma_chan *tx_chan;
  95. dma_addr_t rx_addr;
  96. dma_addr_t tx_addr;
  97. dma_cookie_t rx_cookie;
  98. dma_cookie_t tx_cookie;
  99. char *rx_buf;
  100. dma_addr_t tx_transfer_addr;
  101. size_t rx_size;
  102. size_t tx_size;
  103. struct dma_async_tx_descriptor *tx_desc;
  104. struct dma_async_tx_descriptor *rx_desc;
  105. int tx_bytes_requested;
  106. int rx_bytes_requested;
  107. };
  108. struct s3c24xx_uart_port {
  109. unsigned char rx_enabled;
  110. unsigned char tx_enabled;
  111. unsigned int pm_level;
  112. unsigned long baudclk_rate;
  113. unsigned int min_dma_size;
  114. unsigned int rx_irq;
  115. unsigned int tx_irq;
  116. unsigned int tx_in_progress;
  117. unsigned int tx_mode;
  118. unsigned int rx_mode;
  119. const struct s3c24xx_uart_info *info;
  120. struct clk *clk;
  121. struct clk *baudclk;
  122. struct uart_port port;
  123. const struct s3c24xx_serial_drv_data *drv_data;
  124. /* reference to platform data */
  125. const struct s3c2410_uartcfg *cfg;
  126. struct s3c24xx_uart_dma *dma;
  127. };
  128. static void s3c24xx_serial_tx_chars(struct s3c24xx_uart_port *ourport);
  129. /* conversion functions */
  130. #define s3c24xx_dev_to_port(__dev) dev_get_drvdata(__dev)
  131. /* register access controls */
  132. #define portaddr(port, reg) ((port)->membase + (reg))
  133. #define portaddrl(port, reg) \
  134. ((unsigned long *)(unsigned long)((port)->membase + (reg)))
  135. static u32 rd_reg(const struct uart_port *port, u32 reg)
  136. {
  137. switch (port->iotype) {
  138. case UPIO_MEM:
  139. return readb_relaxed(portaddr(port, reg));
  140. case UPIO_MEM32:
  141. return readl_relaxed(portaddr(port, reg));
  142. default:
  143. return 0;
  144. }
  145. return 0;
  146. }
  147. #define rd_regl(port, reg) (readl_relaxed(portaddr(port, reg)))
  148. static void wr_reg(const struct uart_port *port, u32 reg, u32 val)
  149. {
  150. switch (port->iotype) {
  151. case UPIO_MEM:
  152. writeb_relaxed(val, portaddr(port, reg));
  153. break;
  154. case UPIO_MEM32:
  155. writel_relaxed(val, portaddr(port, reg));
  156. break;
  157. }
  158. }
  159. #define wr_regl(port, reg, val) writel_relaxed(val, portaddr(port, reg))
  160. /* Byte-order aware bit setting/clearing functions. */
  161. static inline void s3c24xx_set_bit(const struct uart_port *port, int idx,
  162. u32 reg)
  163. {
  164. unsigned long flags;
  165. u32 val;
  166. local_irq_save(flags);
  167. val = rd_regl(port, reg);
  168. val |= (1 << idx);
  169. wr_regl(port, reg, val);
  170. local_irq_restore(flags);
  171. }
  172. static inline void s3c24xx_clear_bit(const struct uart_port *port, int idx,
  173. u32 reg)
  174. {
  175. unsigned long flags;
  176. u32 val;
  177. local_irq_save(flags);
  178. val = rd_regl(port, reg);
  179. val &= ~(1 << idx);
  180. wr_regl(port, reg, val);
  181. local_irq_restore(flags);
  182. }
  183. static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
  184. {
  185. return container_of(port, struct s3c24xx_uart_port, port);
  186. }
  187. /* translate a port to the device name */
  188. static inline const char *s3c24xx_serial_portname(const struct uart_port *port)
  189. {
  190. return to_platform_device(port->dev)->name;
  191. }
  192. static bool s3c24xx_serial_txempty_nofifo(const struct uart_port *port)
  193. {
  194. return rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE;
  195. }
  196. static void s3c24xx_serial_rx_enable(struct uart_port *port)
  197. {
  198. struct s3c24xx_uart_port *ourport = to_ourport(port);
  199. unsigned long flags;
  200. int count = 10000;
  201. u32 ucon, ufcon;
  202. uart_port_lock_irqsave(port, &flags);
  203. while (--count && !s3c24xx_serial_txempty_nofifo(port))
  204. udelay(100);
  205. ufcon = rd_regl(port, S3C2410_UFCON);
  206. ufcon |= S3C2410_UFCON_RESETRX;
  207. wr_regl(port, S3C2410_UFCON, ufcon);
  208. ucon = rd_regl(port, S3C2410_UCON);
  209. ucon |= S3C2410_UCON_RXIRQMODE;
  210. wr_regl(port, S3C2410_UCON, ucon);
  211. ourport->rx_enabled = 1;
  212. uart_port_unlock_irqrestore(port, flags);
  213. }
  214. static void s3c24xx_serial_rx_disable(struct uart_port *port)
  215. {
  216. struct s3c24xx_uart_port *ourport = to_ourport(port);
  217. unsigned long flags;
  218. u32 ucon;
  219. uart_port_lock_irqsave(port, &flags);
  220. ucon = rd_regl(port, S3C2410_UCON);
  221. ucon &= ~S3C2410_UCON_RXIRQMODE;
  222. wr_regl(port, S3C2410_UCON, ucon);
  223. ourport->rx_enabled = 0;
  224. uart_port_unlock_irqrestore(port, flags);
  225. }
  226. static void s3c24xx_serial_stop_tx(struct uart_port *port)
  227. {
  228. struct s3c24xx_uart_port *ourport = to_ourport(port);
  229. struct s3c24xx_uart_dma *dma = ourport->dma;
  230. struct dma_tx_state state;
  231. int count;
  232. if (!ourport->tx_enabled)
  233. return;
  234. switch (ourport->info->type) {
  235. case TYPE_S3C6400:
  236. s3c24xx_set_bit(port, S3C64XX_UINTM_TXD, S3C64XX_UINTM);
  237. break;
  238. case TYPE_APPLE_S5L:
  239. s3c24xx_clear_bit(port, APPLE_S5L_UCON_TXTHRESH_ENA, S3C2410_UCON);
  240. break;
  241. default:
  242. disable_irq_nosync(ourport->tx_irq);
  243. break;
  244. }
  245. if (dma && dma->tx_chan && ourport->tx_in_progress == S3C24XX_TX_DMA) {
  246. dmaengine_pause(dma->tx_chan);
  247. dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state);
  248. dmaengine_terminate_all(dma->tx_chan);
  249. dma_sync_single_for_cpu(dma->tx_chan->device->dev,
  250. dma->tx_transfer_addr, dma->tx_size,
  251. DMA_TO_DEVICE);
  252. async_tx_ack(dma->tx_desc);
  253. count = dma->tx_bytes_requested - state.residue;
  254. uart_xmit_advance(port, count);
  255. }
  256. ourport->tx_enabled = 0;
  257. ourport->tx_in_progress = 0;
  258. if (port->flags & UPF_CONS_FLOW)
  259. s3c24xx_serial_rx_enable(port);
  260. ourport->tx_mode = 0;
  261. }
  262. static void s3c24xx_serial_start_next_tx(struct s3c24xx_uart_port *ourport);
  263. static void s3c24xx_serial_tx_dma_complete(void *args)
  264. {
  265. struct s3c24xx_uart_port *ourport = args;
  266. struct uart_port *port = &ourport->port;
  267. struct tty_port *tport = &port->state->port;
  268. struct s3c24xx_uart_dma *dma = ourport->dma;
  269. struct dma_tx_state state;
  270. unsigned long flags;
  271. int count;
  272. dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state);
  273. count = dma->tx_bytes_requested - state.residue;
  274. async_tx_ack(dma->tx_desc);
  275. dma_sync_single_for_cpu(dma->tx_chan->device->dev,
  276. dma->tx_transfer_addr, dma->tx_size,
  277. DMA_TO_DEVICE);
  278. uart_port_lock_irqsave(port, &flags);
  279. uart_xmit_advance(port, count);
  280. ourport->tx_in_progress = 0;
  281. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  282. uart_write_wakeup(port);
  283. s3c24xx_serial_start_next_tx(ourport);
  284. uart_port_unlock_irqrestore(port, flags);
  285. }
  286. static void enable_tx_dma(struct s3c24xx_uart_port *ourport)
  287. {
  288. const struct uart_port *port = &ourport->port;
  289. u32 ucon;
  290. /* Mask Tx interrupt */
  291. switch (ourport->info->type) {
  292. case TYPE_S3C6400:
  293. s3c24xx_set_bit(port, S3C64XX_UINTM_TXD, S3C64XX_UINTM);
  294. break;
  295. case TYPE_APPLE_S5L:
  296. WARN_ON(1); // No DMA
  297. break;
  298. default:
  299. disable_irq_nosync(ourport->tx_irq);
  300. break;
  301. }
  302. /* Enable tx dma mode */
  303. ucon = rd_regl(port, S3C2410_UCON);
  304. ucon &= ~(S3C64XX_UCON_TXBURST_MASK | S3C64XX_UCON_TXMODE_MASK);
  305. ucon |= S3C64XX_UCON_TXBURST_1;
  306. ucon |= S3C64XX_UCON_TXMODE_DMA;
  307. wr_regl(port, S3C2410_UCON, ucon);
  308. ourport->tx_mode = S3C24XX_TX_DMA;
  309. }
  310. static void enable_tx_pio(struct s3c24xx_uart_port *ourport)
  311. {
  312. const struct uart_port *port = &ourport->port;
  313. u32 ucon, ufcon;
  314. /* Set ufcon txtrig */
  315. ourport->tx_in_progress = S3C24XX_TX_PIO;
  316. ufcon = rd_regl(port, S3C2410_UFCON);
  317. wr_regl(port, S3C2410_UFCON, ufcon);
  318. /* Enable tx pio mode */
  319. ucon = rd_regl(port, S3C2410_UCON);
  320. ucon &= ~(S3C64XX_UCON_TXMODE_MASK);
  321. ucon |= S3C64XX_UCON_TXMODE_CPU;
  322. wr_regl(port, S3C2410_UCON, ucon);
  323. /* Unmask Tx interrupt */
  324. switch (ourport->info->type) {
  325. case TYPE_S3C6400:
  326. s3c24xx_clear_bit(port, S3C64XX_UINTM_TXD,
  327. S3C64XX_UINTM);
  328. break;
  329. case TYPE_APPLE_S5L:
  330. ucon |= APPLE_S5L_UCON_TXTHRESH_ENA_MSK;
  331. wr_regl(port, S3C2410_UCON, ucon);
  332. break;
  333. default:
  334. enable_irq(ourport->tx_irq);
  335. break;
  336. }
  337. ourport->tx_mode = S3C24XX_TX_PIO;
  338. /*
  339. * The Apple version only has edge triggered TX IRQs, so we need
  340. * to kick off the process by sending some characters here.
  341. */
  342. if (ourport->info->type == TYPE_APPLE_S5L)
  343. s3c24xx_serial_tx_chars(ourport);
  344. }
  345. static void s3c24xx_serial_start_tx_pio(struct s3c24xx_uart_port *ourport)
  346. {
  347. if (ourport->tx_mode != S3C24XX_TX_PIO)
  348. enable_tx_pio(ourport);
  349. }
  350. static int s3c24xx_serial_start_tx_dma(struct s3c24xx_uart_port *ourport,
  351. unsigned int count, unsigned int tail)
  352. {
  353. struct s3c24xx_uart_dma *dma = ourport->dma;
  354. if (ourport->tx_mode != S3C24XX_TX_DMA)
  355. enable_tx_dma(ourport);
  356. dma->tx_size = count & ~(dma_get_cache_alignment() - 1);
  357. dma->tx_transfer_addr = dma->tx_addr + tail;
  358. dma_sync_single_for_device(dma->tx_chan->device->dev,
  359. dma->tx_transfer_addr, dma->tx_size,
  360. DMA_TO_DEVICE);
  361. dma->tx_desc = dmaengine_prep_slave_single(dma->tx_chan,
  362. dma->tx_transfer_addr, dma->tx_size,
  363. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
  364. if (!dma->tx_desc) {
  365. dev_err(ourport->port.dev, "Unable to get desc for Tx\n");
  366. return -EIO;
  367. }
  368. dma->tx_desc->callback = s3c24xx_serial_tx_dma_complete;
  369. dma->tx_desc->callback_param = ourport;
  370. dma->tx_bytes_requested = dma->tx_size;
  371. ourport->tx_in_progress = S3C24XX_TX_DMA;
  372. dma->tx_cookie = dmaengine_submit(dma->tx_desc);
  373. dma_async_issue_pending(dma->tx_chan);
  374. return 0;
  375. }
  376. static void s3c24xx_serial_start_next_tx(struct s3c24xx_uart_port *ourport)
  377. {
  378. struct uart_port *port = &ourport->port;
  379. struct tty_port *tport = &port->state->port;
  380. unsigned int count, tail;
  381. /* Get data size up to the end of buffer */
  382. count = kfifo_out_linear(&tport->xmit_fifo, &tail, UART_XMIT_SIZE);
  383. if (!count) {
  384. s3c24xx_serial_stop_tx(port);
  385. return;
  386. }
  387. if (!ourport->dma || !ourport->dma->tx_chan ||
  388. count < ourport->min_dma_size ||
  389. tail & (dma_get_cache_alignment() - 1))
  390. s3c24xx_serial_start_tx_pio(ourport);
  391. else
  392. s3c24xx_serial_start_tx_dma(ourport, count, tail);
  393. }
  394. static void s3c24xx_serial_start_tx(struct uart_port *port)
  395. {
  396. struct s3c24xx_uart_port *ourport = to_ourport(port);
  397. struct tty_port *tport = &port->state->port;
  398. if (!ourport->tx_enabled) {
  399. if (port->flags & UPF_CONS_FLOW)
  400. s3c24xx_serial_rx_disable(port);
  401. ourport->tx_enabled = 1;
  402. if (!ourport->dma || !ourport->dma->tx_chan)
  403. s3c24xx_serial_start_tx_pio(ourport);
  404. }
  405. if (ourport->dma && ourport->dma->tx_chan) {
  406. if (!kfifo_is_empty(&tport->xmit_fifo) &&
  407. !ourport->tx_in_progress)
  408. s3c24xx_serial_start_next_tx(ourport);
  409. }
  410. }
  411. static void s3c24xx_uart_copy_rx_to_tty(struct s3c24xx_uart_port *ourport,
  412. struct tty_port *tty, int count)
  413. {
  414. struct s3c24xx_uart_dma *dma = ourport->dma;
  415. int copied;
  416. if (!count)
  417. return;
  418. dma_sync_single_for_cpu(dma->rx_chan->device->dev, dma->rx_addr,
  419. dma->rx_size, DMA_FROM_DEVICE);
  420. ourport->port.icount.rx += count;
  421. if (!tty) {
  422. dev_err(ourport->port.dev, "No tty port\n");
  423. return;
  424. }
  425. copied = tty_insert_flip_string(tty,
  426. ((unsigned char *)(ourport->dma->rx_buf)), count);
  427. if (copied != count) {
  428. WARN_ON(1);
  429. dev_err(ourport->port.dev, "RxData copy to tty layer failed\n");
  430. }
  431. }
  432. static void s3c24xx_serial_stop_rx(struct uart_port *port)
  433. {
  434. struct s3c24xx_uart_port *ourport = to_ourport(port);
  435. struct s3c24xx_uart_dma *dma = ourport->dma;
  436. struct tty_port *t = &port->state->port;
  437. struct dma_tx_state state;
  438. enum dma_status dma_status;
  439. unsigned int received;
  440. if (ourport->rx_enabled) {
  441. dev_dbg(port->dev, "stopping rx\n");
  442. switch (ourport->info->type) {
  443. case TYPE_S3C6400:
  444. s3c24xx_set_bit(port, S3C64XX_UINTM_RXD,
  445. S3C64XX_UINTM);
  446. break;
  447. case TYPE_APPLE_S5L:
  448. s3c24xx_clear_bit(port, APPLE_S5L_UCON_RXTHRESH_ENA, S3C2410_UCON);
  449. s3c24xx_clear_bit(port, APPLE_S5L_UCON_RXTO_ENA, S3C2410_UCON);
  450. s3c24xx_clear_bit(port, APPLE_S5L_UCON_RXTO_LEGACY_ENA, S3C2410_UCON);
  451. break;
  452. default:
  453. disable_irq_nosync(ourport->rx_irq);
  454. break;
  455. }
  456. ourport->rx_enabled = 0;
  457. }
  458. if (dma && dma->rx_chan) {
  459. dmaengine_pause(dma->tx_chan);
  460. dma_status = dmaengine_tx_status(dma->rx_chan,
  461. dma->rx_cookie, &state);
  462. if (dma_status == DMA_IN_PROGRESS ||
  463. dma_status == DMA_PAUSED) {
  464. received = dma->rx_bytes_requested - state.residue;
  465. dmaengine_terminate_all(dma->rx_chan);
  466. s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
  467. }
  468. }
  469. }
  470. static inline const struct s3c24xx_uart_info
  471. *s3c24xx_port_to_info(struct uart_port *port)
  472. {
  473. return to_ourport(port)->info;
  474. }
  475. static inline const struct s3c2410_uartcfg
  476. *s3c24xx_port_to_cfg(const struct uart_port *port)
  477. {
  478. const struct s3c24xx_uart_port *ourport;
  479. if (port->dev == NULL)
  480. return NULL;
  481. ourport = container_of(port, struct s3c24xx_uart_port, port);
  482. return ourport->cfg;
  483. }
  484. static unsigned int
  485. s3c24xx_serial_rx_fifocnt(const struct s3c24xx_uart_port *ourport, u32 ufstat)
  486. {
  487. const struct s3c24xx_uart_info *info = ourport->info;
  488. if (ufstat & info->rx_fifofull)
  489. return ourport->port.fifosize;
  490. return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
  491. }
  492. static void s3c64xx_start_rx_dma(struct s3c24xx_uart_port *ourport);
  493. static void s3c24xx_serial_rx_dma_complete(void *args)
  494. {
  495. struct s3c24xx_uart_port *ourport = args;
  496. struct uart_port *port = &ourport->port;
  497. struct s3c24xx_uart_dma *dma = ourport->dma;
  498. struct tty_port *t = &port->state->port;
  499. struct tty_struct *tty = tty_port_tty_get(&ourport->port.state->port);
  500. struct dma_tx_state state;
  501. unsigned long flags;
  502. int received;
  503. dmaengine_tx_status(dma->rx_chan, dma->rx_cookie, &state);
  504. received = dma->rx_bytes_requested - state.residue;
  505. async_tx_ack(dma->rx_desc);
  506. uart_port_lock_irqsave(port, &flags);
  507. if (received)
  508. s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
  509. if (tty) {
  510. tty_flip_buffer_push(t);
  511. tty_kref_put(tty);
  512. }
  513. s3c64xx_start_rx_dma(ourport);
  514. uart_port_unlock_irqrestore(port, flags);
  515. }
  516. static void s3c64xx_start_rx_dma(struct s3c24xx_uart_port *ourport)
  517. {
  518. struct s3c24xx_uart_dma *dma = ourport->dma;
  519. dma_sync_single_for_device(dma->rx_chan->device->dev, dma->rx_addr,
  520. dma->rx_size, DMA_FROM_DEVICE);
  521. dma->rx_desc = dmaengine_prep_slave_single(dma->rx_chan,
  522. dma->rx_addr, dma->rx_size, DMA_DEV_TO_MEM,
  523. DMA_PREP_INTERRUPT);
  524. if (!dma->rx_desc) {
  525. dev_err(ourport->port.dev, "Unable to get desc for Rx\n");
  526. return;
  527. }
  528. dma->rx_desc->callback = s3c24xx_serial_rx_dma_complete;
  529. dma->rx_desc->callback_param = ourport;
  530. dma->rx_bytes_requested = dma->rx_size;
  531. dma->rx_cookie = dmaengine_submit(dma->rx_desc);
  532. dma_async_issue_pending(dma->rx_chan);
  533. }
  534. /* ? - where has parity gone?? */
  535. #define S3C2410_UERSTAT_PARITY (0x1000)
  536. static void enable_rx_dma(struct s3c24xx_uart_port *ourport)
  537. {
  538. struct uart_port *port = &ourport->port;
  539. u32 ucon;
  540. /* set Rx mode to DMA mode */
  541. ucon = rd_regl(port, S3C2410_UCON);
  542. ucon &= ~(S3C64XX_UCON_RXBURST_MASK |
  543. S3C64XX_UCON_TIMEOUT_MASK |
  544. S3C64XX_UCON_EMPTYINT_EN |
  545. S3C64XX_UCON_DMASUS_EN |
  546. S3C64XX_UCON_TIMEOUT_EN |
  547. S3C64XX_UCON_RXMODE_MASK);
  548. ucon |= S3C64XX_UCON_RXBURST_1 |
  549. 0xf << S3C64XX_UCON_TIMEOUT_SHIFT |
  550. S3C64XX_UCON_EMPTYINT_EN |
  551. S3C64XX_UCON_TIMEOUT_EN |
  552. S3C64XX_UCON_RXMODE_DMA;
  553. wr_regl(port, S3C2410_UCON, ucon);
  554. ourport->rx_mode = S3C24XX_RX_DMA;
  555. }
  556. static void enable_rx_pio(struct s3c24xx_uart_port *ourport)
  557. {
  558. struct uart_port *port = &ourport->port;
  559. u32 ucon;
  560. /* set Rx mode to DMA mode */
  561. ucon = rd_regl(port, S3C2410_UCON);
  562. ucon &= ~S3C64XX_UCON_RXMODE_MASK;
  563. ucon |= S3C64XX_UCON_RXMODE_CPU;
  564. /* Apple types use these bits for IRQ masks */
  565. if (ourport->info->type != TYPE_APPLE_S5L) {
  566. ucon &= ~(S3C64XX_UCON_TIMEOUT_MASK |
  567. S3C64XX_UCON_EMPTYINT_EN |
  568. S3C64XX_UCON_DMASUS_EN |
  569. S3C64XX_UCON_TIMEOUT_EN);
  570. ucon |= 0xf << S3C64XX_UCON_TIMEOUT_SHIFT |
  571. S3C64XX_UCON_TIMEOUT_EN;
  572. }
  573. wr_regl(port, S3C2410_UCON, ucon);
  574. ourport->rx_mode = S3C24XX_RX_PIO;
  575. }
  576. static void s3c24xx_serial_rx_drain_fifo(struct s3c24xx_uart_port *ourport);
  577. static irqreturn_t s3c24xx_serial_rx_chars_dma(struct s3c24xx_uart_port *ourport)
  578. {
  579. struct uart_port *port = &ourport->port;
  580. struct s3c24xx_uart_dma *dma = ourport->dma;
  581. struct tty_struct *tty = tty_port_tty_get(&ourport->port.state->port);
  582. struct tty_port *t = &port->state->port;
  583. struct dma_tx_state state;
  584. unsigned int received;
  585. u32 utrstat;
  586. utrstat = rd_regl(port, S3C2410_UTRSTAT);
  587. rd_regl(port, S3C2410_UFSTAT);
  588. uart_port_lock(port);
  589. if (!(utrstat & S3C2410_UTRSTAT_TIMEOUT)) {
  590. s3c64xx_start_rx_dma(ourport);
  591. if (ourport->rx_mode == S3C24XX_RX_PIO)
  592. enable_rx_dma(ourport);
  593. goto finish;
  594. }
  595. if (ourport->rx_mode == S3C24XX_RX_DMA) {
  596. dmaengine_pause(dma->rx_chan);
  597. dmaengine_tx_status(dma->rx_chan, dma->rx_cookie, &state);
  598. dmaengine_terminate_all(dma->rx_chan);
  599. received = dma->rx_bytes_requested - state.residue;
  600. s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
  601. enable_rx_pio(ourport);
  602. }
  603. s3c24xx_serial_rx_drain_fifo(ourport);
  604. if (tty) {
  605. tty_flip_buffer_push(t);
  606. tty_kref_put(tty);
  607. }
  608. wr_regl(port, S3C2410_UTRSTAT, S3C2410_UTRSTAT_TIMEOUT);
  609. finish:
  610. uart_port_unlock(port);
  611. return IRQ_HANDLED;
  612. }
  613. static void s3c24xx_serial_rx_drain_fifo(struct s3c24xx_uart_port *ourport)
  614. {
  615. struct uart_port *port = &ourport->port;
  616. unsigned int max_count = port->fifosize;
  617. unsigned int fifocnt = 0;
  618. u32 ufcon, ufstat, uerstat;
  619. u8 ch, flag;
  620. while (max_count-- > 0) {
  621. /*
  622. * Receive all characters known to be in FIFO
  623. * before reading FIFO level again
  624. */
  625. if (fifocnt == 0) {
  626. ufstat = rd_regl(port, S3C2410_UFSTAT);
  627. fifocnt = s3c24xx_serial_rx_fifocnt(ourport, ufstat);
  628. if (fifocnt == 0)
  629. break;
  630. }
  631. fifocnt--;
  632. uerstat = rd_regl(port, S3C2410_UERSTAT);
  633. ch = rd_reg(port, S3C2410_URXH);
  634. if (port->flags & UPF_CONS_FLOW) {
  635. bool txe = s3c24xx_serial_txempty_nofifo(port);
  636. if (ourport->rx_enabled) {
  637. if (!txe) {
  638. ourport->rx_enabled = 0;
  639. continue;
  640. }
  641. } else {
  642. if (txe) {
  643. ufcon = rd_regl(port, S3C2410_UFCON);
  644. ufcon |= S3C2410_UFCON_RESETRX;
  645. wr_regl(port, S3C2410_UFCON, ufcon);
  646. ourport->rx_enabled = 1;
  647. return;
  648. }
  649. continue;
  650. }
  651. }
  652. /* insert the character into the buffer */
  653. flag = TTY_NORMAL;
  654. port->icount.rx++;
  655. if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
  656. dev_dbg(port->dev,
  657. "rxerr: port ch=0x%02x, rxs=0x%08x\n",
  658. ch, uerstat);
  659. /* check for break */
  660. if (uerstat & S3C2410_UERSTAT_BREAK) {
  661. dev_dbg(port->dev, "break!\n");
  662. port->icount.brk++;
  663. if (uart_handle_break(port))
  664. continue; /* Ignore character */
  665. }
  666. if (uerstat & S3C2410_UERSTAT_FRAME)
  667. port->icount.frame++;
  668. if (uerstat & S3C2410_UERSTAT_OVERRUN)
  669. port->icount.overrun++;
  670. uerstat &= port->read_status_mask;
  671. if (uerstat & S3C2410_UERSTAT_BREAK)
  672. flag = TTY_BREAK;
  673. else if (uerstat & S3C2410_UERSTAT_PARITY)
  674. flag = TTY_PARITY;
  675. else if (uerstat & (S3C2410_UERSTAT_FRAME |
  676. S3C2410_UERSTAT_OVERRUN))
  677. flag = TTY_FRAME;
  678. }
  679. if (uart_handle_sysrq_char(port, ch))
  680. continue; /* Ignore character */
  681. uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
  682. ch, flag);
  683. }
  684. tty_flip_buffer_push(&port->state->port);
  685. }
  686. static irqreturn_t s3c24xx_serial_rx_chars_pio(struct s3c24xx_uart_port *ourport)
  687. {
  688. struct uart_port *port = &ourport->port;
  689. uart_port_lock(port);
  690. s3c24xx_serial_rx_drain_fifo(ourport);
  691. uart_port_unlock(port);
  692. return IRQ_HANDLED;
  693. }
  694. static irqreturn_t s3c24xx_serial_rx_irq(struct s3c24xx_uart_port *ourport)
  695. {
  696. if (ourport->dma && ourport->dma->rx_chan)
  697. return s3c24xx_serial_rx_chars_dma(ourport);
  698. return s3c24xx_serial_rx_chars_pio(ourport);
  699. }
  700. static void s3c24xx_serial_tx_chars(struct s3c24xx_uart_port *ourport)
  701. {
  702. struct uart_port *port = &ourport->port;
  703. struct tty_port *tport = &port->state->port;
  704. unsigned int count, dma_count = 0, tail;
  705. count = kfifo_out_linear(&tport->xmit_fifo, &tail, UART_XMIT_SIZE);
  706. if (ourport->dma && ourport->dma->tx_chan &&
  707. count >= ourport->min_dma_size) {
  708. int align = dma_get_cache_alignment() -
  709. (tail & (dma_get_cache_alignment() - 1));
  710. if (count - align >= ourport->min_dma_size) {
  711. dma_count = count - align;
  712. count = align;
  713. tail += align;
  714. }
  715. }
  716. if (port->x_char) {
  717. wr_reg(port, S3C2410_UTXH, port->x_char);
  718. port->icount.tx++;
  719. port->x_char = 0;
  720. return;
  721. }
  722. /* if there isn't anything more to transmit, or the uart is now
  723. * stopped, disable the uart and exit
  724. */
  725. if (kfifo_is_empty(&tport->xmit_fifo) || uart_tx_stopped(port)) {
  726. s3c24xx_serial_stop_tx(port);
  727. return;
  728. }
  729. /* try and drain the buffer... */
  730. if (count > port->fifosize) {
  731. count = port->fifosize;
  732. dma_count = 0;
  733. }
  734. while (!(rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)) {
  735. unsigned char ch;
  736. if (!uart_fifo_get(port, &ch))
  737. break;
  738. wr_reg(port, S3C2410_UTXH, ch);
  739. count--;
  740. }
  741. if (!count && dma_count) {
  742. s3c24xx_serial_start_tx_dma(ourport, dma_count, tail);
  743. return;
  744. }
  745. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  746. uart_write_wakeup(port);
  747. if (kfifo_is_empty(&tport->xmit_fifo))
  748. s3c24xx_serial_stop_tx(port);
  749. }
  750. static irqreturn_t s3c24xx_serial_tx_irq(struct s3c24xx_uart_port *ourport)
  751. {
  752. struct uart_port *port = &ourport->port;
  753. uart_port_lock(port);
  754. s3c24xx_serial_tx_chars(ourport);
  755. uart_port_unlock(port);
  756. return IRQ_HANDLED;
  757. }
  758. /* interrupt handler for s3c64xx and later SoC's.*/
  759. static irqreturn_t s3c64xx_serial_handle_irq(int irq, void *id)
  760. {
  761. struct s3c24xx_uart_port *ourport = id;
  762. const struct uart_port *port = &ourport->port;
  763. u32 pend = rd_regl(port, S3C64XX_UINTP);
  764. irqreturn_t ret = IRQ_HANDLED;
  765. if (pend & S3C64XX_UINTM_RXD_MSK) {
  766. ret = s3c24xx_serial_rx_irq(ourport);
  767. wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_RXD_MSK);
  768. }
  769. if (pend & S3C64XX_UINTM_TXD_MSK) {
  770. ret = s3c24xx_serial_tx_irq(ourport);
  771. wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_TXD_MSK);
  772. }
  773. return ret;
  774. }
  775. /* interrupt handler for Apple SoC's.*/
  776. static irqreturn_t apple_serial_handle_irq(int irq, void *id)
  777. {
  778. struct s3c24xx_uart_port *ourport = id;
  779. const struct uart_port *port = &ourport->port;
  780. u32 pend = rd_regl(port, S3C2410_UTRSTAT);
  781. irqreturn_t ret = IRQ_NONE;
  782. if (pend & (APPLE_S5L_UTRSTAT_RXTHRESH | APPLE_S5L_UTRSTAT_RXTO |
  783. APPLE_S5L_UTRSTAT_RXTO_LEGACY)) {
  784. wr_regl(port, S3C2410_UTRSTAT,
  785. APPLE_S5L_UTRSTAT_RXTHRESH | APPLE_S5L_UTRSTAT_RXTO |
  786. APPLE_S5L_UTRSTAT_RXTO_LEGACY);
  787. ret = s3c24xx_serial_rx_irq(ourport);
  788. }
  789. if (pend & APPLE_S5L_UTRSTAT_TXTHRESH) {
  790. wr_regl(port, S3C2410_UTRSTAT, APPLE_S5L_UTRSTAT_TXTHRESH);
  791. ret = s3c24xx_serial_tx_irq(ourport);
  792. }
  793. return ret;
  794. }
  795. static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
  796. {
  797. const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  798. u32 ufstat = rd_regl(port, S3C2410_UFSTAT);
  799. u32 ufcon = rd_regl(port, S3C2410_UFCON);
  800. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  801. if ((ufstat & info->tx_fifomask) ||
  802. (ufstat & info->tx_fifofull))
  803. return 0;
  804. return TIOCSER_TEMT;
  805. }
  806. return s3c24xx_serial_txempty_nofifo(port) ? TIOCSER_TEMT : 0;
  807. }
  808. /* no modem control lines */
  809. static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
  810. {
  811. u32 umstat = rd_reg(port, S3C2410_UMSTAT);
  812. if (umstat & S3C2410_UMSTAT_CTS)
  813. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  814. else
  815. return TIOCM_CAR | TIOCM_DSR;
  816. }
  817. static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  818. {
  819. u32 umcon = rd_regl(port, S3C2410_UMCON);
  820. u32 ucon = rd_regl(port, S3C2410_UCON);
  821. if (mctrl & TIOCM_RTS)
  822. umcon |= S3C2410_UMCOM_RTS_LOW;
  823. else
  824. umcon &= ~S3C2410_UMCOM_RTS_LOW;
  825. wr_regl(port, S3C2410_UMCON, umcon);
  826. if (mctrl & TIOCM_LOOP)
  827. ucon |= S3C2410_UCON_LOOPBACK;
  828. else
  829. ucon &= ~S3C2410_UCON_LOOPBACK;
  830. wr_regl(port, S3C2410_UCON, ucon);
  831. }
  832. static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
  833. {
  834. unsigned long flags;
  835. u32 ucon;
  836. uart_port_lock_irqsave(port, &flags);
  837. ucon = rd_regl(port, S3C2410_UCON);
  838. if (break_state)
  839. ucon |= S3C2410_UCON_SBREAK;
  840. else
  841. ucon &= ~S3C2410_UCON_SBREAK;
  842. wr_regl(port, S3C2410_UCON, ucon);
  843. uart_port_unlock_irqrestore(port, flags);
  844. }
  845. static int s3c24xx_serial_request_dma(struct s3c24xx_uart_port *p)
  846. {
  847. struct s3c24xx_uart_dma *dma = p->dma;
  848. struct dma_slave_caps dma_caps;
  849. const char *reason = NULL;
  850. int ret;
  851. /* Default slave configuration parameters */
  852. dma->rx_conf.direction = DMA_DEV_TO_MEM;
  853. dma->rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  854. dma->rx_conf.src_addr = p->port.mapbase + S3C2410_URXH;
  855. dma->rx_conf.src_maxburst = 1;
  856. dma->tx_conf.direction = DMA_MEM_TO_DEV;
  857. dma->tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  858. dma->tx_conf.dst_addr = p->port.mapbase + S3C2410_UTXH;
  859. dma->tx_conf.dst_maxburst = 1;
  860. dma->rx_chan = dma_request_chan(p->port.dev, "rx");
  861. if (IS_ERR(dma->rx_chan)) {
  862. reason = "DMA RX channel request failed";
  863. ret = PTR_ERR(dma->rx_chan);
  864. goto err_warn;
  865. }
  866. ret = dma_get_slave_caps(dma->rx_chan, &dma_caps);
  867. if (ret < 0 ||
  868. dma_caps.residue_granularity < DMA_RESIDUE_GRANULARITY_BURST) {
  869. reason = "insufficient DMA RX engine capabilities";
  870. ret = -EOPNOTSUPP;
  871. goto err_release_rx;
  872. }
  873. dmaengine_slave_config(dma->rx_chan, &dma->rx_conf);
  874. dma->tx_chan = dma_request_chan(p->port.dev, "tx");
  875. if (IS_ERR(dma->tx_chan)) {
  876. reason = "DMA TX channel request failed";
  877. ret = PTR_ERR(dma->tx_chan);
  878. goto err_release_rx;
  879. }
  880. ret = dma_get_slave_caps(dma->tx_chan, &dma_caps);
  881. if (ret < 0 ||
  882. dma_caps.residue_granularity < DMA_RESIDUE_GRANULARITY_BURST) {
  883. reason = "insufficient DMA TX engine capabilities";
  884. ret = -EOPNOTSUPP;
  885. goto err_release_tx;
  886. }
  887. dmaengine_slave_config(dma->tx_chan, &dma->tx_conf);
  888. /* RX buffer */
  889. dma->rx_size = PAGE_SIZE;
  890. dma->rx_buf = kmalloc(dma->rx_size, GFP_KERNEL);
  891. if (!dma->rx_buf) {
  892. ret = -ENOMEM;
  893. goto err_release_tx;
  894. }
  895. dma->rx_addr = dma_map_single(dma->rx_chan->device->dev, dma->rx_buf,
  896. dma->rx_size, DMA_FROM_DEVICE);
  897. if (dma_mapping_error(dma->rx_chan->device->dev, dma->rx_addr)) {
  898. reason = "DMA mapping error for RX buffer";
  899. ret = -EIO;
  900. goto err_free_rx;
  901. }
  902. /* TX buffer */
  903. dma->tx_addr = dma_map_single(dma->tx_chan->device->dev,
  904. p->port.state->port.xmit_buf,
  905. UART_XMIT_SIZE,
  906. DMA_TO_DEVICE);
  907. if (dma_mapping_error(dma->tx_chan->device->dev, dma->tx_addr)) {
  908. reason = "DMA mapping error for TX buffer";
  909. ret = -EIO;
  910. goto err_unmap_rx;
  911. }
  912. return 0;
  913. err_unmap_rx:
  914. dma_unmap_single(dma->rx_chan->device->dev, dma->rx_addr,
  915. dma->rx_size, DMA_FROM_DEVICE);
  916. err_free_rx:
  917. kfree(dma->rx_buf);
  918. err_release_tx:
  919. dma_release_channel(dma->tx_chan);
  920. err_release_rx:
  921. dma_release_channel(dma->rx_chan);
  922. err_warn:
  923. if (reason)
  924. dev_warn(p->port.dev, "%s, DMA will not be used\n", reason);
  925. return ret;
  926. }
  927. static void s3c24xx_serial_release_dma(struct s3c24xx_uart_port *p)
  928. {
  929. struct s3c24xx_uart_dma *dma = p->dma;
  930. if (dma->rx_chan) {
  931. dmaengine_terminate_all(dma->rx_chan);
  932. dma_unmap_single(dma->rx_chan->device->dev, dma->rx_addr,
  933. dma->rx_size, DMA_FROM_DEVICE);
  934. kfree(dma->rx_buf);
  935. dma_release_channel(dma->rx_chan);
  936. dma->rx_chan = NULL;
  937. }
  938. if (dma->tx_chan) {
  939. dmaengine_terminate_all(dma->tx_chan);
  940. dma_unmap_single(dma->tx_chan->device->dev, dma->tx_addr,
  941. UART_XMIT_SIZE, DMA_TO_DEVICE);
  942. dma_release_channel(dma->tx_chan);
  943. dma->tx_chan = NULL;
  944. }
  945. }
  946. static void s3c64xx_serial_shutdown(struct uart_port *port)
  947. {
  948. struct s3c24xx_uart_port *ourport = to_ourport(port);
  949. ourport->tx_enabled = 0;
  950. ourport->tx_mode = 0;
  951. ourport->rx_enabled = 0;
  952. free_irq(port->irq, ourport);
  953. wr_regl(port, S3C64XX_UINTP, 0xf);
  954. wr_regl(port, S3C64XX_UINTM, 0xf);
  955. if (ourport->dma)
  956. s3c24xx_serial_release_dma(ourport);
  957. ourport->tx_in_progress = 0;
  958. }
  959. static void apple_s5l_serial_shutdown(struct uart_port *port)
  960. {
  961. struct s3c24xx_uart_port *ourport = to_ourport(port);
  962. u32 ucon;
  963. ucon = rd_regl(port, S3C2410_UCON);
  964. ucon &= ~(APPLE_S5L_UCON_TXTHRESH_ENA_MSK |
  965. APPLE_S5L_UCON_RXTHRESH_ENA_MSK |
  966. APPLE_S5L_UCON_RXTO_ENA_MSK |
  967. APPLE_S5L_UCON_RXTO_LEGACY_ENA_MSK);
  968. wr_regl(port, S3C2410_UCON, ucon);
  969. wr_regl(port, S3C2410_UTRSTAT, APPLE_S5L_UTRSTAT_ALL_FLAGS);
  970. free_irq(port->irq, ourport);
  971. ourport->tx_enabled = 0;
  972. ourport->tx_mode = 0;
  973. ourport->rx_enabled = 0;
  974. if (ourport->dma)
  975. s3c24xx_serial_release_dma(ourport);
  976. ourport->tx_in_progress = 0;
  977. }
  978. static int s3c64xx_serial_startup(struct uart_port *port)
  979. {
  980. struct s3c24xx_uart_port *ourport = to_ourport(port);
  981. unsigned long flags;
  982. u32 ufcon;
  983. int ret;
  984. wr_regl(port, S3C64XX_UINTM, 0xf);
  985. if (ourport->dma) {
  986. ret = s3c24xx_serial_request_dma(ourport);
  987. if (ret < 0) {
  988. devm_kfree(port->dev, ourport->dma);
  989. ourport->dma = NULL;
  990. }
  991. }
  992. ret = request_irq(port->irq, s3c64xx_serial_handle_irq, IRQF_SHARED,
  993. s3c24xx_serial_portname(port), ourport);
  994. if (ret) {
  995. dev_err(port->dev, "cannot get irq %d\n", port->irq);
  996. return ret;
  997. }
  998. /* For compatibility with s3c24xx Soc's */
  999. ourport->rx_enabled = 1;
  1000. ourport->tx_enabled = 0;
  1001. uart_port_lock_irqsave(port, &flags);
  1002. ufcon = rd_regl(port, S3C2410_UFCON);
  1003. ufcon |= S3C2410_UFCON_RESETRX | S5PV210_UFCON_RXTRIG8;
  1004. if (!uart_console(port))
  1005. ufcon |= S3C2410_UFCON_RESETTX;
  1006. wr_regl(port, S3C2410_UFCON, ufcon);
  1007. enable_rx_pio(ourport);
  1008. uart_port_unlock_irqrestore(port, flags);
  1009. /* Enable Rx Interrupt */
  1010. s3c24xx_clear_bit(port, S3C64XX_UINTM_RXD, S3C64XX_UINTM);
  1011. return ret;
  1012. }
  1013. static int apple_s5l_serial_startup(struct uart_port *port)
  1014. {
  1015. struct s3c24xx_uart_port *ourport = to_ourport(port);
  1016. unsigned long flags;
  1017. u32 ufcon;
  1018. int ret;
  1019. wr_regl(port, S3C2410_UTRSTAT, APPLE_S5L_UTRSTAT_ALL_FLAGS);
  1020. ret = request_irq(port->irq, apple_serial_handle_irq, 0,
  1021. s3c24xx_serial_portname(port), ourport);
  1022. if (ret) {
  1023. dev_err(port->dev, "cannot get irq %d\n", port->irq);
  1024. return ret;
  1025. }
  1026. /* For compatibility with s3c24xx Soc's */
  1027. ourport->rx_enabled = 1;
  1028. ourport->tx_enabled = 0;
  1029. uart_port_lock_irqsave(port, &flags);
  1030. ufcon = rd_regl(port, S3C2410_UFCON);
  1031. ufcon |= S3C2410_UFCON_RESETRX | S5PV210_UFCON_RXTRIG8;
  1032. if (!uart_console(port))
  1033. ufcon |= S3C2410_UFCON_RESETTX;
  1034. wr_regl(port, S3C2410_UFCON, ufcon);
  1035. enable_rx_pio(ourport);
  1036. uart_port_unlock_irqrestore(port, flags);
  1037. /* Enable Rx Interrupt */
  1038. s3c24xx_set_bit(port, APPLE_S5L_UCON_RXTHRESH_ENA, S3C2410_UCON);
  1039. s3c24xx_set_bit(port, APPLE_S5L_UCON_RXTO_ENA, S3C2410_UCON);
  1040. s3c24xx_set_bit(port, APPLE_S5L_UCON_RXTO_LEGACY_ENA, S3C2410_UCON);
  1041. return ret;
  1042. }
  1043. static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
  1044. unsigned int old)
  1045. {
  1046. struct s3c24xx_uart_port *ourport = to_ourport(port);
  1047. int timeout = 10000;
  1048. ourport->pm_level = level;
  1049. switch (level) {
  1050. case 3:
  1051. while (--timeout && !s3c24xx_serial_txempty_nofifo(port))
  1052. udelay(100);
  1053. if (!IS_ERR(ourport->baudclk))
  1054. clk_disable_unprepare(ourport->baudclk);
  1055. clk_disable_unprepare(ourport->clk);
  1056. break;
  1057. case 0:
  1058. clk_prepare_enable(ourport->clk);
  1059. if (!IS_ERR(ourport->baudclk))
  1060. clk_prepare_enable(ourport->baudclk);
  1061. break;
  1062. default:
  1063. dev_err(port->dev, "s3c24xx_serial: unknown pm %d\n", level);
  1064. }
  1065. }
  1066. /* baud rate calculation
  1067. *
  1068. * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
  1069. * of different sources, including the peripheral clock ("pclk") and an
  1070. * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
  1071. * with a programmable extra divisor.
  1072. *
  1073. * The following code goes through the clock sources, and calculates the
  1074. * baud clocks (and the resultant actual baud rates) and then tries to
  1075. * pick the closest one and select that.
  1076. *
  1077. */
  1078. #define MAX_CLK_NAME_LENGTH 15
  1079. static inline u8 s3c24xx_serial_getsource(struct uart_port *port)
  1080. {
  1081. const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1082. u32 ucon;
  1083. if (info->num_clks == 1)
  1084. return 0;
  1085. ucon = rd_regl(port, S3C2410_UCON);
  1086. ucon &= info->clksel_mask;
  1087. return ucon >> info->clksel_shift;
  1088. }
  1089. static void s3c24xx_serial_setsource(struct uart_port *port, u8 clk_sel)
  1090. {
  1091. const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1092. u32 ucon;
  1093. if (info->num_clks == 1)
  1094. return;
  1095. ucon = rd_regl(port, S3C2410_UCON);
  1096. if ((ucon & info->clksel_mask) >> info->clksel_shift == clk_sel)
  1097. return;
  1098. ucon &= ~info->clksel_mask;
  1099. ucon |= clk_sel << info->clksel_shift;
  1100. wr_regl(port, S3C2410_UCON, ucon);
  1101. }
  1102. static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
  1103. unsigned int req_baud, struct clk **best_clk,
  1104. u8 *clk_num)
  1105. {
  1106. const struct s3c24xx_uart_info *info = ourport->info;
  1107. struct clk *clk;
  1108. unsigned long rate;
  1109. unsigned int baud, quot, best_quot = 0;
  1110. char clkname[MAX_CLK_NAME_LENGTH];
  1111. int calc_deviation, deviation = (1 << 30) - 1;
  1112. u8 cnt;
  1113. for (cnt = 0; cnt < info->num_clks; cnt++) {
  1114. /* Keep selected clock if provided */
  1115. if (ourport->cfg->clk_sel &&
  1116. !(ourport->cfg->clk_sel & (1 << cnt)))
  1117. continue;
  1118. sprintf(clkname, "clk_uart_baud%d", cnt);
  1119. clk = clk_get(ourport->port.dev, clkname);
  1120. if (IS_ERR(clk))
  1121. continue;
  1122. rate = clk_get_rate(clk);
  1123. if (!rate) {
  1124. dev_err(ourport->port.dev,
  1125. "Failed to get clock rate for %s.\n", clkname);
  1126. clk_put(clk);
  1127. continue;
  1128. }
  1129. if (ourport->info->has_divslot) {
  1130. unsigned long div = rate / req_baud;
  1131. /* The UDIVSLOT register on the newer UARTs allows us to
  1132. * get a divisor adjustment of 1/16th on the baud clock.
  1133. *
  1134. * We don't keep the UDIVSLOT value (the 16ths we
  1135. * calculated by not multiplying the baud by 16) as it
  1136. * is easy enough to recalculate.
  1137. */
  1138. quot = div / 16;
  1139. baud = rate / div;
  1140. } else {
  1141. quot = (rate + (8 * req_baud)) / (16 * req_baud);
  1142. baud = rate / (quot * 16);
  1143. }
  1144. quot--;
  1145. calc_deviation = abs(req_baud - baud);
  1146. if (calc_deviation < deviation) {
  1147. /*
  1148. * If we find a better clk, release the previous one, if
  1149. * any.
  1150. */
  1151. if (!IS_ERR(*best_clk))
  1152. clk_put(*best_clk);
  1153. *best_clk = clk;
  1154. best_quot = quot;
  1155. *clk_num = cnt;
  1156. deviation = calc_deviation;
  1157. } else {
  1158. clk_put(clk);
  1159. }
  1160. }
  1161. return best_quot;
  1162. }
  1163. /* udivslot_table[]
  1164. *
  1165. * This table takes the fractional value of the baud divisor and gives
  1166. * the recommended setting for the UDIVSLOT register.
  1167. */
  1168. static const u16 udivslot_table[16] = {
  1169. [0] = 0x0000,
  1170. [1] = 0x0080,
  1171. [2] = 0x0808,
  1172. [3] = 0x0888,
  1173. [4] = 0x2222,
  1174. [5] = 0x4924,
  1175. [6] = 0x4A52,
  1176. [7] = 0x54AA,
  1177. [8] = 0x5555,
  1178. [9] = 0xD555,
  1179. [10] = 0xD5D5,
  1180. [11] = 0xDDD5,
  1181. [12] = 0xDDDD,
  1182. [13] = 0xDFDD,
  1183. [14] = 0xDFDF,
  1184. [15] = 0xFFDF,
  1185. };
  1186. static void s3c24xx_serial_set_termios(struct uart_port *port,
  1187. struct ktermios *termios,
  1188. const struct ktermios *old)
  1189. {
  1190. const struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
  1191. struct s3c24xx_uart_port *ourport = to_ourport(port);
  1192. struct clk *clk = ERR_PTR(-EINVAL);
  1193. unsigned long flags;
  1194. unsigned int baud, quot;
  1195. unsigned int udivslot = 0;
  1196. u32 ulcon, umcon;
  1197. u8 clk_sel = 0;
  1198. /*
  1199. * We don't support modem control lines.
  1200. */
  1201. termios->c_cflag &= ~(HUPCL | CMSPAR);
  1202. termios->c_cflag |= CLOCAL;
  1203. /*
  1204. * Ask the core to calculate the divisor for us.
  1205. */
  1206. baud = uart_get_baud_rate(port, termios, old, 0, 3000000);
  1207. quot = s3c24xx_serial_getclk(ourport, baud, &clk, &clk_sel);
  1208. if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
  1209. quot = port->custom_divisor;
  1210. if (IS_ERR(clk))
  1211. return;
  1212. /* check to see if we need to change clock source */
  1213. if (ourport->baudclk != clk) {
  1214. clk_prepare_enable(clk);
  1215. s3c24xx_serial_setsource(port, clk_sel);
  1216. if (!IS_ERR(ourport->baudclk)) {
  1217. clk_disable_unprepare(ourport->baudclk);
  1218. ourport->baudclk = ERR_PTR(-EINVAL);
  1219. }
  1220. ourport->baudclk = clk;
  1221. ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
  1222. }
  1223. if (ourport->info->has_divslot) {
  1224. unsigned int div = ourport->baudclk_rate / baud;
  1225. if (cfg->has_fracval) {
  1226. udivslot = (div & 15);
  1227. dev_dbg(port->dev, "fracval = %04x\n", udivslot);
  1228. } else {
  1229. udivslot = udivslot_table[div & 15];
  1230. dev_dbg(port->dev, "udivslot = %04x (div %d)\n",
  1231. udivslot, div & 15);
  1232. }
  1233. }
  1234. switch (termios->c_cflag & CSIZE) {
  1235. case CS5:
  1236. dev_dbg(port->dev, "config: 5bits/char\n");
  1237. ulcon = S3C2410_LCON_CS5;
  1238. break;
  1239. case CS6:
  1240. dev_dbg(port->dev, "config: 6bits/char\n");
  1241. ulcon = S3C2410_LCON_CS6;
  1242. break;
  1243. case CS7:
  1244. dev_dbg(port->dev, "config: 7bits/char\n");
  1245. ulcon = S3C2410_LCON_CS7;
  1246. break;
  1247. case CS8:
  1248. default:
  1249. dev_dbg(port->dev, "config: 8bits/char\n");
  1250. ulcon = S3C2410_LCON_CS8;
  1251. break;
  1252. }
  1253. /* preserve original lcon IR settings */
  1254. ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
  1255. if (termios->c_cflag & CSTOPB)
  1256. ulcon |= S3C2410_LCON_STOPB;
  1257. if (termios->c_cflag & PARENB) {
  1258. if (termios->c_cflag & PARODD)
  1259. ulcon |= S3C2410_LCON_PODD;
  1260. else
  1261. ulcon |= S3C2410_LCON_PEVEN;
  1262. } else {
  1263. ulcon |= S3C2410_LCON_PNONE;
  1264. }
  1265. uart_port_lock_irqsave(port, &flags);
  1266. dev_dbg(port->dev,
  1267. "setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
  1268. ulcon, quot, udivslot);
  1269. wr_regl(port, S3C2410_ULCON, ulcon);
  1270. wr_regl(port, S3C2410_UBRDIV, quot);
  1271. port->status &= ~UPSTAT_AUTOCTS;
  1272. umcon = rd_regl(port, S3C2410_UMCON);
  1273. if (termios->c_cflag & CRTSCTS) {
  1274. umcon |= S3C2410_UMCOM_AFC;
  1275. /* Disable RTS when RX FIFO contains 63 bytes */
  1276. umcon &= ~S3C2412_UMCON_AFC_8;
  1277. port->status = UPSTAT_AUTOCTS;
  1278. } else {
  1279. umcon &= ~S3C2410_UMCOM_AFC;
  1280. }
  1281. wr_regl(port, S3C2410_UMCON, umcon);
  1282. if (ourport->info->has_divslot)
  1283. wr_regl(port, S3C2443_DIVSLOT, udivslot);
  1284. dev_dbg(port->dev,
  1285. "uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
  1286. rd_regl(port, S3C2410_ULCON),
  1287. rd_regl(port, S3C2410_UCON),
  1288. rd_regl(port, S3C2410_UFCON));
  1289. /*
  1290. * Update the per-port timeout.
  1291. */
  1292. uart_update_timeout(port, termios->c_cflag, baud);
  1293. /*
  1294. * Which character status flags are we interested in?
  1295. */
  1296. port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
  1297. if (termios->c_iflag & INPCK)
  1298. port->read_status_mask |= S3C2410_UERSTAT_FRAME |
  1299. S3C2410_UERSTAT_PARITY;
  1300. /*
  1301. * Which character status flags should we ignore?
  1302. */
  1303. port->ignore_status_mask = 0;
  1304. if (termios->c_iflag & IGNPAR)
  1305. port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
  1306. if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
  1307. port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
  1308. /*
  1309. * Ignore all characters if CREAD is not set.
  1310. */
  1311. if ((termios->c_cflag & CREAD) == 0)
  1312. port->ignore_status_mask |= RXSTAT_DUMMY_READ;
  1313. uart_port_unlock_irqrestore(port, flags);
  1314. }
  1315. static const char *s3c24xx_serial_type(struct uart_port *port)
  1316. {
  1317. const struct s3c24xx_uart_port *ourport = to_ourport(port);
  1318. switch (ourport->info->type) {
  1319. case TYPE_S3C6400:
  1320. return "S3C6400/10";
  1321. case TYPE_APPLE_S5L:
  1322. return "APPLE S5L";
  1323. default:
  1324. return NULL;
  1325. }
  1326. }
  1327. static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
  1328. {
  1329. const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1330. if (flags & UART_CONFIG_TYPE)
  1331. port->type = info->port_type;
  1332. }
  1333. /*
  1334. * verify the new serial_struct (for TIOCSSERIAL).
  1335. */
  1336. static int
  1337. s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  1338. {
  1339. const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1340. if (ser->type != PORT_UNKNOWN && ser->type != info->port_type)
  1341. return -EINVAL;
  1342. return 0;
  1343. }
  1344. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  1345. static struct console s3c24xx_serial_console;
  1346. static void __init s3c24xx_serial_register_console(void)
  1347. {
  1348. register_console(&s3c24xx_serial_console);
  1349. }
  1350. static void s3c24xx_serial_unregister_console(void)
  1351. {
  1352. if (console_is_registered(&s3c24xx_serial_console))
  1353. unregister_console(&s3c24xx_serial_console);
  1354. }
  1355. #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
  1356. #else
  1357. static inline void s3c24xx_serial_register_console(void) { }
  1358. static inline void s3c24xx_serial_unregister_console(void) { }
  1359. #define S3C24XX_SERIAL_CONSOLE NULL
  1360. #endif
  1361. #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
  1362. static int s3c24xx_serial_get_poll_char(struct uart_port *port);
  1363. static void s3c24xx_serial_put_poll_char(struct uart_port *port,
  1364. unsigned char c);
  1365. #endif
  1366. static const struct uart_ops s3c64xx_serial_ops = {
  1367. .pm = s3c24xx_serial_pm,
  1368. .tx_empty = s3c24xx_serial_tx_empty,
  1369. .get_mctrl = s3c24xx_serial_get_mctrl,
  1370. .set_mctrl = s3c24xx_serial_set_mctrl,
  1371. .stop_tx = s3c24xx_serial_stop_tx,
  1372. .start_tx = s3c24xx_serial_start_tx,
  1373. .stop_rx = s3c24xx_serial_stop_rx,
  1374. .break_ctl = s3c24xx_serial_break_ctl,
  1375. .startup = s3c64xx_serial_startup,
  1376. .shutdown = s3c64xx_serial_shutdown,
  1377. .set_termios = s3c24xx_serial_set_termios,
  1378. .type = s3c24xx_serial_type,
  1379. .config_port = s3c24xx_serial_config_port,
  1380. .verify_port = s3c24xx_serial_verify_port,
  1381. #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
  1382. .poll_get_char = s3c24xx_serial_get_poll_char,
  1383. .poll_put_char = s3c24xx_serial_put_poll_char,
  1384. #endif
  1385. };
  1386. static const struct uart_ops apple_s5l_serial_ops = {
  1387. .pm = s3c24xx_serial_pm,
  1388. .tx_empty = s3c24xx_serial_tx_empty,
  1389. .get_mctrl = s3c24xx_serial_get_mctrl,
  1390. .set_mctrl = s3c24xx_serial_set_mctrl,
  1391. .stop_tx = s3c24xx_serial_stop_tx,
  1392. .start_tx = s3c24xx_serial_start_tx,
  1393. .stop_rx = s3c24xx_serial_stop_rx,
  1394. .break_ctl = s3c24xx_serial_break_ctl,
  1395. .startup = apple_s5l_serial_startup,
  1396. .shutdown = apple_s5l_serial_shutdown,
  1397. .set_termios = s3c24xx_serial_set_termios,
  1398. .type = s3c24xx_serial_type,
  1399. .config_port = s3c24xx_serial_config_port,
  1400. .verify_port = s3c24xx_serial_verify_port,
  1401. #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
  1402. .poll_get_char = s3c24xx_serial_get_poll_char,
  1403. .poll_put_char = s3c24xx_serial_put_poll_char,
  1404. #endif
  1405. };
  1406. static struct uart_driver s3c24xx_uart_drv = {
  1407. .owner = THIS_MODULE,
  1408. .driver_name = "s3c2410_serial",
  1409. .nr = UART_NR,
  1410. .cons = S3C24XX_SERIAL_CONSOLE,
  1411. .dev_name = S3C24XX_SERIAL_NAME,
  1412. .major = S3C24XX_SERIAL_MAJOR,
  1413. .minor = S3C24XX_SERIAL_MINOR,
  1414. };
  1415. static struct s3c24xx_uart_port s3c24xx_serial_ports[UART_NR];
  1416. static void s3c24xx_serial_init_port_default(int index)
  1417. {
  1418. struct uart_port *port = &s3c24xx_serial_ports[index].port;
  1419. spin_lock_init(&port->lock);
  1420. port->uartclk = 0;
  1421. port->fifosize = 16;
  1422. port->flags = UPF_BOOT_AUTOCONF;
  1423. port->line = index;
  1424. }
  1425. /* s3c24xx_serial_resetport
  1426. *
  1427. * reset the fifos and other the settings.
  1428. */
  1429. static void s3c24xx_serial_resetport(struct uart_port *port,
  1430. const struct s3c2410_uartcfg *cfg)
  1431. {
  1432. const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1433. u32 ucon = rd_regl(port, S3C2410_UCON);
  1434. ucon &= (info->clksel_mask | info->ucon_mask);
  1435. wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
  1436. /* reset both fifos */
  1437. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  1438. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  1439. /* some delay is required after fifo reset */
  1440. udelay(1);
  1441. }
  1442. static int s3c24xx_serial_enable_baudclk(struct s3c24xx_uart_port *ourport)
  1443. {
  1444. struct device *dev = ourport->port.dev;
  1445. const struct s3c24xx_uart_info *info = ourport->info;
  1446. char clk_name[MAX_CLK_NAME_LENGTH];
  1447. struct clk *clk;
  1448. int ret;
  1449. u8 clk_sel, clk_num;
  1450. clk_sel = ourport->cfg->clk_sel ? : info->def_clk_sel;
  1451. for (clk_num = 0; clk_num < info->num_clks; clk_num++) {
  1452. if (!(clk_sel & (1 << clk_num)))
  1453. continue;
  1454. sprintf(clk_name, "clk_uart_baud%d", clk_num);
  1455. clk = clk_get(dev, clk_name);
  1456. if (IS_ERR(clk))
  1457. continue;
  1458. ret = clk_prepare_enable(clk);
  1459. if (ret) {
  1460. clk_put(clk);
  1461. continue;
  1462. }
  1463. ourport->baudclk = clk;
  1464. ourport->baudclk_rate = clk_get_rate(clk);
  1465. s3c24xx_serial_setsource(&ourport->port, clk_num);
  1466. return 0;
  1467. }
  1468. return -EINVAL;
  1469. }
  1470. /* s3c24xx_serial_init_port
  1471. *
  1472. * initialise a single serial port from the platform device given
  1473. */
  1474. static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
  1475. struct platform_device *platdev)
  1476. {
  1477. struct uart_port *port = &ourport->port;
  1478. const struct s3c2410_uartcfg *cfg = ourport->cfg;
  1479. struct resource *res;
  1480. int ret;
  1481. if (platdev == NULL)
  1482. return -ENODEV;
  1483. if (port->mapbase != 0)
  1484. return -EINVAL;
  1485. /* setup info for port */
  1486. port->dev = &platdev->dev;
  1487. port->uartclk = 1;
  1488. if (cfg->uart_flags & UPF_CONS_FLOW) {
  1489. dev_dbg(port->dev, "enabling flow control\n");
  1490. port->flags |= UPF_CONS_FLOW;
  1491. }
  1492. /* sort our the physical and virtual addresses for each UART */
  1493. res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
  1494. if (res == NULL) {
  1495. dev_err(port->dev, "failed to find memory resource for uart\n");
  1496. return -EINVAL;
  1497. }
  1498. dev_dbg(port->dev, "resource %pR)\n", res);
  1499. port->membase = devm_ioremap_resource(port->dev, res);
  1500. if (IS_ERR(port->membase)) {
  1501. dev_err(port->dev, "failed to remap controller address\n");
  1502. return -EBUSY;
  1503. }
  1504. port->mapbase = res->start;
  1505. ret = platform_get_irq(platdev, 0);
  1506. if (ret < 0) {
  1507. port->irq = 0;
  1508. } else {
  1509. port->irq = ret;
  1510. ourport->rx_irq = ret;
  1511. ourport->tx_irq = ret + 1;
  1512. }
  1513. /*
  1514. * DMA is currently supported only on DT platforms, if DMA properties
  1515. * are specified.
  1516. */
  1517. if (platdev->dev.of_node && of_find_property(platdev->dev.of_node,
  1518. "dmas", NULL)) {
  1519. ourport->dma = devm_kzalloc(port->dev,
  1520. sizeof(*ourport->dma),
  1521. GFP_KERNEL);
  1522. if (!ourport->dma) {
  1523. ret = -ENOMEM;
  1524. goto err;
  1525. }
  1526. }
  1527. ourport->clk = clk_get(&platdev->dev, "uart");
  1528. if (IS_ERR(ourport->clk)) {
  1529. pr_err("%s: Controller clock not found\n",
  1530. dev_name(&platdev->dev));
  1531. ret = PTR_ERR(ourport->clk);
  1532. goto err;
  1533. }
  1534. ret = clk_prepare_enable(ourport->clk);
  1535. if (ret) {
  1536. pr_err("uart: clock failed to prepare+enable: %d\n", ret);
  1537. clk_put(ourport->clk);
  1538. goto err;
  1539. }
  1540. ret = s3c24xx_serial_enable_baudclk(ourport);
  1541. if (ret)
  1542. pr_warn("uart: failed to enable baudclk\n");
  1543. /* Keep all interrupts masked and cleared */
  1544. switch (ourport->info->type) {
  1545. case TYPE_S3C6400:
  1546. wr_regl(port, S3C64XX_UINTM, 0xf);
  1547. wr_regl(port, S3C64XX_UINTP, 0xf);
  1548. wr_regl(port, S3C64XX_UINTSP, 0xf);
  1549. break;
  1550. case TYPE_APPLE_S5L: {
  1551. u32 ucon;
  1552. ucon = rd_regl(port, S3C2410_UCON);
  1553. ucon &= ~(APPLE_S5L_UCON_TXTHRESH_ENA_MSK |
  1554. APPLE_S5L_UCON_RXTHRESH_ENA_MSK |
  1555. APPLE_S5L_UCON_RXTO_ENA_MSK);
  1556. wr_regl(port, S3C2410_UCON, ucon);
  1557. wr_regl(port, S3C2410_UTRSTAT, APPLE_S5L_UTRSTAT_ALL_FLAGS);
  1558. break;
  1559. }
  1560. default:
  1561. break;
  1562. }
  1563. dev_dbg(port->dev, "port: map=%pa, mem=%p, irq=%d (%d,%d), clock=%u\n",
  1564. &port->mapbase, port->membase, port->irq,
  1565. ourport->rx_irq, ourport->tx_irq, port->uartclk);
  1566. /* reset the fifos (and setup the uart) */
  1567. s3c24xx_serial_resetport(port, cfg);
  1568. return 0;
  1569. err:
  1570. port->mapbase = 0;
  1571. return ret;
  1572. }
  1573. /* Device driver serial port probe */
  1574. static int probe_index;
  1575. static inline const struct s3c24xx_serial_drv_data *
  1576. s3c24xx_get_driver_data(struct platform_device *pdev)
  1577. {
  1578. if (dev_of_node(&pdev->dev))
  1579. return of_device_get_match_data(&pdev->dev);
  1580. return (struct s3c24xx_serial_drv_data *)
  1581. platform_get_device_id(pdev)->driver_data;
  1582. }
  1583. static int s3c24xx_serial_probe(struct platform_device *pdev)
  1584. {
  1585. struct device_node *np = pdev->dev.of_node;
  1586. struct s3c24xx_uart_port *ourport;
  1587. int index = probe_index;
  1588. int ret, prop = 0, fifosize_prop = 1;
  1589. if (np) {
  1590. ret = of_alias_get_id(np, "serial");
  1591. if (ret >= 0)
  1592. index = ret;
  1593. }
  1594. if (index >= ARRAY_SIZE(s3c24xx_serial_ports)) {
  1595. dev_err(&pdev->dev, "serial%d out of range\n", index);
  1596. return -EINVAL;
  1597. }
  1598. ourport = &s3c24xx_serial_ports[index];
  1599. s3c24xx_serial_init_port_default(index);
  1600. ourport->drv_data = s3c24xx_get_driver_data(pdev);
  1601. if (!ourport->drv_data) {
  1602. dev_err(&pdev->dev, "could not find driver data\n");
  1603. return -ENODEV;
  1604. }
  1605. ourport->baudclk = ERR_PTR(-EINVAL);
  1606. ourport->info = &ourport->drv_data->info;
  1607. ourport->cfg = (dev_get_platdata(&pdev->dev)) ?
  1608. dev_get_platdata(&pdev->dev) :
  1609. &ourport->drv_data->def_cfg;
  1610. switch (ourport->info->type) {
  1611. case TYPE_S3C6400:
  1612. ourport->port.ops = &s3c64xx_serial_ops;
  1613. break;
  1614. case TYPE_APPLE_S5L:
  1615. ourport->port.ops = &apple_s5l_serial_ops;
  1616. break;
  1617. }
  1618. ourport->port.iotype = ourport->info->iotype;
  1619. if (np) {
  1620. fifosize_prop = of_property_read_u32(np, "samsung,uart-fifosize",
  1621. &ourport->port.fifosize);
  1622. if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
  1623. switch (prop) {
  1624. case 1:
  1625. ourport->port.iotype = UPIO_MEM;
  1626. break;
  1627. case 4:
  1628. ourport->port.iotype = UPIO_MEM32;
  1629. break;
  1630. default:
  1631. dev_warn(&pdev->dev, "unsupported reg-io-width (%d)\n",
  1632. prop);
  1633. return -EINVAL;
  1634. }
  1635. }
  1636. }
  1637. if (fifosize_prop) {
  1638. if (ourport->drv_data->fifosize[index])
  1639. ourport->port.fifosize = ourport->drv_data->fifosize[index];
  1640. else if (ourport->info->fifosize)
  1641. ourport->port.fifosize = ourport->info->fifosize;
  1642. }
  1643. ourport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_SAMSUNG_CONSOLE);
  1644. /*
  1645. * DMA transfers must be aligned at least to cache line size,
  1646. * so find minimal transfer size suitable for DMA mode
  1647. */
  1648. ourport->min_dma_size = max_t(int, ourport->port.fifosize,
  1649. dma_get_cache_alignment());
  1650. dev_dbg(&pdev->dev, "%s: initialising port %p...\n", __func__, ourport);
  1651. ret = s3c24xx_serial_init_port(ourport, pdev);
  1652. if (ret < 0)
  1653. return ret;
  1654. if (!s3c24xx_uart_drv.state) {
  1655. ret = uart_register_driver(&s3c24xx_uart_drv);
  1656. if (ret < 0) {
  1657. pr_err("Failed to register Samsung UART driver\n");
  1658. return ret;
  1659. }
  1660. }
  1661. dev_dbg(&pdev->dev, "%s: adding port\n", __func__);
  1662. uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
  1663. platform_set_drvdata(pdev, &ourport->port);
  1664. /*
  1665. * Deactivate the clock enabled in s3c24xx_serial_init_port here,
  1666. * so that a potential re-enablement through the pm-callback overlaps
  1667. * and keeps the clock enabled in this case.
  1668. */
  1669. clk_disable_unprepare(ourport->clk);
  1670. if (!IS_ERR(ourport->baudclk))
  1671. clk_disable_unprepare(ourport->baudclk);
  1672. probe_index++;
  1673. return 0;
  1674. }
  1675. static void s3c24xx_serial_remove(struct platform_device *dev)
  1676. {
  1677. struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
  1678. if (port)
  1679. uart_remove_one_port(&s3c24xx_uart_drv, port);
  1680. uart_unregister_driver(&s3c24xx_uart_drv);
  1681. }
  1682. /* UART power management code */
  1683. #ifdef CONFIG_PM_SLEEP
  1684. static int s3c24xx_serial_suspend(struct device *dev)
  1685. {
  1686. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1687. if (port)
  1688. uart_suspend_port(&s3c24xx_uart_drv, port);
  1689. return 0;
  1690. }
  1691. static int s3c24xx_serial_resume(struct device *dev)
  1692. {
  1693. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1694. struct s3c24xx_uart_port *ourport = to_ourport(port);
  1695. if (port) {
  1696. clk_prepare_enable(ourport->clk);
  1697. if (!IS_ERR(ourport->baudclk))
  1698. clk_prepare_enable(ourport->baudclk);
  1699. s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
  1700. if (!IS_ERR(ourport->baudclk))
  1701. clk_disable_unprepare(ourport->baudclk);
  1702. clk_disable_unprepare(ourport->clk);
  1703. uart_resume_port(&s3c24xx_uart_drv, port);
  1704. }
  1705. return 0;
  1706. }
  1707. static int s3c24xx_serial_resume_noirq(struct device *dev)
  1708. {
  1709. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1710. struct s3c24xx_uart_port *ourport = to_ourport(port);
  1711. if (port) {
  1712. /* restore IRQ mask */
  1713. switch (ourport->info->type) {
  1714. case TYPE_S3C6400: {
  1715. u32 uintm = 0xf;
  1716. if (ourport->tx_enabled)
  1717. uintm &= ~S3C64XX_UINTM_TXD_MSK;
  1718. if (ourport->rx_enabled)
  1719. uintm &= ~S3C64XX_UINTM_RXD_MSK;
  1720. clk_prepare_enable(ourport->clk);
  1721. if (!IS_ERR(ourport->baudclk))
  1722. clk_prepare_enable(ourport->baudclk);
  1723. wr_regl(port, S3C64XX_UINTM, uintm);
  1724. if (!IS_ERR(ourport->baudclk))
  1725. clk_disable_unprepare(ourport->baudclk);
  1726. clk_disable_unprepare(ourport->clk);
  1727. break;
  1728. }
  1729. case TYPE_APPLE_S5L: {
  1730. u32 ucon;
  1731. int ret;
  1732. ret = clk_prepare_enable(ourport->clk);
  1733. if (ret) {
  1734. dev_err(dev, "clk_enable clk failed: %d\n", ret);
  1735. return ret;
  1736. }
  1737. if (!IS_ERR(ourport->baudclk)) {
  1738. ret = clk_prepare_enable(ourport->baudclk);
  1739. if (ret) {
  1740. dev_err(dev, "clk_enable baudclk failed: %d\n", ret);
  1741. clk_disable_unprepare(ourport->clk);
  1742. return ret;
  1743. }
  1744. }
  1745. ucon = rd_regl(port, S3C2410_UCON);
  1746. ucon &= ~(APPLE_S5L_UCON_TXTHRESH_ENA_MSK |
  1747. APPLE_S5L_UCON_RXTHRESH_ENA_MSK |
  1748. APPLE_S5L_UCON_RXTO_ENA_MSK |
  1749. APPLE_S5L_UCON_RXTO_LEGACY_ENA_MSK);
  1750. if (ourport->tx_enabled)
  1751. ucon |= APPLE_S5L_UCON_TXTHRESH_ENA_MSK;
  1752. if (ourport->rx_enabled)
  1753. ucon |= APPLE_S5L_UCON_RXTHRESH_ENA_MSK |
  1754. APPLE_S5L_UCON_RXTO_ENA_MSK |
  1755. APPLE_S5L_UCON_RXTO_LEGACY_ENA_MSK;
  1756. wr_regl(port, S3C2410_UCON, ucon);
  1757. if (!IS_ERR(ourport->baudclk))
  1758. clk_disable_unprepare(ourport->baudclk);
  1759. clk_disable_unprepare(ourport->clk);
  1760. break;
  1761. }
  1762. default:
  1763. break;
  1764. }
  1765. }
  1766. return 0;
  1767. }
  1768. static const struct dev_pm_ops s3c24xx_serial_pm_ops = {
  1769. SET_SYSTEM_SLEEP_PM_OPS(s3c24xx_serial_suspend, s3c24xx_serial_resume)
  1770. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(NULL, s3c24xx_serial_resume_noirq)
  1771. };
  1772. #define SERIAL_SAMSUNG_PM_OPS (&s3c24xx_serial_pm_ops)
  1773. #else /* !CONFIG_PM_SLEEP */
  1774. #define SERIAL_SAMSUNG_PM_OPS NULL
  1775. #endif /* CONFIG_PM_SLEEP */
  1776. /* Console code */
  1777. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  1778. static struct uart_port *cons_uart;
  1779. static bool
  1780. s3c24xx_serial_console_txrdy(struct uart_port *port, u32 ufcon)
  1781. {
  1782. const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1783. u32 ufstat, utrstat;
  1784. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  1785. /* fifo mode - check amount of data in fifo registers... */
  1786. ufstat = rd_regl(port, S3C2410_UFSTAT);
  1787. return !(ufstat & info->tx_fifofull);
  1788. }
  1789. /* in non-fifo mode, we go and use the tx buffer empty */
  1790. utrstat = rd_regl(port, S3C2410_UTRSTAT);
  1791. return utrstat & S3C2410_UTRSTAT_TXE;
  1792. }
  1793. static bool
  1794. s3c24xx_port_configured(u32 ucon)
  1795. {
  1796. /* consider the serial port configured if the tx/rx mode set */
  1797. return (ucon & 0xf) != 0;
  1798. }
  1799. #ifdef CONFIG_CONSOLE_POLL
  1800. /*
  1801. * Console polling routines for writing and reading from the uart while
  1802. * in an interrupt or debug context.
  1803. */
  1804. static int s3c24xx_serial_get_poll_char(struct uart_port *port)
  1805. {
  1806. const struct s3c24xx_uart_port *ourport = to_ourport(port);
  1807. u32 ufstat;
  1808. ufstat = rd_regl(port, S3C2410_UFSTAT);
  1809. if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
  1810. return NO_POLL_CHAR;
  1811. return rd_reg(port, S3C2410_URXH);
  1812. }
  1813. static void s3c24xx_serial_put_poll_char(struct uart_port *port,
  1814. unsigned char c)
  1815. {
  1816. u32 ufcon = rd_regl(port, S3C2410_UFCON);
  1817. u32 ucon = rd_regl(port, S3C2410_UCON);
  1818. /* not possible to xmit on unconfigured port */
  1819. if (!s3c24xx_port_configured(ucon))
  1820. return;
  1821. while (!s3c24xx_serial_console_txrdy(port, ufcon))
  1822. cpu_relax();
  1823. wr_reg(port, S3C2410_UTXH, c);
  1824. }
  1825. #endif /* CONFIG_CONSOLE_POLL */
  1826. static void
  1827. s3c24xx_serial_console_putchar(struct uart_port *port, unsigned char ch)
  1828. {
  1829. u32 ufcon = rd_regl(port, S3C2410_UFCON);
  1830. while (!s3c24xx_serial_console_txrdy(port, ufcon))
  1831. cpu_relax();
  1832. wr_reg(port, S3C2410_UTXH, ch);
  1833. }
  1834. static void
  1835. s3c24xx_serial_console_write(struct console *co, const char *s,
  1836. unsigned int count)
  1837. {
  1838. u32 ucon = rd_regl(cons_uart, S3C2410_UCON);
  1839. unsigned long flags;
  1840. bool locked = true;
  1841. /* not possible to xmit on unconfigured port */
  1842. if (!s3c24xx_port_configured(ucon))
  1843. return;
  1844. if (cons_uart->sysrq)
  1845. locked = false;
  1846. else if (oops_in_progress)
  1847. locked = uart_port_trylock_irqsave(cons_uart, &flags);
  1848. else
  1849. uart_port_lock_irqsave(cons_uart, &flags);
  1850. uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
  1851. if (locked)
  1852. uart_port_unlock_irqrestore(cons_uart, flags);
  1853. }
  1854. /* Shouldn't be __init, as it can be instantiated from other module */
  1855. static void
  1856. s3c24xx_serial_get_options(struct uart_port *port, int *baud,
  1857. int *parity, int *bits)
  1858. {
  1859. struct clk *clk;
  1860. unsigned long rate;
  1861. u32 ulcon, ucon, ubrdiv;
  1862. char clk_name[MAX_CLK_NAME_LENGTH];
  1863. u8 clk_sel;
  1864. ulcon = rd_regl(port, S3C2410_ULCON);
  1865. ucon = rd_regl(port, S3C2410_UCON);
  1866. ubrdiv = rd_regl(port, S3C2410_UBRDIV);
  1867. if (s3c24xx_port_configured(ucon)) {
  1868. switch (ulcon & S3C2410_LCON_CSMASK) {
  1869. case S3C2410_LCON_CS5:
  1870. *bits = 5;
  1871. break;
  1872. case S3C2410_LCON_CS6:
  1873. *bits = 6;
  1874. break;
  1875. case S3C2410_LCON_CS7:
  1876. *bits = 7;
  1877. break;
  1878. case S3C2410_LCON_CS8:
  1879. default:
  1880. *bits = 8;
  1881. break;
  1882. }
  1883. switch (ulcon & S3C2410_LCON_PMASK) {
  1884. case S3C2410_LCON_PEVEN:
  1885. *parity = 'e';
  1886. break;
  1887. case S3C2410_LCON_PODD:
  1888. *parity = 'o';
  1889. break;
  1890. case S3C2410_LCON_PNONE:
  1891. default:
  1892. *parity = 'n';
  1893. }
  1894. /* now calculate the baud rate */
  1895. clk_sel = s3c24xx_serial_getsource(port);
  1896. sprintf(clk_name, "clk_uart_baud%d", clk_sel);
  1897. clk = clk_get(port->dev, clk_name);
  1898. if (!IS_ERR(clk))
  1899. rate = clk_get_rate(clk);
  1900. else
  1901. rate = 1;
  1902. *baud = rate / (16 * (ubrdiv + 1));
  1903. dev_dbg(port->dev, "calculated baud %d\n", *baud);
  1904. }
  1905. }
  1906. /* Shouldn't be __init, as it can be instantiated from other module */
  1907. static int
  1908. s3c24xx_serial_console_setup(struct console *co, char *options)
  1909. {
  1910. struct uart_port *port;
  1911. int baud = 9600;
  1912. int bits = 8;
  1913. int parity = 'n';
  1914. int flow = 'n';
  1915. /* is this a valid port */
  1916. if (co->index == -1 || co->index >= UART_NR)
  1917. co->index = 0;
  1918. port = &s3c24xx_serial_ports[co->index].port;
  1919. /* is the port configured? */
  1920. if (port->mapbase == 0x0)
  1921. return -ENODEV;
  1922. cons_uart = port;
  1923. /*
  1924. * Check whether an invalid uart number has been specified, and
  1925. * if so, search for the first available port that does have
  1926. * console support.
  1927. */
  1928. if (options)
  1929. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1930. else
  1931. s3c24xx_serial_get_options(port, &baud, &parity, &bits);
  1932. dev_dbg(port->dev, "baud %d\n", baud);
  1933. return uart_set_options(port, co, baud, parity, bits, flow);
  1934. }
  1935. static struct console s3c24xx_serial_console = {
  1936. .name = S3C24XX_SERIAL_NAME,
  1937. .device = uart_console_device,
  1938. .flags = CON_PRINTBUFFER,
  1939. .index = -1,
  1940. .write = s3c24xx_serial_console_write,
  1941. .setup = s3c24xx_serial_console_setup,
  1942. .data = &s3c24xx_uart_drv,
  1943. };
  1944. #endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
  1945. #if defined(CONFIG_CPU_S3C6400) || defined(CONFIG_CPU_S3C6410)
  1946. static const struct s3c24xx_serial_drv_data s3c6400_serial_drv_data = {
  1947. .info = {
  1948. .name = "Samsung S3C6400 UART",
  1949. .type = TYPE_S3C6400,
  1950. .port_type = PORT_S3C6400,
  1951. .iotype = UPIO_MEM,
  1952. .fifosize = 64,
  1953. .has_divslot = true,
  1954. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1955. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1956. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1957. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1958. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1959. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1960. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1961. .num_clks = 4,
  1962. .clksel_mask = S3C6400_UCON_CLKMASK,
  1963. .clksel_shift = S3C6400_UCON_CLKSHIFT,
  1964. },
  1965. .def_cfg = {
  1966. .ucon = S3C2410_UCON_DEFAULT,
  1967. .ufcon = S3C2410_UFCON_DEFAULT,
  1968. },
  1969. };
  1970. #define S3C6400_SERIAL_DRV_DATA (&s3c6400_serial_drv_data)
  1971. #else
  1972. #define S3C6400_SERIAL_DRV_DATA NULL
  1973. #endif
  1974. #ifdef CONFIG_CPU_S5PV210
  1975. static const struct s3c24xx_serial_drv_data s5pv210_serial_drv_data = {
  1976. .info = {
  1977. .name = "Samsung S5PV210 UART",
  1978. .type = TYPE_S3C6400,
  1979. .port_type = PORT_S3C6400,
  1980. .iotype = UPIO_MEM,
  1981. .has_divslot = true,
  1982. .rx_fifomask = S5PV210_UFSTAT_RXMASK,
  1983. .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
  1984. .rx_fifofull = S5PV210_UFSTAT_RXFULL,
  1985. .tx_fifofull = S5PV210_UFSTAT_TXFULL,
  1986. .tx_fifomask = S5PV210_UFSTAT_TXMASK,
  1987. .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
  1988. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  1989. .num_clks = 2,
  1990. .clksel_mask = S5PV210_UCON_CLKMASK,
  1991. .clksel_shift = S5PV210_UCON_CLKSHIFT,
  1992. },
  1993. .def_cfg = {
  1994. .ucon = S5PV210_UCON_DEFAULT,
  1995. .ufcon = S5PV210_UFCON_DEFAULT,
  1996. },
  1997. .fifosize = { 256, 64, 16, 16 },
  1998. };
  1999. #define S5PV210_SERIAL_DRV_DATA (&s5pv210_serial_drv_data)
  2000. #else
  2001. #define S5PV210_SERIAL_DRV_DATA NULL
  2002. #endif
  2003. #if defined(CONFIG_ARCH_EXYNOS)
  2004. #define EXYNOS_COMMON_SERIAL_DRV_DATA \
  2005. .info = { \
  2006. .name = "Samsung Exynos UART", \
  2007. .type = TYPE_S3C6400, \
  2008. .port_type = PORT_S3C6400, \
  2009. .iotype = UPIO_MEM, \
  2010. .has_divslot = true, \
  2011. .rx_fifomask = S5PV210_UFSTAT_RXMASK, \
  2012. .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT, \
  2013. .rx_fifofull = S5PV210_UFSTAT_RXFULL, \
  2014. .tx_fifofull = S5PV210_UFSTAT_TXFULL, \
  2015. .tx_fifomask = S5PV210_UFSTAT_TXMASK, \
  2016. .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT, \
  2017. .def_clk_sel = S3C2410_UCON_CLKSEL0, \
  2018. .num_clks = 1, \
  2019. .clksel_mask = 0, \
  2020. .clksel_shift = 0, \
  2021. }, \
  2022. .def_cfg = { \
  2023. .ucon = S5PV210_UCON_DEFAULT, \
  2024. .ufcon = S5PV210_UFCON_DEFAULT, \
  2025. .has_fracval = 1, \
  2026. } \
  2027. static const struct s3c24xx_serial_drv_data exynos4210_serial_drv_data = {
  2028. EXYNOS_COMMON_SERIAL_DRV_DATA,
  2029. .fifosize = { 256, 64, 16, 16 },
  2030. };
  2031. static const struct s3c24xx_serial_drv_data exynos5433_serial_drv_data = {
  2032. EXYNOS_COMMON_SERIAL_DRV_DATA,
  2033. .fifosize = { 64, 256, 16, 256 },
  2034. };
  2035. static const struct s3c24xx_serial_drv_data exynos850_serial_drv_data = {
  2036. EXYNOS_COMMON_SERIAL_DRV_DATA,
  2037. .fifosize = { 256, 64, 64, 64 },
  2038. };
  2039. static const struct s3c24xx_serial_drv_data gs101_serial_drv_data = {
  2040. .info = {
  2041. .name = "Google GS101 UART",
  2042. .type = TYPE_S3C6400,
  2043. .port_type = PORT_S3C6400,
  2044. .iotype = UPIO_MEM32,
  2045. .has_divslot = true,
  2046. .rx_fifomask = S5PV210_UFSTAT_RXMASK,
  2047. .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
  2048. .rx_fifofull = S5PV210_UFSTAT_RXFULL,
  2049. .tx_fifofull = S5PV210_UFSTAT_TXFULL,
  2050. .tx_fifomask = S5PV210_UFSTAT_TXMASK,
  2051. .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
  2052. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  2053. .num_clks = 1,
  2054. .clksel_mask = 0,
  2055. .clksel_shift = 0,
  2056. },
  2057. .def_cfg = {
  2058. .ucon = S5PV210_UCON_DEFAULT,
  2059. .ufcon = S5PV210_UFCON_DEFAULT,
  2060. .has_fracval = 1,
  2061. },
  2062. /* samsung,uart-fifosize must be specified in the device tree. */
  2063. .fifosize = { 0 },
  2064. };
  2065. #define EXYNOS4210_SERIAL_DRV_DATA (&exynos4210_serial_drv_data)
  2066. #define EXYNOS5433_SERIAL_DRV_DATA (&exynos5433_serial_drv_data)
  2067. #define EXYNOS850_SERIAL_DRV_DATA (&exynos850_serial_drv_data)
  2068. #define GS101_SERIAL_DRV_DATA (&gs101_serial_drv_data)
  2069. #else
  2070. #define EXYNOS4210_SERIAL_DRV_DATA NULL
  2071. #define EXYNOS5433_SERIAL_DRV_DATA NULL
  2072. #define EXYNOS850_SERIAL_DRV_DATA NULL
  2073. #define GS101_SERIAL_DRV_DATA NULL
  2074. #endif
  2075. #ifdef CONFIG_ARCH_APPLE
  2076. static const struct s3c24xx_serial_drv_data s5l_serial_drv_data = {
  2077. .info = {
  2078. .name = "Apple S5L UART",
  2079. .type = TYPE_APPLE_S5L,
  2080. .port_type = PORT_8250,
  2081. .iotype = UPIO_MEM32,
  2082. .fifosize = 16,
  2083. .rx_fifomask = S3C2410_UFSTAT_RXMASK,
  2084. .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
  2085. .rx_fifofull = S3C2410_UFSTAT_RXFULL,
  2086. .tx_fifofull = S3C2410_UFSTAT_TXFULL,
  2087. .tx_fifomask = S3C2410_UFSTAT_TXMASK,
  2088. .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
  2089. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  2090. .num_clks = 1,
  2091. .clksel_mask = 0,
  2092. .clksel_shift = 0,
  2093. .ucon_mask = APPLE_S5L_UCON_MASK,
  2094. },
  2095. .def_cfg = {
  2096. .ucon = APPLE_S5L_UCON_DEFAULT,
  2097. .ufcon = S3C2410_UFCON_DEFAULT,
  2098. },
  2099. };
  2100. #define S5L_SERIAL_DRV_DATA (&s5l_serial_drv_data)
  2101. #else
  2102. #define S5L_SERIAL_DRV_DATA NULL
  2103. #endif
  2104. #if defined(CONFIG_ARCH_ARTPEC)
  2105. static const struct s3c24xx_serial_drv_data artpec8_serial_drv_data = {
  2106. .info = {
  2107. .name = "Axis ARTPEC-8 UART",
  2108. .type = TYPE_S3C6400,
  2109. .port_type = PORT_S3C6400,
  2110. .iotype = UPIO_MEM,
  2111. .fifosize = 64,
  2112. .has_divslot = true,
  2113. .rx_fifomask = S5PV210_UFSTAT_RXMASK,
  2114. .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
  2115. .rx_fifofull = S5PV210_UFSTAT_RXFULL,
  2116. .tx_fifofull = S5PV210_UFSTAT_TXFULL,
  2117. .tx_fifomask = S5PV210_UFSTAT_TXMASK,
  2118. .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
  2119. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  2120. .num_clks = 1,
  2121. .clksel_mask = 0,
  2122. .clksel_shift = 0,
  2123. },
  2124. .def_cfg = {
  2125. .ucon = S5PV210_UCON_DEFAULT,
  2126. .ufcon = S5PV210_UFCON_DEFAULT,
  2127. .has_fracval = 1,
  2128. }
  2129. };
  2130. #define ARTPEC8_SERIAL_DRV_DATA (&artpec8_serial_drv_data)
  2131. #else
  2132. #define ARTPEC8_SERIAL_DRV_DATA (NULL)
  2133. #endif
  2134. static const struct platform_device_id s3c24xx_serial_driver_ids[] = {
  2135. {
  2136. .name = "s3c6400-uart",
  2137. .driver_data = (kernel_ulong_t)S3C6400_SERIAL_DRV_DATA,
  2138. }, {
  2139. .name = "s5pv210-uart",
  2140. .driver_data = (kernel_ulong_t)S5PV210_SERIAL_DRV_DATA,
  2141. }, {
  2142. .name = "exynos4210-uart",
  2143. .driver_data = (kernel_ulong_t)EXYNOS4210_SERIAL_DRV_DATA,
  2144. }, {
  2145. .name = "exynos5433-uart",
  2146. .driver_data = (kernel_ulong_t)EXYNOS5433_SERIAL_DRV_DATA,
  2147. }, {
  2148. .name = "s5l-uart",
  2149. .driver_data = (kernel_ulong_t)S5L_SERIAL_DRV_DATA,
  2150. }, {
  2151. .name = "exynos850-uart",
  2152. .driver_data = (kernel_ulong_t)EXYNOS850_SERIAL_DRV_DATA,
  2153. }, {
  2154. .name = "artpec8-uart",
  2155. .driver_data = (kernel_ulong_t)ARTPEC8_SERIAL_DRV_DATA,
  2156. }, {
  2157. .name = "gs101-uart",
  2158. .driver_data = (kernel_ulong_t)GS101_SERIAL_DRV_DATA,
  2159. },
  2160. { },
  2161. };
  2162. MODULE_DEVICE_TABLE(platform, s3c24xx_serial_driver_ids);
  2163. #ifdef CONFIG_OF
  2164. static const struct of_device_id s3c24xx_uart_dt_match[] = {
  2165. { .compatible = "samsung,s3c6400-uart",
  2166. .data = S3C6400_SERIAL_DRV_DATA },
  2167. { .compatible = "samsung,s5pv210-uart",
  2168. .data = S5PV210_SERIAL_DRV_DATA },
  2169. { .compatible = "samsung,exynos4210-uart",
  2170. .data = EXYNOS4210_SERIAL_DRV_DATA },
  2171. { .compatible = "samsung,exynos5433-uart",
  2172. .data = EXYNOS5433_SERIAL_DRV_DATA },
  2173. { .compatible = "apple,s5l-uart",
  2174. .data = S5L_SERIAL_DRV_DATA },
  2175. { .compatible = "samsung,exynos850-uart",
  2176. .data = EXYNOS850_SERIAL_DRV_DATA },
  2177. { .compatible = "axis,artpec8-uart",
  2178. .data = ARTPEC8_SERIAL_DRV_DATA },
  2179. { .compatible = "google,gs101-uart",
  2180. .data = GS101_SERIAL_DRV_DATA },
  2181. {},
  2182. };
  2183. MODULE_DEVICE_TABLE(of, s3c24xx_uart_dt_match);
  2184. #endif
  2185. static struct platform_driver samsung_serial_driver = {
  2186. .probe = s3c24xx_serial_probe,
  2187. .remove_new = s3c24xx_serial_remove,
  2188. .id_table = s3c24xx_serial_driver_ids,
  2189. .driver = {
  2190. .name = "samsung-uart",
  2191. .pm = SERIAL_SAMSUNG_PM_OPS,
  2192. .of_match_table = of_match_ptr(s3c24xx_uart_dt_match),
  2193. },
  2194. };
  2195. static int __init samsung_serial_init(void)
  2196. {
  2197. int ret;
  2198. s3c24xx_serial_register_console();
  2199. ret = platform_driver_register(&samsung_serial_driver);
  2200. if (ret) {
  2201. s3c24xx_serial_unregister_console();
  2202. return ret;
  2203. }
  2204. return 0;
  2205. }
  2206. static void __exit samsung_serial_exit(void)
  2207. {
  2208. platform_driver_unregister(&samsung_serial_driver);
  2209. s3c24xx_serial_unregister_console();
  2210. }
  2211. module_init(samsung_serial_init);
  2212. module_exit(samsung_serial_exit);
  2213. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  2214. /*
  2215. * Early console.
  2216. */
  2217. static void wr_reg_barrier(const struct uart_port *port, u32 reg, u32 val)
  2218. {
  2219. switch (port->iotype) {
  2220. case UPIO_MEM:
  2221. writeb(val, portaddr(port, reg));
  2222. break;
  2223. case UPIO_MEM32:
  2224. writel(val, portaddr(port, reg));
  2225. break;
  2226. }
  2227. }
  2228. struct samsung_early_console_data {
  2229. u32 txfull_mask;
  2230. u32 rxfifo_mask;
  2231. };
  2232. static void samsung_early_busyuart(const struct uart_port *port)
  2233. {
  2234. while (!(readl(port->membase + S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXFE))
  2235. ;
  2236. }
  2237. static void samsung_early_busyuart_fifo(const struct uart_port *port)
  2238. {
  2239. const struct samsung_early_console_data *data = port->private_data;
  2240. while (readl(port->membase + S3C2410_UFSTAT) & data->txfull_mask)
  2241. ;
  2242. }
  2243. static void samsung_early_putc(struct uart_port *port, unsigned char c)
  2244. {
  2245. if (readl(port->membase + S3C2410_UFCON) & S3C2410_UFCON_FIFOMODE)
  2246. samsung_early_busyuart_fifo(port);
  2247. else
  2248. samsung_early_busyuart(port);
  2249. wr_reg_barrier(port, S3C2410_UTXH, c);
  2250. }
  2251. static void samsung_early_write(struct console *con, const char *s,
  2252. unsigned int n)
  2253. {
  2254. struct earlycon_device *dev = con->data;
  2255. uart_console_write(&dev->port, s, n, samsung_early_putc);
  2256. }
  2257. static int samsung_early_read(struct console *con, char *s, unsigned int n)
  2258. {
  2259. struct earlycon_device *dev = con->data;
  2260. const struct samsung_early_console_data *data = dev->port.private_data;
  2261. int num_read = 0;
  2262. u32 ch, ufstat;
  2263. while (num_read < n) {
  2264. ufstat = rd_regl(&dev->port, S3C2410_UFSTAT);
  2265. if (!(ufstat & data->rxfifo_mask))
  2266. break;
  2267. ch = rd_reg(&dev->port, S3C2410_URXH);
  2268. if (ch == NO_POLL_CHAR)
  2269. break;
  2270. s[num_read++] = ch;
  2271. }
  2272. return num_read;
  2273. }
  2274. static int __init samsung_early_console_setup(struct earlycon_device *device,
  2275. const char *opt)
  2276. {
  2277. if (!device->port.membase)
  2278. return -ENODEV;
  2279. device->con->write = samsung_early_write;
  2280. device->con->read = samsung_early_read;
  2281. return 0;
  2282. }
  2283. /* S3C2410 */
  2284. static struct samsung_early_console_data s3c2410_early_console_data = {
  2285. .txfull_mask = S3C2410_UFSTAT_TXFULL,
  2286. .rxfifo_mask = S3C2410_UFSTAT_RXFULL | S3C2410_UFSTAT_RXMASK,
  2287. };
  2288. /* S3C64xx */
  2289. static struct samsung_early_console_data s3c2440_early_console_data = {
  2290. .txfull_mask = S3C2440_UFSTAT_TXFULL,
  2291. .rxfifo_mask = S3C2440_UFSTAT_RXFULL | S3C2440_UFSTAT_RXMASK,
  2292. };
  2293. static int __init s3c2440_early_console_setup(struct earlycon_device *device,
  2294. const char *opt)
  2295. {
  2296. device->port.private_data = &s3c2440_early_console_data;
  2297. return samsung_early_console_setup(device, opt);
  2298. }
  2299. OF_EARLYCON_DECLARE(s3c6400, "samsung,s3c6400-uart",
  2300. s3c2440_early_console_setup);
  2301. /* S5PV210, Exynos */
  2302. static struct samsung_early_console_data s5pv210_early_console_data = {
  2303. .txfull_mask = S5PV210_UFSTAT_TXFULL,
  2304. .rxfifo_mask = S5PV210_UFSTAT_RXFULL | S5PV210_UFSTAT_RXMASK,
  2305. };
  2306. static int __init s5pv210_early_console_setup(struct earlycon_device *device,
  2307. const char *opt)
  2308. {
  2309. device->port.private_data = &s5pv210_early_console_data;
  2310. return samsung_early_console_setup(device, opt);
  2311. }
  2312. OF_EARLYCON_DECLARE(s5pv210, "samsung,s5pv210-uart",
  2313. s5pv210_early_console_setup);
  2314. OF_EARLYCON_DECLARE(exynos4210, "samsung,exynos4210-uart",
  2315. s5pv210_early_console_setup);
  2316. OF_EARLYCON_DECLARE(artpec8, "axis,artpec8-uart",
  2317. s5pv210_early_console_setup);
  2318. static int __init gs101_early_console_setup(struct earlycon_device *device,
  2319. const char *opt)
  2320. {
  2321. /* gs101 always expects MMIO32 register accesses. */
  2322. device->port.iotype = UPIO_MEM32;
  2323. return s5pv210_early_console_setup(device, opt);
  2324. }
  2325. OF_EARLYCON_DECLARE(gs101, "google,gs101-uart", gs101_early_console_setup);
  2326. /* Apple S5L */
  2327. static int __init apple_s5l_early_console_setup(struct earlycon_device *device,
  2328. const char *opt)
  2329. {
  2330. /* Apple A7-A11 requires MMIO32 register accesses. */
  2331. device->port.iotype = UPIO_MEM32;
  2332. /* Close enough to S3C2410 for earlycon... */
  2333. device->port.private_data = &s3c2410_early_console_data;
  2334. #ifdef CONFIG_ARM64
  2335. /* ... but we need to override the existing fixmap entry as nGnRnE */
  2336. __set_fixmap(FIX_EARLYCON_MEM_BASE, device->port.mapbase,
  2337. __pgprot(PROT_DEVICE_nGnRnE));
  2338. #endif
  2339. return samsung_early_console_setup(device, opt);
  2340. }
  2341. OF_EARLYCON_DECLARE(s5l, "apple,s5l-uart", apple_s5l_early_console_setup);
  2342. #endif
  2343. MODULE_ALIAS("platform:samsung-uart");
  2344. MODULE_DESCRIPTION("Samsung SoC Serial port driver");
  2345. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  2346. MODULE_LICENSE("GPL v2");