samsung_tty.c 71 KB

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