samsung_tty.c 77 KB

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