8250.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341
  1. /*
  2. * Driver for 8250/16550-type serial ports
  3. *
  4. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  5. *
  6. * Copyright (C) 2001 Russell King.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * A note about mapbase / membase
  14. *
  15. * mapbase is the physical address of the IO port.
  16. * membase is an 'ioremapped' cookie.
  17. */
  18. #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  19. #define SUPPORT_SYSRQ
  20. #endif
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/ioport.h>
  24. #include <linux/init.h>
  25. #include <linux/console.h>
  26. #include <linux/sysrq.h>
  27. #include <linux/delay.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/tty.h>
  30. #include <linux/ratelimit.h>
  31. #include <linux/tty_flip.h>
  32. #include <linux/serial_reg.h>
  33. #include <linux/serial_core.h>
  34. #include <linux/serial.h>
  35. #include <linux/serial_8250.h>
  36. #include <linux/nmi.h>
  37. #include <linux/mutex.h>
  38. #include <linux/slab.h>
  39. #ifdef CONFIG_SPARC
  40. #include <linux/sunserialcore.h>
  41. #endif
  42. #include <asm/io.h>
  43. #include <asm/irq.h>
  44. #include "8250.h"
  45. /*
  46. * Configuration:
  47. * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
  48. * is unsafe when used on edge-triggered interrupts.
  49. */
  50. static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
  51. static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
  52. static struct uart_driver serial8250_reg;
  53. static int serial_index(struct uart_port *port)
  54. {
  55. return (serial8250_reg.minor - 64) + port->line;
  56. }
  57. static unsigned int skip_txen_test; /* force skip of txen test at init time */
  58. /*
  59. * Debugging.
  60. */
  61. #if 0
  62. #define DEBUG_AUTOCONF(fmt...) printk(fmt)
  63. #else
  64. #define DEBUG_AUTOCONF(fmt...) do { } while (0)
  65. #endif
  66. #if 0
  67. #define DEBUG_INTR(fmt...) printk(fmt)
  68. #else
  69. #define DEBUG_INTR(fmt...) do { } while (0)
  70. #endif
  71. #define PASS_LIMIT 512
  72. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  73. #ifdef CONFIG_SERIAL_8250_DETECT_IRQ
  74. #define CONFIG_SERIAL_DETECT_IRQ 1
  75. #endif
  76. #ifdef CONFIG_SERIAL_8250_MANY_PORTS
  77. #define CONFIG_SERIAL_MANY_PORTS 1
  78. #endif
  79. /*
  80. * HUB6 is always on. This will be removed once the header
  81. * files have been cleaned.
  82. */
  83. #define CONFIG_HUB6 1
  84. #include <asm/serial.h>
  85. /*
  86. * SERIAL_PORT_DFNS tells us about built-in ports that have no
  87. * standard enumeration mechanism. Platforms that can find all
  88. * serial ports via mechanisms like ACPI or PCI need not supply it.
  89. */
  90. #ifndef SERIAL_PORT_DFNS
  91. #define SERIAL_PORT_DFNS
  92. #endif
  93. static const struct old_serial_port old_serial_port[] = {
  94. SERIAL_PORT_DFNS /* defined in asm/serial.h */
  95. };
  96. #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
  97. #ifdef CONFIG_SERIAL_8250_RSA
  98. #define PORT_RSA_MAX 4
  99. static unsigned long probe_rsa[PORT_RSA_MAX];
  100. static unsigned int probe_rsa_count;
  101. #endif /* CONFIG_SERIAL_8250_RSA */
  102. struct irq_info {
  103. struct hlist_node node;
  104. int irq;
  105. spinlock_t lock; /* Protects list not the hash */
  106. struct list_head *head;
  107. };
  108. #define NR_IRQ_HASH 32 /* Can be adjusted later */
  109. static struct hlist_head irq_lists[NR_IRQ_HASH];
  110. static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
  111. /*
  112. * Here we define the default xmit fifo size used for each type of UART.
  113. */
  114. static const struct serial8250_config uart_config[] = {
  115. [PORT_UNKNOWN] = {
  116. .name = "unknown",
  117. .fifo_size = 1,
  118. .tx_loadsz = 1,
  119. },
  120. [PORT_8250] = {
  121. .name = "8250",
  122. .fifo_size = 1,
  123. .tx_loadsz = 1,
  124. },
  125. [PORT_16450] = {
  126. .name = "16450",
  127. .fifo_size = 1,
  128. .tx_loadsz = 1,
  129. },
  130. [PORT_16550] = {
  131. .name = "16550",
  132. .fifo_size = 1,
  133. .tx_loadsz = 1,
  134. },
  135. [PORT_16550A] = {
  136. .name = "16550A",
  137. .fifo_size = 16,
  138. .tx_loadsz = 16,
  139. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  140. .flags = UART_CAP_FIFO,
  141. },
  142. [PORT_CIRRUS] = {
  143. .name = "Cirrus",
  144. .fifo_size = 1,
  145. .tx_loadsz = 1,
  146. },
  147. [PORT_16650] = {
  148. .name = "ST16650",
  149. .fifo_size = 1,
  150. .tx_loadsz = 1,
  151. .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
  152. },
  153. [PORT_16650V2] = {
  154. .name = "ST16650V2",
  155. .fifo_size = 32,
  156. .tx_loadsz = 16,
  157. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
  158. UART_FCR_T_TRIG_00,
  159. .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
  160. },
  161. [PORT_16750] = {
  162. .name = "TI16750",
  163. .fifo_size = 64,
  164. .tx_loadsz = 64,
  165. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
  166. UART_FCR7_64BYTE,
  167. .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
  168. },
  169. [PORT_STARTECH] = {
  170. .name = "Startech",
  171. .fifo_size = 1,
  172. .tx_loadsz = 1,
  173. },
  174. [PORT_16C950] = {
  175. .name = "16C950/954",
  176. .fifo_size = 128,
  177. .tx_loadsz = 128,
  178. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  179. /* UART_CAP_EFR breaks billionon CF bluetooth card. */
  180. .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
  181. },
  182. [PORT_16654] = {
  183. .name = "ST16654",
  184. .fifo_size = 64,
  185. .tx_loadsz = 32,
  186. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
  187. UART_FCR_T_TRIG_10,
  188. .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
  189. },
  190. [PORT_16850] = {
  191. .name = "XR16850",
  192. .fifo_size = 128,
  193. .tx_loadsz = 128,
  194. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  195. .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
  196. },
  197. [PORT_RSA] = {
  198. .name = "RSA",
  199. .fifo_size = 2048,
  200. .tx_loadsz = 2048,
  201. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
  202. .flags = UART_CAP_FIFO,
  203. },
  204. [PORT_NS16550A] = {
  205. .name = "NS16550A",
  206. .fifo_size = 16,
  207. .tx_loadsz = 16,
  208. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  209. .flags = UART_CAP_FIFO | UART_NATSEMI,
  210. },
  211. [PORT_XSCALE] = {
  212. .name = "XScale",
  213. .fifo_size = 32,
  214. .tx_loadsz = 32,
  215. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  216. .flags = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
  217. },
  218. [PORT_RM9000] = {
  219. .name = "RM9000",
  220. .fifo_size = 16,
  221. .tx_loadsz = 16,
  222. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  223. .flags = UART_CAP_FIFO,
  224. },
  225. [PORT_OCTEON] = {
  226. .name = "OCTEON",
  227. .fifo_size = 64,
  228. .tx_loadsz = 64,
  229. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  230. .flags = UART_CAP_FIFO,
  231. },
  232. [PORT_AR7] = {
  233. .name = "AR7",
  234. .fifo_size = 16,
  235. .tx_loadsz = 16,
  236. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
  237. .flags = UART_CAP_FIFO | UART_CAP_AFE,
  238. },
  239. [PORT_U6_16550A] = {
  240. .name = "U6_16550A",
  241. .fifo_size = 64,
  242. .tx_loadsz = 64,
  243. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  244. .flags = UART_CAP_FIFO | UART_CAP_AFE,
  245. },
  246. [PORT_TEGRA] = {
  247. .name = "Tegra",
  248. .fifo_size = 32,
  249. .tx_loadsz = 8,
  250. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
  251. UART_FCR_T_TRIG_01,
  252. .flags = UART_CAP_FIFO | UART_CAP_RTOIE,
  253. },
  254. [PORT_XR17D15X] = {
  255. .name = "XR17D15X",
  256. .fifo_size = 64,
  257. .tx_loadsz = 64,
  258. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  259. .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR,
  260. },
  261. };
  262. /* Uart divisor latch read */
  263. static int default_dl_read(struct uart_8250_port *up)
  264. {
  265. return serial_in(up, UART_DLL) | serial_in(up, UART_DLM) << 8;
  266. }
  267. /* Uart divisor latch write */
  268. static void default_dl_write(struct uart_8250_port *up, int value)
  269. {
  270. serial_out(up, UART_DLL, value & 0xff);
  271. serial_out(up, UART_DLM, value >> 8 & 0xff);
  272. }
  273. #if defined(CONFIG_MIPS_ALCHEMY)
  274. /* Au1x00 haven't got a standard divisor latch */
  275. static int _serial_dl_read(struct uart_8250_port *up)
  276. {
  277. if (up->port.iotype == UPIO_AU)
  278. return __raw_readl(up->port.membase + 0x28);
  279. else
  280. return default_dl_read(up);
  281. }
  282. static void _serial_dl_write(struct uart_8250_port *up, int value)
  283. {
  284. if (up->port.iotype == UPIO_AU)
  285. __raw_writel(value, up->port.membase + 0x28);
  286. else
  287. default_dl_write(up, value);
  288. }
  289. #elif defined(CONFIG_SERIAL_8250_RM9K)
  290. static int _serial_dl_read(struct uart_8250_port *up)
  291. {
  292. return (up->port.iotype == UPIO_RM9000) ?
  293. (((__raw_readl(up->port.membase + 0x10) << 8) |
  294. (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
  295. default_dl_read(up);
  296. }
  297. static void _serial_dl_write(struct uart_8250_port *up, int value)
  298. {
  299. if (up->port.iotype == UPIO_RM9000) {
  300. __raw_writel(value, up->port.membase + 0x08);
  301. __raw_writel(value >> 8, up->port.membase + 0x10);
  302. } else {
  303. default_dl_write(up, value);
  304. }
  305. }
  306. #else
  307. static int _serial_dl_read(struct uart_8250_port *up)
  308. {
  309. return default_dl_read(up);
  310. }
  311. static void _serial_dl_write(struct uart_8250_port *up, int value)
  312. {
  313. default_dl_write(up, value);
  314. }
  315. #endif
  316. #if defined(CONFIG_MIPS_ALCHEMY)
  317. /* Au1x00 UART hardware has a weird register layout */
  318. static const u8 au_io_in_map[] = {
  319. [UART_RX] = 0,
  320. [UART_IER] = 2,
  321. [UART_IIR] = 3,
  322. [UART_LCR] = 5,
  323. [UART_MCR] = 6,
  324. [UART_LSR] = 7,
  325. [UART_MSR] = 8,
  326. };
  327. static const u8 au_io_out_map[] = {
  328. [UART_TX] = 1,
  329. [UART_IER] = 2,
  330. [UART_FCR] = 4,
  331. [UART_LCR] = 5,
  332. [UART_MCR] = 6,
  333. };
  334. /* sane hardware needs no mapping */
  335. static inline int map_8250_in_reg(struct uart_port *p, int offset)
  336. {
  337. if (p->iotype != UPIO_AU)
  338. return offset;
  339. return au_io_in_map[offset];
  340. }
  341. static inline int map_8250_out_reg(struct uart_port *p, int offset)
  342. {
  343. if (p->iotype != UPIO_AU)
  344. return offset;
  345. return au_io_out_map[offset];
  346. }
  347. #elif defined(CONFIG_SERIAL_8250_RM9K)
  348. static const u8
  349. regmap_in[8] = {
  350. [UART_RX] = 0x00,
  351. [UART_IER] = 0x0c,
  352. [UART_IIR] = 0x14,
  353. [UART_LCR] = 0x1c,
  354. [UART_MCR] = 0x20,
  355. [UART_LSR] = 0x24,
  356. [UART_MSR] = 0x28,
  357. [UART_SCR] = 0x2c
  358. },
  359. regmap_out[8] = {
  360. [UART_TX] = 0x04,
  361. [UART_IER] = 0x0c,
  362. [UART_FCR] = 0x18,
  363. [UART_LCR] = 0x1c,
  364. [UART_MCR] = 0x20,
  365. [UART_LSR] = 0x24,
  366. [UART_MSR] = 0x28,
  367. [UART_SCR] = 0x2c
  368. };
  369. static inline int map_8250_in_reg(struct uart_port *p, int offset)
  370. {
  371. if (p->iotype != UPIO_RM9000)
  372. return offset;
  373. return regmap_in[offset];
  374. }
  375. static inline int map_8250_out_reg(struct uart_port *p, int offset)
  376. {
  377. if (p->iotype != UPIO_RM9000)
  378. return offset;
  379. return regmap_out[offset];
  380. }
  381. #else
  382. /* sane hardware needs no mapping */
  383. #define map_8250_in_reg(up, offset) (offset)
  384. #define map_8250_out_reg(up, offset) (offset)
  385. #endif
  386. static unsigned int hub6_serial_in(struct uart_port *p, int offset)
  387. {
  388. offset = map_8250_in_reg(p, offset) << p->regshift;
  389. outb(p->hub6 - 1 + offset, p->iobase);
  390. return inb(p->iobase + 1);
  391. }
  392. static void hub6_serial_out(struct uart_port *p, int offset, int value)
  393. {
  394. offset = map_8250_out_reg(p, offset) << p->regshift;
  395. outb(p->hub6 - 1 + offset, p->iobase);
  396. outb(value, p->iobase + 1);
  397. }
  398. static unsigned int mem_serial_in(struct uart_port *p, int offset)
  399. {
  400. offset = map_8250_in_reg(p, offset) << p->regshift;
  401. return readb(p->membase + offset);
  402. }
  403. static void mem_serial_out(struct uart_port *p, int offset, int value)
  404. {
  405. offset = map_8250_out_reg(p, offset) << p->regshift;
  406. writeb(value, p->membase + offset);
  407. }
  408. static void mem32_serial_out(struct uart_port *p, int offset, int value)
  409. {
  410. offset = map_8250_out_reg(p, offset) << p->regshift;
  411. writel(value, p->membase + offset);
  412. }
  413. static unsigned int mem32_serial_in(struct uart_port *p, int offset)
  414. {
  415. offset = map_8250_in_reg(p, offset) << p->regshift;
  416. return readl(p->membase + offset);
  417. }
  418. static unsigned int au_serial_in(struct uart_port *p, int offset)
  419. {
  420. offset = map_8250_in_reg(p, offset) << p->regshift;
  421. return __raw_readl(p->membase + offset);
  422. }
  423. static void au_serial_out(struct uart_port *p, int offset, int value)
  424. {
  425. offset = map_8250_out_reg(p, offset) << p->regshift;
  426. __raw_writel(value, p->membase + offset);
  427. }
  428. static unsigned int io_serial_in(struct uart_port *p, int offset)
  429. {
  430. offset = map_8250_in_reg(p, offset) << p->regshift;
  431. return inb(p->iobase + offset);
  432. }
  433. static void io_serial_out(struct uart_port *p, int offset, int value)
  434. {
  435. offset = map_8250_out_reg(p, offset) << p->regshift;
  436. outb(value, p->iobase + offset);
  437. }
  438. static int serial8250_default_handle_irq(struct uart_port *port);
  439. static void set_io_from_upio(struct uart_port *p)
  440. {
  441. struct uart_8250_port *up =
  442. container_of(p, struct uart_8250_port, port);
  443. up->dl_read = _serial_dl_read;
  444. up->dl_write = _serial_dl_write;
  445. switch (p->iotype) {
  446. case UPIO_HUB6:
  447. p->serial_in = hub6_serial_in;
  448. p->serial_out = hub6_serial_out;
  449. break;
  450. case UPIO_MEM:
  451. p->serial_in = mem_serial_in;
  452. p->serial_out = mem_serial_out;
  453. break;
  454. case UPIO_RM9000:
  455. case UPIO_MEM32:
  456. p->serial_in = mem32_serial_in;
  457. p->serial_out = mem32_serial_out;
  458. break;
  459. case UPIO_AU:
  460. p->serial_in = au_serial_in;
  461. p->serial_out = au_serial_out;
  462. break;
  463. default:
  464. p->serial_in = io_serial_in;
  465. p->serial_out = io_serial_out;
  466. break;
  467. }
  468. /* Remember loaded iotype */
  469. up->cur_iotype = p->iotype;
  470. p->handle_irq = serial8250_default_handle_irq;
  471. }
  472. static void
  473. serial_port_out_sync(struct uart_port *p, int offset, int value)
  474. {
  475. switch (p->iotype) {
  476. case UPIO_MEM:
  477. case UPIO_MEM32:
  478. case UPIO_AU:
  479. p->serial_out(p, offset, value);
  480. p->serial_in(p, UART_LCR); /* safe, no side-effects */
  481. break;
  482. default:
  483. p->serial_out(p, offset, value);
  484. }
  485. }
  486. /*
  487. * For the 16C950
  488. */
  489. static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
  490. {
  491. serial_out(up, UART_SCR, offset);
  492. serial_out(up, UART_ICR, value);
  493. }
  494. static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
  495. {
  496. unsigned int value;
  497. serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
  498. serial_out(up, UART_SCR, offset);
  499. value = serial_in(up, UART_ICR);
  500. serial_icr_write(up, UART_ACR, up->acr);
  501. return value;
  502. }
  503. /*
  504. * FIFO support.
  505. */
  506. static void serial8250_clear_fifos(struct uart_8250_port *p)
  507. {
  508. if (p->capabilities & UART_CAP_FIFO) {
  509. serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
  510. serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
  511. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  512. serial_out(p, UART_FCR, 0);
  513. }
  514. }
  515. void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p)
  516. {
  517. unsigned char fcr;
  518. serial8250_clear_fifos(p);
  519. fcr = uart_config[p->port.type].fcr;
  520. serial_out(p, UART_FCR, fcr);
  521. }
  522. EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos);
  523. /*
  524. * IER sleep support. UARTs which have EFRs need the "extended
  525. * capability" bit enabled. Note that on XR16C850s, we need to
  526. * reset LCR to write to IER.
  527. */
  528. static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
  529. {
  530. if (p->capabilities & UART_CAP_SLEEP) {
  531. if (p->capabilities & UART_CAP_EFR) {
  532. serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
  533. serial_out(p, UART_EFR, UART_EFR_ECB);
  534. serial_out(p, UART_LCR, 0);
  535. }
  536. serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
  537. if (p->capabilities & UART_CAP_EFR) {
  538. serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
  539. serial_out(p, UART_EFR, 0);
  540. serial_out(p, UART_LCR, 0);
  541. }
  542. }
  543. }
  544. #ifdef CONFIG_SERIAL_8250_RSA
  545. /*
  546. * Attempts to turn on the RSA FIFO. Returns zero on failure.
  547. * We set the port uart clock rate if we succeed.
  548. */
  549. static int __enable_rsa(struct uart_8250_port *up)
  550. {
  551. unsigned char mode;
  552. int result;
  553. mode = serial_in(up, UART_RSA_MSR);
  554. result = mode & UART_RSA_MSR_FIFO;
  555. if (!result) {
  556. serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
  557. mode = serial_in(up, UART_RSA_MSR);
  558. result = mode & UART_RSA_MSR_FIFO;
  559. }
  560. if (result)
  561. up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
  562. return result;
  563. }
  564. static void enable_rsa(struct uart_8250_port *up)
  565. {
  566. if (up->port.type == PORT_RSA) {
  567. if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
  568. spin_lock_irq(&up->port.lock);
  569. __enable_rsa(up);
  570. spin_unlock_irq(&up->port.lock);
  571. }
  572. if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
  573. serial_out(up, UART_RSA_FRR, 0);
  574. }
  575. }
  576. /*
  577. * Attempts to turn off the RSA FIFO. Returns zero on failure.
  578. * It is unknown why interrupts were disabled in here. However,
  579. * the caller is expected to preserve this behaviour by grabbing
  580. * the spinlock before calling this function.
  581. */
  582. static void disable_rsa(struct uart_8250_port *up)
  583. {
  584. unsigned char mode;
  585. int result;
  586. if (up->port.type == PORT_RSA &&
  587. up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
  588. spin_lock_irq(&up->port.lock);
  589. mode = serial_in(up, UART_RSA_MSR);
  590. result = !(mode & UART_RSA_MSR_FIFO);
  591. if (!result) {
  592. serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
  593. mode = serial_in(up, UART_RSA_MSR);
  594. result = !(mode & UART_RSA_MSR_FIFO);
  595. }
  596. if (result)
  597. up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
  598. spin_unlock_irq(&up->port.lock);
  599. }
  600. }
  601. #endif /* CONFIG_SERIAL_8250_RSA */
  602. /*
  603. * This is a quickie test to see how big the FIFO is.
  604. * It doesn't work at all the time, more's the pity.
  605. */
  606. static int size_fifo(struct uart_8250_port *up)
  607. {
  608. unsigned char old_fcr, old_mcr, old_lcr;
  609. unsigned short old_dl;
  610. int count;
  611. old_lcr = serial_in(up, UART_LCR);
  612. serial_out(up, UART_LCR, 0);
  613. old_fcr = serial_in(up, UART_FCR);
  614. old_mcr = serial_in(up, UART_MCR);
  615. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  616. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  617. serial_out(up, UART_MCR, UART_MCR_LOOP);
  618. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  619. old_dl = serial_dl_read(up);
  620. serial_dl_write(up, 0x0001);
  621. serial_out(up, UART_LCR, 0x03);
  622. for (count = 0; count < 256; count++)
  623. serial_out(up, UART_TX, count);
  624. mdelay(20);/* FIXME - schedule_timeout */
  625. for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
  626. (count < 256); count++)
  627. serial_in(up, UART_RX);
  628. serial_out(up, UART_FCR, old_fcr);
  629. serial_out(up, UART_MCR, old_mcr);
  630. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  631. serial_dl_write(up, old_dl);
  632. serial_out(up, UART_LCR, old_lcr);
  633. return count;
  634. }
  635. /*
  636. * Read UART ID using the divisor method - set DLL and DLM to zero
  637. * and the revision will be in DLL and device type in DLM. We
  638. * preserve the device state across this.
  639. */
  640. static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
  641. {
  642. unsigned char old_dll, old_dlm, old_lcr;
  643. unsigned int id;
  644. old_lcr = serial_in(p, UART_LCR);
  645. serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
  646. old_dll = serial_in(p, UART_DLL);
  647. old_dlm = serial_in(p, UART_DLM);
  648. serial_out(p, UART_DLL, 0);
  649. serial_out(p, UART_DLM, 0);
  650. id = serial_in(p, UART_DLL) | serial_in(p, UART_DLM) << 8;
  651. serial_out(p, UART_DLL, old_dll);
  652. serial_out(p, UART_DLM, old_dlm);
  653. serial_out(p, UART_LCR, old_lcr);
  654. return id;
  655. }
  656. /*
  657. * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
  658. * When this function is called we know it is at least a StarTech
  659. * 16650 V2, but it might be one of several StarTech UARTs, or one of
  660. * its clones. (We treat the broken original StarTech 16650 V1 as a
  661. * 16550, and why not? Startech doesn't seem to even acknowledge its
  662. * existence.)
  663. *
  664. * What evil have men's minds wrought...
  665. */
  666. static void autoconfig_has_efr(struct uart_8250_port *up)
  667. {
  668. unsigned int id1, id2, id3, rev;
  669. /*
  670. * Everything with an EFR has SLEEP
  671. */
  672. up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
  673. /*
  674. * First we check to see if it's an Oxford Semiconductor UART.
  675. *
  676. * If we have to do this here because some non-National
  677. * Semiconductor clone chips lock up if you try writing to the
  678. * LSR register (which serial_icr_read does)
  679. */
  680. /*
  681. * Check for Oxford Semiconductor 16C950.
  682. *
  683. * EFR [4] must be set else this test fails.
  684. *
  685. * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
  686. * claims that it's needed for 952 dual UART's (which are not
  687. * recommended for new designs).
  688. */
  689. up->acr = 0;
  690. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  691. serial_out(up, UART_EFR, UART_EFR_ECB);
  692. serial_out(up, UART_LCR, 0x00);
  693. id1 = serial_icr_read(up, UART_ID1);
  694. id2 = serial_icr_read(up, UART_ID2);
  695. id3 = serial_icr_read(up, UART_ID3);
  696. rev = serial_icr_read(up, UART_REV);
  697. DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
  698. if (id1 == 0x16 && id2 == 0xC9 &&
  699. (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
  700. up->port.type = PORT_16C950;
  701. /*
  702. * Enable work around for the Oxford Semiconductor 952 rev B
  703. * chip which causes it to seriously miscalculate baud rates
  704. * when DLL is 0.
  705. */
  706. if (id3 == 0x52 && rev == 0x01)
  707. up->bugs |= UART_BUG_QUOT;
  708. return;
  709. }
  710. /*
  711. * We check for a XR16C850 by setting DLL and DLM to 0, and then
  712. * reading back DLL and DLM. The chip type depends on the DLM
  713. * value read back:
  714. * 0x10 - XR16C850 and the DLL contains the chip revision.
  715. * 0x12 - XR16C2850.
  716. * 0x14 - XR16C854.
  717. */
  718. id1 = autoconfig_read_divisor_id(up);
  719. DEBUG_AUTOCONF("850id=%04x ", id1);
  720. id2 = id1 >> 8;
  721. if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
  722. up->port.type = PORT_16850;
  723. return;
  724. }
  725. /*
  726. * It wasn't an XR16C850.
  727. *
  728. * We distinguish between the '654 and the '650 by counting
  729. * how many bytes are in the FIFO. I'm using this for now,
  730. * since that's the technique that was sent to me in the
  731. * serial driver update, but I'm not convinced this works.
  732. * I've had problems doing this in the past. -TYT
  733. */
  734. if (size_fifo(up) == 64)
  735. up->port.type = PORT_16654;
  736. else
  737. up->port.type = PORT_16650V2;
  738. }
  739. /*
  740. * We detected a chip without a FIFO. Only two fall into
  741. * this category - the original 8250 and the 16450. The
  742. * 16450 has a scratch register (accessible with LCR=0)
  743. */
  744. static void autoconfig_8250(struct uart_8250_port *up)
  745. {
  746. unsigned char scratch, status1, status2;
  747. up->port.type = PORT_8250;
  748. scratch = serial_in(up, UART_SCR);
  749. serial_out(up, UART_SCR, 0xa5);
  750. status1 = serial_in(up, UART_SCR);
  751. serial_out(up, UART_SCR, 0x5a);
  752. status2 = serial_in(up, UART_SCR);
  753. serial_out(up, UART_SCR, scratch);
  754. if (status1 == 0xa5 && status2 == 0x5a)
  755. up->port.type = PORT_16450;
  756. }
  757. static int broken_efr(struct uart_8250_port *up)
  758. {
  759. /*
  760. * Exar ST16C2550 "A2" devices incorrectly detect as
  761. * having an EFR, and report an ID of 0x0201. See
  762. * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
  763. */
  764. if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
  765. return 1;
  766. return 0;
  767. }
  768. static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
  769. {
  770. unsigned char status;
  771. status = serial_in(up, 0x04); /* EXCR2 */
  772. #define PRESL(x) ((x) & 0x30)
  773. if (PRESL(status) == 0x10) {
  774. /* already in high speed mode */
  775. return 0;
  776. } else {
  777. status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
  778. status |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
  779. serial_out(up, 0x04, status);
  780. }
  781. return 1;
  782. }
  783. /*
  784. * We know that the chip has FIFOs. Does it have an EFR? The
  785. * EFR is located in the same register position as the IIR and
  786. * we know the top two bits of the IIR are currently set. The
  787. * EFR should contain zero. Try to read the EFR.
  788. */
  789. static void autoconfig_16550a(struct uart_8250_port *up)
  790. {
  791. unsigned char status1, status2;
  792. unsigned int iersave;
  793. up->port.type = PORT_16550A;
  794. up->capabilities |= UART_CAP_FIFO;
  795. /*
  796. * Check for presence of the EFR when DLAB is set.
  797. * Only ST16C650V1 UARTs pass this test.
  798. */
  799. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  800. if (serial_in(up, UART_EFR) == 0) {
  801. serial_out(up, UART_EFR, 0xA8);
  802. if (serial_in(up, UART_EFR) != 0) {
  803. DEBUG_AUTOCONF("EFRv1 ");
  804. up->port.type = PORT_16650;
  805. up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
  806. } else {
  807. DEBUG_AUTOCONF("Motorola 8xxx DUART ");
  808. }
  809. serial_out(up, UART_EFR, 0);
  810. return;
  811. }
  812. /*
  813. * Maybe it requires 0xbf to be written to the LCR.
  814. * (other ST16C650V2 UARTs, TI16C752A, etc)
  815. */
  816. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  817. if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
  818. DEBUG_AUTOCONF("EFRv2 ");
  819. autoconfig_has_efr(up);
  820. return;
  821. }
  822. /*
  823. * Check for a National Semiconductor SuperIO chip.
  824. * Attempt to switch to bank 2, read the value of the LOOP bit
  825. * from EXCR1. Switch back to bank 0, change it in MCR. Then
  826. * switch back to bank 2, read it from EXCR1 again and check
  827. * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
  828. */
  829. serial_out(up, UART_LCR, 0);
  830. status1 = serial_in(up, UART_MCR);
  831. serial_out(up, UART_LCR, 0xE0);
  832. status2 = serial_in(up, 0x02); /* EXCR1 */
  833. if (!((status2 ^ status1) & UART_MCR_LOOP)) {
  834. serial_out(up, UART_LCR, 0);
  835. serial_out(up, UART_MCR, status1 ^ UART_MCR_LOOP);
  836. serial_out(up, UART_LCR, 0xE0);
  837. status2 = serial_in(up, 0x02); /* EXCR1 */
  838. serial_out(up, UART_LCR, 0);
  839. serial_out(up, UART_MCR, status1);
  840. if ((status2 ^ status1) & UART_MCR_LOOP) {
  841. unsigned short quot;
  842. serial_out(up, UART_LCR, 0xE0);
  843. quot = serial_dl_read(up);
  844. quot <<= 3;
  845. if (ns16550a_goto_highspeed(up))
  846. serial_dl_write(up, quot);
  847. serial_out(up, UART_LCR, 0);
  848. up->port.uartclk = 921600*16;
  849. up->port.type = PORT_NS16550A;
  850. up->capabilities |= UART_NATSEMI;
  851. return;
  852. }
  853. }
  854. /*
  855. * No EFR. Try to detect a TI16750, which only sets bit 5 of
  856. * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
  857. * Try setting it with and without DLAB set. Cheap clones
  858. * set bit 5 without DLAB set.
  859. */
  860. serial_out(up, UART_LCR, 0);
  861. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
  862. status1 = serial_in(up, UART_IIR) >> 5;
  863. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  864. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  865. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
  866. status2 = serial_in(up, UART_IIR) >> 5;
  867. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  868. serial_out(up, UART_LCR, 0);
  869. DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
  870. if (status1 == 6 && status2 == 7) {
  871. up->port.type = PORT_16750;
  872. up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
  873. return;
  874. }
  875. /*
  876. * Try writing and reading the UART_IER_UUE bit (b6).
  877. * If it works, this is probably one of the Xscale platform's
  878. * internal UARTs.
  879. * We're going to explicitly set the UUE bit to 0 before
  880. * trying to write and read a 1 just to make sure it's not
  881. * already a 1 and maybe locked there before we even start start.
  882. */
  883. iersave = serial_in(up, UART_IER);
  884. serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
  885. if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
  886. /*
  887. * OK it's in a known zero state, try writing and reading
  888. * without disturbing the current state of the other bits.
  889. */
  890. serial_out(up, UART_IER, iersave | UART_IER_UUE);
  891. if (serial_in(up, UART_IER) & UART_IER_UUE) {
  892. /*
  893. * It's an Xscale.
  894. * We'll leave the UART_IER_UUE bit set to 1 (enabled).
  895. */
  896. DEBUG_AUTOCONF("Xscale ");
  897. up->port.type = PORT_XSCALE;
  898. up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
  899. return;
  900. }
  901. } else {
  902. /*
  903. * If we got here we couldn't force the IER_UUE bit to 0.
  904. * Log it and continue.
  905. */
  906. DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
  907. }
  908. serial_out(up, UART_IER, iersave);
  909. /*
  910. * Exar uarts have EFR in a weird location
  911. */
  912. if (up->port.flags & UPF_EXAR_EFR) {
  913. up->port.type = PORT_XR17D15X;
  914. up->capabilities |= UART_CAP_AFE | UART_CAP_EFR;
  915. }
  916. /*
  917. * We distinguish between 16550A and U6 16550A by counting
  918. * how many bytes are in the FIFO.
  919. */
  920. if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
  921. up->port.type = PORT_U6_16550A;
  922. up->capabilities |= UART_CAP_AFE;
  923. }
  924. }
  925. /*
  926. * This routine is called by rs_init() to initialize a specific serial
  927. * port. It determines what type of UART chip this serial port is
  928. * using: 8250, 16450, 16550, 16550A. The important question is
  929. * whether or not this UART is a 16550A or not, since this will
  930. * determine whether or not we can use its FIFO features or not.
  931. */
  932. static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
  933. {
  934. unsigned char status1, scratch, scratch2, scratch3;
  935. unsigned char save_lcr, save_mcr;
  936. struct uart_port *port = &up->port;
  937. unsigned long flags;
  938. if (!port->iobase && !port->mapbase && !port->membase)
  939. return;
  940. DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
  941. serial_index(port), port->iobase, port->membase);
  942. /*
  943. * We really do need global IRQs disabled here - we're going to
  944. * be frobbing the chips IRQ enable register to see if it exists.
  945. */
  946. spin_lock_irqsave(&port->lock, flags);
  947. up->capabilities = 0;
  948. up->bugs = 0;
  949. if (!(port->flags & UPF_BUGGY_UART)) {
  950. /*
  951. * Do a simple existence test first; if we fail this,
  952. * there's no point trying anything else.
  953. *
  954. * 0x80 is used as a nonsense port to prevent against
  955. * false positives due to ISA bus float. The
  956. * assumption is that 0x80 is a non-existent port;
  957. * which should be safe since include/asm/io.h also
  958. * makes this assumption.
  959. *
  960. * Note: this is safe as long as MCR bit 4 is clear
  961. * and the device is in "PC" mode.
  962. */
  963. scratch = serial_in(up, UART_IER);
  964. serial_out(up, UART_IER, 0);
  965. #ifdef __i386__
  966. outb(0xff, 0x080);
  967. #endif
  968. /*
  969. * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
  970. * 16C754B) allow only to modify them if an EFR bit is set.
  971. */
  972. scratch2 = serial_in(up, UART_IER) & 0x0f;
  973. serial_out(up, UART_IER, 0x0F);
  974. #ifdef __i386__
  975. outb(0, 0x080);
  976. #endif
  977. scratch3 = serial_in(up, UART_IER) & 0x0f;
  978. serial_out(up, UART_IER, scratch);
  979. if (scratch2 != 0 || scratch3 != 0x0F) {
  980. /*
  981. * We failed; there's nothing here
  982. */
  983. DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
  984. scratch2, scratch3);
  985. goto out;
  986. }
  987. }
  988. save_mcr = serial_in(up, UART_MCR);
  989. save_lcr = serial_in(up, UART_LCR);
  990. /*
  991. * Check to see if a UART is really there. Certain broken
  992. * internal modems based on the Rockwell chipset fail this
  993. * test, because they apparently don't implement the loopback
  994. * test mode. So this test is skipped on the COM 1 through
  995. * COM 4 ports. This *should* be safe, since no board
  996. * manufacturer would be stupid enough to design a board
  997. * that conflicts with COM 1-4 --- we hope!
  998. */
  999. if (!(port->flags & UPF_SKIP_TEST)) {
  1000. serial_out(up, UART_MCR, UART_MCR_LOOP | 0x0A);
  1001. status1 = serial_in(up, UART_MSR) & 0xF0;
  1002. serial_out(up, UART_MCR, save_mcr);
  1003. if (status1 != 0x90) {
  1004. DEBUG_AUTOCONF("LOOP test failed (%02x) ",
  1005. status1);
  1006. goto out;
  1007. }
  1008. }
  1009. /*
  1010. * We're pretty sure there's a port here. Lets find out what
  1011. * type of port it is. The IIR top two bits allows us to find
  1012. * out if it's 8250 or 16450, 16550, 16550A or later. This
  1013. * determines what we test for next.
  1014. *
  1015. * We also initialise the EFR (if any) to zero for later. The
  1016. * EFR occupies the same register location as the FCR and IIR.
  1017. */
  1018. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  1019. serial_out(up, UART_EFR, 0);
  1020. serial_out(up, UART_LCR, 0);
  1021. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  1022. scratch = serial_in(up, UART_IIR) >> 6;
  1023. DEBUG_AUTOCONF("iir=%d ", scratch);
  1024. switch (scratch) {
  1025. case 0:
  1026. autoconfig_8250(up);
  1027. break;
  1028. case 1:
  1029. port->type = PORT_UNKNOWN;
  1030. break;
  1031. case 2:
  1032. port->type = PORT_16550;
  1033. break;
  1034. case 3:
  1035. autoconfig_16550a(up);
  1036. break;
  1037. }
  1038. #ifdef CONFIG_SERIAL_8250_RSA
  1039. /*
  1040. * Only probe for RSA ports if we got the region.
  1041. */
  1042. if (port->type == PORT_16550A && probeflags & PROBE_RSA) {
  1043. int i;
  1044. for (i = 0 ; i < probe_rsa_count; ++i) {
  1045. if (probe_rsa[i] == port->iobase && __enable_rsa(up)) {
  1046. port->type = PORT_RSA;
  1047. break;
  1048. }
  1049. }
  1050. }
  1051. #endif
  1052. serial_out(up, UART_LCR, save_lcr);
  1053. if (up->capabilities != uart_config[port->type].flags) {
  1054. printk(KERN_WARNING
  1055. "ttyS%d: detected caps %08x should be %08x\n",
  1056. serial_index(port), up->capabilities,
  1057. uart_config[port->type].flags);
  1058. }
  1059. port->fifosize = uart_config[up->port.type].fifo_size;
  1060. up->capabilities = uart_config[port->type].flags;
  1061. up->tx_loadsz = uart_config[port->type].tx_loadsz;
  1062. if (port->type == PORT_UNKNOWN)
  1063. goto out;
  1064. /*
  1065. * Reset the UART.
  1066. */
  1067. #ifdef CONFIG_SERIAL_8250_RSA
  1068. if (port->type == PORT_RSA)
  1069. serial_out(up, UART_RSA_FRR, 0);
  1070. #endif
  1071. serial_out(up, UART_MCR, save_mcr);
  1072. serial8250_clear_fifos(up);
  1073. serial_in(up, UART_RX);
  1074. if (up->capabilities & UART_CAP_UUE)
  1075. serial_out(up, UART_IER, UART_IER_UUE);
  1076. else
  1077. serial_out(up, UART_IER, 0);
  1078. out:
  1079. spin_unlock_irqrestore(&port->lock, flags);
  1080. DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
  1081. }
  1082. static void autoconfig_irq(struct uart_8250_port *up)
  1083. {
  1084. struct uart_port *port = &up->port;
  1085. unsigned char save_mcr, save_ier;
  1086. unsigned char save_ICP = 0;
  1087. unsigned int ICP = 0;
  1088. unsigned long irqs;
  1089. int irq;
  1090. if (port->flags & UPF_FOURPORT) {
  1091. ICP = (port->iobase & 0xfe0) | 0x1f;
  1092. save_ICP = inb_p(ICP);
  1093. outb_p(0x80, ICP);
  1094. inb_p(ICP);
  1095. }
  1096. /* forget possible initially masked and pending IRQ */
  1097. probe_irq_off(probe_irq_on());
  1098. save_mcr = serial_in(up, UART_MCR);
  1099. save_ier = serial_in(up, UART_IER);
  1100. serial_out(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
  1101. irqs = probe_irq_on();
  1102. serial_out(up, UART_MCR, 0);
  1103. udelay(10);
  1104. if (port->flags & UPF_FOURPORT) {
  1105. serial_out(up, UART_MCR,
  1106. UART_MCR_DTR | UART_MCR_RTS);
  1107. } else {
  1108. serial_out(up, UART_MCR,
  1109. UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
  1110. }
  1111. serial_out(up, UART_IER, 0x0f); /* enable all intrs */
  1112. serial_in(up, UART_LSR);
  1113. serial_in(up, UART_RX);
  1114. serial_in(up, UART_IIR);
  1115. serial_in(up, UART_MSR);
  1116. serial_out(up, UART_TX, 0xFF);
  1117. udelay(20);
  1118. irq = probe_irq_off(irqs);
  1119. serial_out(up, UART_MCR, save_mcr);
  1120. serial_out(up, UART_IER, save_ier);
  1121. if (port->flags & UPF_FOURPORT)
  1122. outb_p(save_ICP, ICP);
  1123. port->irq = (irq > 0) ? irq : 0;
  1124. }
  1125. static inline void __stop_tx(struct uart_8250_port *p)
  1126. {
  1127. if (p->ier & UART_IER_THRI) {
  1128. p->ier &= ~UART_IER_THRI;
  1129. serial_out(p, UART_IER, p->ier);
  1130. }
  1131. }
  1132. static void serial8250_stop_tx(struct uart_port *port)
  1133. {
  1134. struct uart_8250_port *up =
  1135. container_of(port, struct uart_8250_port, port);
  1136. __stop_tx(up);
  1137. /*
  1138. * We really want to stop the transmitter from sending.
  1139. */
  1140. if (port->type == PORT_16C950) {
  1141. up->acr |= UART_ACR_TXDIS;
  1142. serial_icr_write(up, UART_ACR, up->acr);
  1143. }
  1144. }
  1145. static void serial8250_start_tx(struct uart_port *port)
  1146. {
  1147. struct uart_8250_port *up =
  1148. container_of(port, struct uart_8250_port, port);
  1149. if (!(up->ier & UART_IER_THRI)) {
  1150. up->ier |= UART_IER_THRI;
  1151. serial_port_out(port, UART_IER, up->ier);
  1152. if (up->bugs & UART_BUG_TXEN) {
  1153. unsigned char lsr;
  1154. lsr = serial_in(up, UART_LSR);
  1155. up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
  1156. if ((port->type == PORT_RM9000) ?
  1157. (lsr & UART_LSR_THRE) :
  1158. (lsr & UART_LSR_TEMT))
  1159. serial8250_tx_chars(up);
  1160. }
  1161. }
  1162. /*
  1163. * Re-enable the transmitter if we disabled it.
  1164. */
  1165. if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
  1166. up->acr &= ~UART_ACR_TXDIS;
  1167. serial_icr_write(up, UART_ACR, up->acr);
  1168. }
  1169. }
  1170. static void serial8250_stop_rx(struct uart_port *port)
  1171. {
  1172. struct uart_8250_port *up =
  1173. container_of(port, struct uart_8250_port, port);
  1174. up->ier &= ~UART_IER_RLSI;
  1175. up->port.read_status_mask &= ~UART_LSR_DR;
  1176. serial_port_out(port, UART_IER, up->ier);
  1177. }
  1178. static void serial8250_enable_ms(struct uart_port *port)
  1179. {
  1180. struct uart_8250_port *up =
  1181. container_of(port, struct uart_8250_port, port);
  1182. /* no MSR capabilities */
  1183. if (up->bugs & UART_BUG_NOMSR)
  1184. return;
  1185. up->ier |= UART_IER_MSI;
  1186. serial_port_out(port, UART_IER, up->ier);
  1187. }
  1188. /*
  1189. * serial8250_rx_chars: processes according to the passed in LSR
  1190. * value, and returns the remaining LSR bits not handled
  1191. * by this Rx routine.
  1192. */
  1193. unsigned char
  1194. serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
  1195. {
  1196. struct uart_port *port = &up->port;
  1197. struct tty_struct *tty = port->state->port.tty;
  1198. unsigned char ch;
  1199. int max_count = 256;
  1200. char flag;
  1201. do {
  1202. if (likely(lsr & UART_LSR_DR))
  1203. ch = serial_in(up, UART_RX);
  1204. else
  1205. /*
  1206. * Intel 82571 has a Serial Over Lan device that will
  1207. * set UART_LSR_BI without setting UART_LSR_DR when
  1208. * it receives a break. To avoid reading from the
  1209. * receive buffer without UART_LSR_DR bit set, we
  1210. * just force the read character to be 0
  1211. */
  1212. ch = 0;
  1213. flag = TTY_NORMAL;
  1214. port->icount.rx++;
  1215. lsr |= up->lsr_saved_flags;
  1216. up->lsr_saved_flags = 0;
  1217. if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
  1218. if (lsr & UART_LSR_BI) {
  1219. lsr &= ~(UART_LSR_FE | UART_LSR_PE);
  1220. port->icount.brk++;
  1221. /*
  1222. * We do the SysRQ and SAK checking
  1223. * here because otherwise the break
  1224. * may get masked by ignore_status_mask
  1225. * or read_status_mask.
  1226. */
  1227. if (uart_handle_break(port))
  1228. goto ignore_char;
  1229. } else if (lsr & UART_LSR_PE)
  1230. port->icount.parity++;
  1231. else if (lsr & UART_LSR_FE)
  1232. port->icount.frame++;
  1233. if (lsr & UART_LSR_OE)
  1234. port->icount.overrun++;
  1235. /*
  1236. * Mask off conditions which should be ignored.
  1237. */
  1238. lsr &= port->read_status_mask;
  1239. if (lsr & UART_LSR_BI) {
  1240. DEBUG_INTR("handling break....");
  1241. flag = TTY_BREAK;
  1242. } else if (lsr & UART_LSR_PE)
  1243. flag = TTY_PARITY;
  1244. else if (lsr & UART_LSR_FE)
  1245. flag = TTY_FRAME;
  1246. }
  1247. if (uart_handle_sysrq_char(port, ch))
  1248. goto ignore_char;
  1249. uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
  1250. ignore_char:
  1251. lsr = serial_in(up, UART_LSR);
  1252. } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
  1253. spin_unlock(&port->lock);
  1254. tty_flip_buffer_push(tty);
  1255. spin_lock(&port->lock);
  1256. return lsr;
  1257. }
  1258. EXPORT_SYMBOL_GPL(serial8250_rx_chars);
  1259. void serial8250_tx_chars(struct uart_8250_port *up)
  1260. {
  1261. struct uart_port *port = &up->port;
  1262. struct circ_buf *xmit = &port->state->xmit;
  1263. int count;
  1264. if (port->x_char) {
  1265. serial_out(up, UART_TX, port->x_char);
  1266. port->icount.tx++;
  1267. port->x_char = 0;
  1268. return;
  1269. }
  1270. if (uart_tx_stopped(port)) {
  1271. serial8250_stop_tx(port);
  1272. return;
  1273. }
  1274. if (uart_circ_empty(xmit)) {
  1275. __stop_tx(up);
  1276. return;
  1277. }
  1278. count = up->tx_loadsz;
  1279. do {
  1280. serial_out(up, UART_TX, xmit->buf[xmit->tail]);
  1281. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  1282. port->icount.tx++;
  1283. if (uart_circ_empty(xmit))
  1284. break;
  1285. } while (--count > 0);
  1286. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  1287. uart_write_wakeup(port);
  1288. DEBUG_INTR("THRE...");
  1289. if (uart_circ_empty(xmit))
  1290. __stop_tx(up);
  1291. }
  1292. EXPORT_SYMBOL_GPL(serial8250_tx_chars);
  1293. unsigned int serial8250_modem_status(struct uart_8250_port *up)
  1294. {
  1295. struct uart_port *port = &up->port;
  1296. unsigned int status = serial_in(up, UART_MSR);
  1297. status |= up->msr_saved_flags;
  1298. up->msr_saved_flags = 0;
  1299. if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
  1300. port->state != NULL) {
  1301. if (status & UART_MSR_TERI)
  1302. port->icount.rng++;
  1303. if (status & UART_MSR_DDSR)
  1304. port->icount.dsr++;
  1305. if (status & UART_MSR_DDCD)
  1306. uart_handle_dcd_change(port, status & UART_MSR_DCD);
  1307. if (status & UART_MSR_DCTS)
  1308. uart_handle_cts_change(port, status & UART_MSR_CTS);
  1309. wake_up_interruptible(&port->state->port.delta_msr_wait);
  1310. }
  1311. return status;
  1312. }
  1313. EXPORT_SYMBOL_GPL(serial8250_modem_status);
  1314. /*
  1315. * This handles the interrupt from one port.
  1316. */
  1317. int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
  1318. {
  1319. unsigned char status;
  1320. unsigned long flags;
  1321. struct uart_8250_port *up =
  1322. container_of(port, struct uart_8250_port, port);
  1323. if (iir & UART_IIR_NO_INT)
  1324. return 0;
  1325. spin_lock_irqsave(&port->lock, flags);
  1326. status = serial_port_in(port, UART_LSR);
  1327. DEBUG_INTR("status = %x...", status);
  1328. if (status & (UART_LSR_DR | UART_LSR_BI))
  1329. status = serial8250_rx_chars(up, status);
  1330. serial8250_modem_status(up);
  1331. if (status & UART_LSR_THRE)
  1332. serial8250_tx_chars(up);
  1333. spin_unlock_irqrestore(&port->lock, flags);
  1334. return 1;
  1335. }
  1336. EXPORT_SYMBOL_GPL(serial8250_handle_irq);
  1337. static int serial8250_default_handle_irq(struct uart_port *port)
  1338. {
  1339. unsigned int iir = serial_port_in(port, UART_IIR);
  1340. return serial8250_handle_irq(port, iir);
  1341. }
  1342. /*
  1343. * This is the serial driver's interrupt routine.
  1344. *
  1345. * Arjan thinks the old way was overly complex, so it got simplified.
  1346. * Alan disagrees, saying that need the complexity to handle the weird
  1347. * nature of ISA shared interrupts. (This is a special exception.)
  1348. *
  1349. * In order to handle ISA shared interrupts properly, we need to check
  1350. * that all ports have been serviced, and therefore the ISA interrupt
  1351. * line has been de-asserted.
  1352. *
  1353. * This means we need to loop through all ports. checking that they
  1354. * don't have an interrupt pending.
  1355. */
  1356. static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
  1357. {
  1358. struct irq_info *i = dev_id;
  1359. struct list_head *l, *end = NULL;
  1360. int pass_counter = 0, handled = 0;
  1361. DEBUG_INTR("serial8250_interrupt(%d)...", irq);
  1362. spin_lock(&i->lock);
  1363. l = i->head;
  1364. do {
  1365. struct uart_8250_port *up;
  1366. struct uart_port *port;
  1367. up = list_entry(l, struct uart_8250_port, list);
  1368. port = &up->port;
  1369. if (port->handle_irq(port)) {
  1370. handled = 1;
  1371. end = NULL;
  1372. } else if (end == NULL)
  1373. end = l;
  1374. l = l->next;
  1375. if (l == i->head && pass_counter++ > PASS_LIMIT) {
  1376. /* If we hit this, we're dead. */
  1377. printk_ratelimited(KERN_ERR
  1378. "serial8250: too much work for irq%d\n", irq);
  1379. break;
  1380. }
  1381. } while (l != end);
  1382. spin_unlock(&i->lock);
  1383. DEBUG_INTR("end.\n");
  1384. return IRQ_RETVAL(handled);
  1385. }
  1386. /*
  1387. * To support ISA shared interrupts, we need to have one interrupt
  1388. * handler that ensures that the IRQ line has been deasserted
  1389. * before returning. Failing to do this will result in the IRQ
  1390. * line being stuck active, and, since ISA irqs are edge triggered,
  1391. * no more IRQs will be seen.
  1392. */
  1393. static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
  1394. {
  1395. spin_lock_irq(&i->lock);
  1396. if (!list_empty(i->head)) {
  1397. if (i->head == &up->list)
  1398. i->head = i->head->next;
  1399. list_del(&up->list);
  1400. } else {
  1401. BUG_ON(i->head != &up->list);
  1402. i->head = NULL;
  1403. }
  1404. spin_unlock_irq(&i->lock);
  1405. /* List empty so throw away the hash node */
  1406. if (i->head == NULL) {
  1407. hlist_del(&i->node);
  1408. kfree(i);
  1409. }
  1410. }
  1411. static int serial_link_irq_chain(struct uart_8250_port *up)
  1412. {
  1413. struct hlist_head *h;
  1414. struct hlist_node *n;
  1415. struct irq_info *i;
  1416. int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
  1417. mutex_lock(&hash_mutex);
  1418. h = &irq_lists[up->port.irq % NR_IRQ_HASH];
  1419. hlist_for_each(n, h) {
  1420. i = hlist_entry(n, struct irq_info, node);
  1421. if (i->irq == up->port.irq)
  1422. break;
  1423. }
  1424. if (n == NULL) {
  1425. i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
  1426. if (i == NULL) {
  1427. mutex_unlock(&hash_mutex);
  1428. return -ENOMEM;
  1429. }
  1430. spin_lock_init(&i->lock);
  1431. i->irq = up->port.irq;
  1432. hlist_add_head(&i->node, h);
  1433. }
  1434. mutex_unlock(&hash_mutex);
  1435. spin_lock_irq(&i->lock);
  1436. if (i->head) {
  1437. list_add(&up->list, i->head);
  1438. spin_unlock_irq(&i->lock);
  1439. ret = 0;
  1440. } else {
  1441. INIT_LIST_HEAD(&up->list);
  1442. i->head = &up->list;
  1443. spin_unlock_irq(&i->lock);
  1444. irq_flags |= up->port.irqflags;
  1445. ret = request_irq(up->port.irq, serial8250_interrupt,
  1446. irq_flags, "serial", i);
  1447. if (ret < 0)
  1448. serial_do_unlink(i, up);
  1449. }
  1450. return ret;
  1451. }
  1452. static void serial_unlink_irq_chain(struct uart_8250_port *up)
  1453. {
  1454. struct irq_info *i;
  1455. struct hlist_node *n;
  1456. struct hlist_head *h;
  1457. mutex_lock(&hash_mutex);
  1458. h = &irq_lists[up->port.irq % NR_IRQ_HASH];
  1459. hlist_for_each(n, h) {
  1460. i = hlist_entry(n, struct irq_info, node);
  1461. if (i->irq == up->port.irq)
  1462. break;
  1463. }
  1464. BUG_ON(n == NULL);
  1465. BUG_ON(i->head == NULL);
  1466. if (list_empty(i->head))
  1467. free_irq(up->port.irq, i);
  1468. serial_do_unlink(i, up);
  1469. mutex_unlock(&hash_mutex);
  1470. }
  1471. /*
  1472. * This function is used to handle ports that do not have an
  1473. * interrupt. This doesn't work very well for 16450's, but gives
  1474. * barely passable results for a 16550A. (Although at the expense
  1475. * of much CPU overhead).
  1476. */
  1477. static void serial8250_timeout(unsigned long data)
  1478. {
  1479. struct uart_8250_port *up = (struct uart_8250_port *)data;
  1480. up->port.handle_irq(&up->port);
  1481. mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
  1482. }
  1483. static void serial8250_backup_timeout(unsigned long data)
  1484. {
  1485. struct uart_8250_port *up = (struct uart_8250_port *)data;
  1486. unsigned int iir, ier = 0, lsr;
  1487. unsigned long flags;
  1488. spin_lock_irqsave(&up->port.lock, flags);
  1489. /*
  1490. * Must disable interrupts or else we risk racing with the interrupt
  1491. * based handler.
  1492. */
  1493. if (up->port.irq) {
  1494. ier = serial_in(up, UART_IER);
  1495. serial_out(up, UART_IER, 0);
  1496. }
  1497. iir = serial_in(up, UART_IIR);
  1498. /*
  1499. * This should be a safe test for anyone who doesn't trust the
  1500. * IIR bits on their UART, but it's specifically designed for
  1501. * the "Diva" UART used on the management processor on many HP
  1502. * ia64 and parisc boxes.
  1503. */
  1504. lsr = serial_in(up, UART_LSR);
  1505. up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
  1506. if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
  1507. (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
  1508. (lsr & UART_LSR_THRE)) {
  1509. iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
  1510. iir |= UART_IIR_THRI;
  1511. }
  1512. if (!(iir & UART_IIR_NO_INT))
  1513. serial8250_tx_chars(up);
  1514. if (up->port.irq)
  1515. serial_out(up, UART_IER, ier);
  1516. spin_unlock_irqrestore(&up->port.lock, flags);
  1517. /* Standard timer interval plus 0.2s to keep the port running */
  1518. mod_timer(&up->timer,
  1519. jiffies + uart_poll_timeout(&up->port) + HZ / 5);
  1520. }
  1521. static unsigned int serial8250_tx_empty(struct uart_port *port)
  1522. {
  1523. struct uart_8250_port *up =
  1524. container_of(port, struct uart_8250_port, port);
  1525. unsigned long flags;
  1526. unsigned int lsr;
  1527. spin_lock_irqsave(&port->lock, flags);
  1528. lsr = serial_port_in(port, UART_LSR);
  1529. up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
  1530. spin_unlock_irqrestore(&port->lock, flags);
  1531. return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
  1532. }
  1533. static unsigned int serial8250_get_mctrl(struct uart_port *port)
  1534. {
  1535. struct uart_8250_port *up =
  1536. container_of(port, struct uart_8250_port, port);
  1537. unsigned int status;
  1538. unsigned int ret;
  1539. status = serial8250_modem_status(up);
  1540. ret = 0;
  1541. if (status & UART_MSR_DCD)
  1542. ret |= TIOCM_CAR;
  1543. if (status & UART_MSR_RI)
  1544. ret |= TIOCM_RNG;
  1545. if (status & UART_MSR_DSR)
  1546. ret |= TIOCM_DSR;
  1547. if (status & UART_MSR_CTS)
  1548. ret |= TIOCM_CTS;
  1549. return ret;
  1550. }
  1551. static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1552. {
  1553. struct uart_8250_port *up =
  1554. container_of(port, struct uart_8250_port, port);
  1555. unsigned char mcr = 0;
  1556. if (mctrl & TIOCM_RTS)
  1557. mcr |= UART_MCR_RTS;
  1558. if (mctrl & TIOCM_DTR)
  1559. mcr |= UART_MCR_DTR;
  1560. if (mctrl & TIOCM_OUT1)
  1561. mcr |= UART_MCR_OUT1;
  1562. if (mctrl & TIOCM_OUT2)
  1563. mcr |= UART_MCR_OUT2;
  1564. if (mctrl & TIOCM_LOOP)
  1565. mcr |= UART_MCR_LOOP;
  1566. mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
  1567. serial_port_out(port, UART_MCR, mcr);
  1568. }
  1569. static void serial8250_break_ctl(struct uart_port *port, int break_state)
  1570. {
  1571. struct uart_8250_port *up =
  1572. container_of(port, struct uart_8250_port, port);
  1573. unsigned long flags;
  1574. spin_lock_irqsave(&port->lock, flags);
  1575. if (break_state == -1)
  1576. up->lcr |= UART_LCR_SBC;
  1577. else
  1578. up->lcr &= ~UART_LCR_SBC;
  1579. serial_port_out(port, UART_LCR, up->lcr);
  1580. spin_unlock_irqrestore(&port->lock, flags);
  1581. }
  1582. /*
  1583. * Wait for transmitter & holding register to empty
  1584. */
  1585. static void wait_for_xmitr(struct uart_8250_port *up, int bits)
  1586. {
  1587. unsigned int status, tmout = 10000;
  1588. /* Wait up to 10ms for the character(s) to be sent. */
  1589. for (;;) {
  1590. status = serial_in(up, UART_LSR);
  1591. up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
  1592. if ((status & bits) == bits)
  1593. break;
  1594. if (--tmout == 0)
  1595. break;
  1596. udelay(1);
  1597. }
  1598. /* Wait up to 1s for flow control if necessary */
  1599. if (up->port.flags & UPF_CONS_FLOW) {
  1600. unsigned int tmout;
  1601. for (tmout = 1000000; tmout; tmout--) {
  1602. unsigned int msr = serial_in(up, UART_MSR);
  1603. up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
  1604. if (msr & UART_MSR_CTS)
  1605. break;
  1606. udelay(1);
  1607. touch_nmi_watchdog();
  1608. }
  1609. }
  1610. }
  1611. #ifdef CONFIG_CONSOLE_POLL
  1612. /*
  1613. * Console polling routines for writing and reading from the uart while
  1614. * in an interrupt or debug context.
  1615. */
  1616. static int serial8250_get_poll_char(struct uart_port *port)
  1617. {
  1618. unsigned char lsr = serial_port_in(port, UART_LSR);
  1619. if (!(lsr & UART_LSR_DR))
  1620. return NO_POLL_CHAR;
  1621. return serial_port_in(port, UART_RX);
  1622. }
  1623. static void serial8250_put_poll_char(struct uart_port *port,
  1624. unsigned char c)
  1625. {
  1626. unsigned int ier;
  1627. struct uart_8250_port *up =
  1628. container_of(port, struct uart_8250_port, port);
  1629. /*
  1630. * First save the IER then disable the interrupts
  1631. */
  1632. ier = serial_port_in(port, UART_IER);
  1633. if (up->capabilities & UART_CAP_UUE)
  1634. serial_port_out(port, UART_IER, UART_IER_UUE);
  1635. else
  1636. serial_port_out(port, UART_IER, 0);
  1637. wait_for_xmitr(up, BOTH_EMPTY);
  1638. /*
  1639. * Send the character out.
  1640. * If a LF, also do CR...
  1641. */
  1642. serial_port_out(port, UART_TX, c);
  1643. if (c == 10) {
  1644. wait_for_xmitr(up, BOTH_EMPTY);
  1645. serial_port_out(port, UART_TX, 13);
  1646. }
  1647. /*
  1648. * Finally, wait for transmitter to become empty
  1649. * and restore the IER
  1650. */
  1651. wait_for_xmitr(up, BOTH_EMPTY);
  1652. serial_port_out(port, UART_IER, ier);
  1653. }
  1654. #endif /* CONFIG_CONSOLE_POLL */
  1655. static int serial8250_startup(struct uart_port *port)
  1656. {
  1657. struct uart_8250_port *up =
  1658. container_of(port, struct uart_8250_port, port);
  1659. unsigned long flags;
  1660. unsigned char lsr, iir;
  1661. int retval;
  1662. port->fifosize = uart_config[up->port.type].fifo_size;
  1663. up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
  1664. up->capabilities = uart_config[up->port.type].flags;
  1665. up->mcr = 0;
  1666. if (port->iotype != up->cur_iotype)
  1667. set_io_from_upio(port);
  1668. if (port->type == PORT_16C950) {
  1669. /* Wake up and initialize UART */
  1670. up->acr = 0;
  1671. serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
  1672. serial_port_out(port, UART_EFR, UART_EFR_ECB);
  1673. serial_port_out(port, UART_IER, 0);
  1674. serial_port_out(port, UART_LCR, 0);
  1675. serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
  1676. serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
  1677. serial_port_out(port, UART_EFR, UART_EFR_ECB);
  1678. serial_port_out(port, UART_LCR, 0);
  1679. }
  1680. #ifdef CONFIG_SERIAL_8250_RSA
  1681. /*
  1682. * If this is an RSA port, see if we can kick it up to the
  1683. * higher speed clock.
  1684. */
  1685. enable_rsa(up);
  1686. #endif
  1687. /*
  1688. * Clear the FIFO buffers and disable them.
  1689. * (they will be reenabled in set_termios())
  1690. */
  1691. serial8250_clear_fifos(up);
  1692. /*
  1693. * Clear the interrupt registers.
  1694. */
  1695. serial_port_in(port, UART_LSR);
  1696. serial_port_in(port, UART_RX);
  1697. serial_port_in(port, UART_IIR);
  1698. serial_port_in(port, UART_MSR);
  1699. /*
  1700. * At this point, there's no way the LSR could still be 0xff;
  1701. * if it is, then bail out, because there's likely no UART
  1702. * here.
  1703. */
  1704. if (!(port->flags & UPF_BUGGY_UART) &&
  1705. (serial_port_in(port, UART_LSR) == 0xff)) {
  1706. printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
  1707. serial_index(port));
  1708. return -ENODEV;
  1709. }
  1710. /*
  1711. * For a XR16C850, we need to set the trigger levels
  1712. */
  1713. if (port->type == PORT_16850) {
  1714. unsigned char fctr;
  1715. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  1716. fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
  1717. serial_port_out(port, UART_FCTR,
  1718. fctr | UART_FCTR_TRGD | UART_FCTR_RX);
  1719. serial_port_out(port, UART_TRG, UART_TRG_96);
  1720. serial_port_out(port, UART_FCTR,
  1721. fctr | UART_FCTR_TRGD | UART_FCTR_TX);
  1722. serial_port_out(port, UART_TRG, UART_TRG_96);
  1723. serial_port_out(port, UART_LCR, 0);
  1724. }
  1725. if (port->irq) {
  1726. unsigned char iir1;
  1727. /*
  1728. * Test for UARTs that do not reassert THRE when the
  1729. * transmitter is idle and the interrupt has already
  1730. * been cleared. Real 16550s should always reassert
  1731. * this interrupt whenever the transmitter is idle and
  1732. * the interrupt is enabled. Delays are necessary to
  1733. * allow register changes to become visible.
  1734. */
  1735. spin_lock_irqsave(&port->lock, flags);
  1736. if (up->port.irqflags & IRQF_SHARED)
  1737. disable_irq_nosync(port->irq);
  1738. wait_for_xmitr(up, UART_LSR_THRE);
  1739. serial_port_out_sync(port, UART_IER, UART_IER_THRI);
  1740. udelay(1); /* allow THRE to set */
  1741. iir1 = serial_port_in(port, UART_IIR);
  1742. serial_port_out(port, UART_IER, 0);
  1743. serial_port_out_sync(port, UART_IER, UART_IER_THRI);
  1744. udelay(1); /* allow a working UART time to re-assert THRE */
  1745. iir = serial_port_in(port, UART_IIR);
  1746. serial_port_out(port, UART_IER, 0);
  1747. if (port->irqflags & IRQF_SHARED)
  1748. enable_irq(port->irq);
  1749. spin_unlock_irqrestore(&port->lock, flags);
  1750. /*
  1751. * If the interrupt is not reasserted, or we otherwise
  1752. * don't trust the iir, setup a timer to kick the UART
  1753. * on a regular basis.
  1754. */
  1755. if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
  1756. up->port.flags & UPF_BUG_THRE) {
  1757. up->bugs |= UART_BUG_THRE;
  1758. pr_debug("ttyS%d - using backup timer\n",
  1759. serial_index(port));
  1760. }
  1761. }
  1762. /*
  1763. * The above check will only give an accurate result the first time
  1764. * the port is opened so this value needs to be preserved.
  1765. */
  1766. if (up->bugs & UART_BUG_THRE) {
  1767. up->timer.function = serial8250_backup_timeout;
  1768. up->timer.data = (unsigned long)up;
  1769. mod_timer(&up->timer, jiffies +
  1770. uart_poll_timeout(port) + HZ / 5);
  1771. }
  1772. /*
  1773. * If the "interrupt" for this port doesn't correspond with any
  1774. * hardware interrupt, we use a timer-based system. The original
  1775. * driver used to do this with IRQ0.
  1776. */
  1777. if (!port->irq) {
  1778. up->timer.data = (unsigned long)up;
  1779. mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
  1780. } else {
  1781. retval = serial_link_irq_chain(up);
  1782. if (retval)
  1783. return retval;
  1784. }
  1785. /*
  1786. * Now, initialize the UART
  1787. */
  1788. serial_port_out(port, UART_LCR, UART_LCR_WLEN8);
  1789. spin_lock_irqsave(&port->lock, flags);
  1790. if (up->port.flags & UPF_FOURPORT) {
  1791. if (!up->port.irq)
  1792. up->port.mctrl |= TIOCM_OUT1;
  1793. } else
  1794. /*
  1795. * Most PC uarts need OUT2 raised to enable interrupts.
  1796. */
  1797. if (port->irq)
  1798. up->port.mctrl |= TIOCM_OUT2;
  1799. serial8250_set_mctrl(port, port->mctrl);
  1800. /* Serial over Lan (SoL) hack:
  1801. Intel 8257x Gigabit ethernet chips have a
  1802. 16550 emulation, to be used for Serial Over Lan.
  1803. Those chips take a longer time than a normal
  1804. serial device to signalize that a transmission
  1805. data was queued. Due to that, the above test generally
  1806. fails. One solution would be to delay the reading of
  1807. iir. However, this is not reliable, since the timeout
  1808. is variable. So, let's just don't test if we receive
  1809. TX irq. This way, we'll never enable UART_BUG_TXEN.
  1810. */
  1811. if (skip_txen_test || up->port.flags & UPF_NO_TXEN_TEST)
  1812. goto dont_test_tx_en;
  1813. /*
  1814. * Do a quick test to see if we receive an
  1815. * interrupt when we enable the TX irq.
  1816. */
  1817. serial_port_out(port, UART_IER, UART_IER_THRI);
  1818. lsr = serial_port_in(port, UART_LSR);
  1819. iir = serial_port_in(port, UART_IIR);
  1820. serial_port_out(port, UART_IER, 0);
  1821. if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
  1822. if (!(up->bugs & UART_BUG_TXEN)) {
  1823. up->bugs |= UART_BUG_TXEN;
  1824. pr_debug("ttyS%d - enabling bad tx status workarounds\n",
  1825. serial_index(port));
  1826. }
  1827. } else {
  1828. up->bugs &= ~UART_BUG_TXEN;
  1829. }
  1830. dont_test_tx_en:
  1831. spin_unlock_irqrestore(&port->lock, flags);
  1832. /*
  1833. * Clear the interrupt registers again for luck, and clear the
  1834. * saved flags to avoid getting false values from polling
  1835. * routines or the previous session.
  1836. */
  1837. serial_port_in(port, UART_LSR);
  1838. serial_port_in(port, UART_RX);
  1839. serial_port_in(port, UART_IIR);
  1840. serial_port_in(port, UART_MSR);
  1841. up->lsr_saved_flags = 0;
  1842. up->msr_saved_flags = 0;
  1843. /*
  1844. * Finally, enable interrupts. Note: Modem status interrupts
  1845. * are set via set_termios(), which will be occurring imminently
  1846. * anyway, so we don't enable them here.
  1847. */
  1848. up->ier = UART_IER_RLSI | UART_IER_RDI;
  1849. serial_port_out(port, UART_IER, up->ier);
  1850. if (port->flags & UPF_FOURPORT) {
  1851. unsigned int icp;
  1852. /*
  1853. * Enable interrupts on the AST Fourport board
  1854. */
  1855. icp = (port->iobase & 0xfe0) | 0x01f;
  1856. outb_p(0x80, icp);
  1857. inb_p(icp);
  1858. }
  1859. return 0;
  1860. }
  1861. static void serial8250_shutdown(struct uart_port *port)
  1862. {
  1863. struct uart_8250_port *up =
  1864. container_of(port, struct uart_8250_port, port);
  1865. unsigned long flags;
  1866. /*
  1867. * Disable interrupts from this port
  1868. */
  1869. up->ier = 0;
  1870. serial_port_out(port, UART_IER, 0);
  1871. spin_lock_irqsave(&port->lock, flags);
  1872. if (port->flags & UPF_FOURPORT) {
  1873. /* reset interrupts on the AST Fourport board */
  1874. inb((port->iobase & 0xfe0) | 0x1f);
  1875. port->mctrl |= TIOCM_OUT1;
  1876. } else
  1877. port->mctrl &= ~TIOCM_OUT2;
  1878. serial8250_set_mctrl(port, port->mctrl);
  1879. spin_unlock_irqrestore(&port->lock, flags);
  1880. /*
  1881. * Disable break condition and FIFOs
  1882. */
  1883. serial_port_out(port, UART_LCR,
  1884. serial_port_in(port, UART_LCR) & ~UART_LCR_SBC);
  1885. serial8250_clear_fifos(up);
  1886. #ifdef CONFIG_SERIAL_8250_RSA
  1887. /*
  1888. * Reset the RSA board back to 115kbps compat mode.
  1889. */
  1890. disable_rsa(up);
  1891. #endif
  1892. /*
  1893. * Read data port to reset things, and then unlink from
  1894. * the IRQ chain.
  1895. */
  1896. serial_port_in(port, UART_RX);
  1897. del_timer_sync(&up->timer);
  1898. up->timer.function = serial8250_timeout;
  1899. if (port->irq)
  1900. serial_unlink_irq_chain(up);
  1901. }
  1902. static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
  1903. {
  1904. unsigned int quot;
  1905. /*
  1906. * Handle magic divisors for baud rates above baud_base on
  1907. * SMSC SuperIO chips.
  1908. */
  1909. if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
  1910. baud == (port->uartclk/4))
  1911. quot = 0x8001;
  1912. else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
  1913. baud == (port->uartclk/8))
  1914. quot = 0x8002;
  1915. else
  1916. quot = uart_get_divisor(port, baud);
  1917. return quot;
  1918. }
  1919. void
  1920. serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
  1921. struct ktermios *old)
  1922. {
  1923. struct uart_8250_port *up =
  1924. container_of(port, struct uart_8250_port, port);
  1925. unsigned char cval, fcr = 0;
  1926. unsigned long flags;
  1927. unsigned int baud, quot;
  1928. switch (termios->c_cflag & CSIZE) {
  1929. case CS5:
  1930. cval = UART_LCR_WLEN5;
  1931. break;
  1932. case CS6:
  1933. cval = UART_LCR_WLEN6;
  1934. break;
  1935. case CS7:
  1936. cval = UART_LCR_WLEN7;
  1937. break;
  1938. default:
  1939. case CS8:
  1940. cval = UART_LCR_WLEN8;
  1941. break;
  1942. }
  1943. if (termios->c_cflag & CSTOPB)
  1944. cval |= UART_LCR_STOP;
  1945. if (termios->c_cflag & PARENB)
  1946. cval |= UART_LCR_PARITY;
  1947. if (!(termios->c_cflag & PARODD))
  1948. cval |= UART_LCR_EPAR;
  1949. #ifdef CMSPAR
  1950. if (termios->c_cflag & CMSPAR)
  1951. cval |= UART_LCR_SPAR;
  1952. #endif
  1953. /*
  1954. * Ask the core to calculate the divisor for us.
  1955. */
  1956. baud = uart_get_baud_rate(port, termios, old,
  1957. port->uartclk / 16 / 0xffff,
  1958. port->uartclk / 16);
  1959. quot = serial8250_get_divisor(port, baud);
  1960. /*
  1961. * Oxford Semi 952 rev B workaround
  1962. */
  1963. if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
  1964. quot++;
  1965. if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
  1966. fcr = uart_config[port->type].fcr;
  1967. if (baud < 2400) {
  1968. fcr &= ~UART_FCR_TRIGGER_MASK;
  1969. fcr |= UART_FCR_TRIGGER_1;
  1970. }
  1971. }
  1972. /*
  1973. * MCR-based auto flow control. When AFE is enabled, RTS will be
  1974. * deasserted when the receive FIFO contains more characters than
  1975. * the trigger, or the MCR RTS bit is cleared. In the case where
  1976. * the remote UART is not using CTS auto flow control, we must
  1977. * have sufficient FIFO entries for the latency of the remote
  1978. * UART to respond. IOW, at least 32 bytes of FIFO.
  1979. */
  1980. if (up->capabilities & UART_CAP_AFE && port->fifosize >= 32) {
  1981. up->mcr &= ~UART_MCR_AFE;
  1982. if (termios->c_cflag & CRTSCTS)
  1983. up->mcr |= UART_MCR_AFE;
  1984. }
  1985. /*
  1986. * Ok, we're now changing the port state. Do it with
  1987. * interrupts disabled.
  1988. */
  1989. spin_lock_irqsave(&port->lock, flags);
  1990. /*
  1991. * Update the per-port timeout.
  1992. */
  1993. uart_update_timeout(port, termios->c_cflag, baud);
  1994. port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  1995. if (termios->c_iflag & INPCK)
  1996. port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  1997. if (termios->c_iflag & (BRKINT | PARMRK))
  1998. port->read_status_mask |= UART_LSR_BI;
  1999. /*
  2000. * Characteres to ignore
  2001. */
  2002. port->ignore_status_mask = 0;
  2003. if (termios->c_iflag & IGNPAR)
  2004. port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  2005. if (termios->c_iflag & IGNBRK) {
  2006. port->ignore_status_mask |= UART_LSR_BI;
  2007. /*
  2008. * If we're ignoring parity and break indicators,
  2009. * ignore overruns too (for real raw support).
  2010. */
  2011. if (termios->c_iflag & IGNPAR)
  2012. port->ignore_status_mask |= UART_LSR_OE;
  2013. }
  2014. /*
  2015. * ignore all characters if CREAD is not set
  2016. */
  2017. if ((termios->c_cflag & CREAD) == 0)
  2018. port->ignore_status_mask |= UART_LSR_DR;
  2019. /*
  2020. * CTS flow control flag and modem status interrupts
  2021. */
  2022. up->ier &= ~UART_IER_MSI;
  2023. if (!(up->bugs & UART_BUG_NOMSR) &&
  2024. UART_ENABLE_MS(&up->port, termios->c_cflag))
  2025. up->ier |= UART_IER_MSI;
  2026. if (up->capabilities & UART_CAP_UUE)
  2027. up->ier |= UART_IER_UUE;
  2028. if (up->capabilities & UART_CAP_RTOIE)
  2029. up->ier |= UART_IER_RTOIE;
  2030. serial_port_out(port, UART_IER, up->ier);
  2031. if (up->capabilities & UART_CAP_EFR) {
  2032. unsigned char efr = 0;
  2033. /*
  2034. * TI16C752/Startech hardware flow control. FIXME:
  2035. * - TI16C752 requires control thresholds to be set.
  2036. * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
  2037. */
  2038. if (termios->c_cflag & CRTSCTS)
  2039. efr |= UART_EFR_CTS;
  2040. serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
  2041. if (port->flags & UPF_EXAR_EFR)
  2042. serial_port_out(port, UART_XR_EFR, efr);
  2043. else
  2044. serial_port_out(port, UART_EFR, efr);
  2045. }
  2046. #ifdef CONFIG_ARCH_OMAP
  2047. /* Workaround to enable 115200 baud on OMAP1510 internal ports */
  2048. if (cpu_is_omap1510() && is_omap_port(up)) {
  2049. if (baud == 115200) {
  2050. quot = 1;
  2051. serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
  2052. } else
  2053. serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
  2054. }
  2055. #endif
  2056. /*
  2057. * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
  2058. * otherwise just set DLAB
  2059. */
  2060. if (up->capabilities & UART_NATSEMI)
  2061. serial_port_out(port, UART_LCR, 0xe0);
  2062. else
  2063. serial_port_out(port, UART_LCR, cval | UART_LCR_DLAB);
  2064. serial_dl_write(up, quot);
  2065. /*
  2066. * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
  2067. * is written without DLAB set, this mode will be disabled.
  2068. */
  2069. if (port->type == PORT_16750)
  2070. serial_port_out(port, UART_FCR, fcr);
  2071. serial_port_out(port, UART_LCR, cval); /* reset DLAB */
  2072. up->lcr = cval; /* Save LCR */
  2073. if (port->type != PORT_16750) {
  2074. /* emulated UARTs (Lucent Venus 167x) need two steps */
  2075. if (fcr & UART_FCR_ENABLE_FIFO)
  2076. serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
  2077. serial_port_out(port, UART_FCR, fcr); /* set fcr */
  2078. }
  2079. serial8250_set_mctrl(port, port->mctrl);
  2080. spin_unlock_irqrestore(&port->lock, flags);
  2081. /* Don't rewrite B0 */
  2082. if (tty_termios_baud_rate(termios))
  2083. tty_termios_encode_baud_rate(termios, baud, baud);
  2084. }
  2085. EXPORT_SYMBOL(serial8250_do_set_termios);
  2086. static void
  2087. serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
  2088. struct ktermios *old)
  2089. {
  2090. if (port->set_termios)
  2091. port->set_termios(port, termios, old);
  2092. else
  2093. serial8250_do_set_termios(port, termios, old);
  2094. }
  2095. static void
  2096. serial8250_set_ldisc(struct uart_port *port, int new)
  2097. {
  2098. if (new == N_PPS) {
  2099. port->flags |= UPF_HARDPPS_CD;
  2100. serial8250_enable_ms(port);
  2101. } else
  2102. port->flags &= ~UPF_HARDPPS_CD;
  2103. }
  2104. void serial8250_do_pm(struct uart_port *port, unsigned int state,
  2105. unsigned int oldstate)
  2106. {
  2107. struct uart_8250_port *p =
  2108. container_of(port, struct uart_8250_port, port);
  2109. serial8250_set_sleep(p, state != 0);
  2110. }
  2111. EXPORT_SYMBOL(serial8250_do_pm);
  2112. static void
  2113. serial8250_pm(struct uart_port *port, unsigned int state,
  2114. unsigned int oldstate)
  2115. {
  2116. if (port->pm)
  2117. port->pm(port, state, oldstate);
  2118. else
  2119. serial8250_do_pm(port, state, oldstate);
  2120. }
  2121. static unsigned int serial8250_port_size(struct uart_8250_port *pt)
  2122. {
  2123. if (pt->port.iotype == UPIO_AU)
  2124. return 0x1000;
  2125. #ifdef CONFIG_ARCH_OMAP
  2126. if (is_omap_port(pt))
  2127. return 0x16 << pt->port.regshift;
  2128. #endif
  2129. return 8 << pt->port.regshift;
  2130. }
  2131. /*
  2132. * Resource handling.
  2133. */
  2134. static int serial8250_request_std_resource(struct uart_8250_port *up)
  2135. {
  2136. unsigned int size = serial8250_port_size(up);
  2137. struct uart_port *port = &up->port;
  2138. int ret = 0;
  2139. switch (port->iotype) {
  2140. case UPIO_AU:
  2141. case UPIO_TSI:
  2142. case UPIO_MEM32:
  2143. case UPIO_MEM:
  2144. if (!port->mapbase)
  2145. break;
  2146. if (!request_mem_region(port->mapbase, size, "serial")) {
  2147. ret = -EBUSY;
  2148. break;
  2149. }
  2150. if (port->flags & UPF_IOREMAP) {
  2151. port->membase = ioremap_nocache(port->mapbase, size);
  2152. if (!port->membase) {
  2153. release_mem_region(port->mapbase, size);
  2154. ret = -ENOMEM;
  2155. }
  2156. }
  2157. break;
  2158. case UPIO_HUB6:
  2159. case UPIO_PORT:
  2160. if (!request_region(port->iobase, size, "serial"))
  2161. ret = -EBUSY;
  2162. break;
  2163. }
  2164. return ret;
  2165. }
  2166. static void serial8250_release_std_resource(struct uart_8250_port *up)
  2167. {
  2168. unsigned int size = serial8250_port_size(up);
  2169. struct uart_port *port = &up->port;
  2170. switch (port->iotype) {
  2171. case UPIO_AU:
  2172. case UPIO_TSI:
  2173. case UPIO_MEM32:
  2174. case UPIO_MEM:
  2175. if (!port->mapbase)
  2176. break;
  2177. if (port->flags & UPF_IOREMAP) {
  2178. iounmap(port->membase);
  2179. port->membase = NULL;
  2180. }
  2181. release_mem_region(port->mapbase, size);
  2182. break;
  2183. case UPIO_HUB6:
  2184. case UPIO_PORT:
  2185. release_region(port->iobase, size);
  2186. break;
  2187. }
  2188. }
  2189. static int serial8250_request_rsa_resource(struct uart_8250_port *up)
  2190. {
  2191. unsigned long start = UART_RSA_BASE << up->port.regshift;
  2192. unsigned int size = 8 << up->port.regshift;
  2193. struct uart_port *port = &up->port;
  2194. int ret = -EINVAL;
  2195. switch (port->iotype) {
  2196. case UPIO_HUB6:
  2197. case UPIO_PORT:
  2198. start += port->iobase;
  2199. if (request_region(start, size, "serial-rsa"))
  2200. ret = 0;
  2201. else
  2202. ret = -EBUSY;
  2203. break;
  2204. }
  2205. return ret;
  2206. }
  2207. static void serial8250_release_rsa_resource(struct uart_8250_port *up)
  2208. {
  2209. unsigned long offset = UART_RSA_BASE << up->port.regshift;
  2210. unsigned int size = 8 << up->port.regshift;
  2211. struct uart_port *port = &up->port;
  2212. switch (port->iotype) {
  2213. case UPIO_HUB6:
  2214. case UPIO_PORT:
  2215. release_region(port->iobase + offset, size);
  2216. break;
  2217. }
  2218. }
  2219. static void serial8250_release_port(struct uart_port *port)
  2220. {
  2221. struct uart_8250_port *up =
  2222. container_of(port, struct uart_8250_port, port);
  2223. serial8250_release_std_resource(up);
  2224. if (port->type == PORT_RSA)
  2225. serial8250_release_rsa_resource(up);
  2226. }
  2227. static int serial8250_request_port(struct uart_port *port)
  2228. {
  2229. struct uart_8250_port *up =
  2230. container_of(port, struct uart_8250_port, port);
  2231. int ret = 0;
  2232. ret = serial8250_request_std_resource(up);
  2233. if (ret == 0 && port->type == PORT_RSA) {
  2234. ret = serial8250_request_rsa_resource(up);
  2235. if (ret < 0)
  2236. serial8250_release_std_resource(up);
  2237. }
  2238. return ret;
  2239. }
  2240. static void serial8250_config_port(struct uart_port *port, int flags)
  2241. {
  2242. struct uart_8250_port *up =
  2243. container_of(port, struct uart_8250_port, port);
  2244. int probeflags = PROBE_ANY;
  2245. int ret;
  2246. /*
  2247. * Find the region that we can probe for. This in turn
  2248. * tells us whether we can probe for the type of port.
  2249. */
  2250. ret = serial8250_request_std_resource(up);
  2251. if (ret < 0)
  2252. return;
  2253. ret = serial8250_request_rsa_resource(up);
  2254. if (ret < 0)
  2255. probeflags &= ~PROBE_RSA;
  2256. if (port->iotype != up->cur_iotype)
  2257. set_io_from_upio(port);
  2258. if (flags & UART_CONFIG_TYPE)
  2259. autoconfig(up, probeflags);
  2260. /* if access method is AU, it is a 16550 with a quirk */
  2261. if (port->type == PORT_16550A && port->iotype == UPIO_AU)
  2262. up->bugs |= UART_BUG_NOMSR;
  2263. if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
  2264. autoconfig_irq(up);
  2265. if (port->type != PORT_RSA && probeflags & PROBE_RSA)
  2266. serial8250_release_rsa_resource(up);
  2267. if (port->type == PORT_UNKNOWN)
  2268. serial8250_release_std_resource(up);
  2269. }
  2270. static int
  2271. serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
  2272. {
  2273. if (ser->irq >= nr_irqs || ser->irq < 0 ||
  2274. ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
  2275. ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
  2276. ser->type == PORT_STARTECH)
  2277. return -EINVAL;
  2278. return 0;
  2279. }
  2280. static const char *
  2281. serial8250_type(struct uart_port *port)
  2282. {
  2283. int type = port->type;
  2284. if (type >= ARRAY_SIZE(uart_config))
  2285. type = 0;
  2286. return uart_config[type].name;
  2287. }
  2288. static struct uart_ops serial8250_pops = {
  2289. .tx_empty = serial8250_tx_empty,
  2290. .set_mctrl = serial8250_set_mctrl,
  2291. .get_mctrl = serial8250_get_mctrl,
  2292. .stop_tx = serial8250_stop_tx,
  2293. .start_tx = serial8250_start_tx,
  2294. .stop_rx = serial8250_stop_rx,
  2295. .enable_ms = serial8250_enable_ms,
  2296. .break_ctl = serial8250_break_ctl,
  2297. .startup = serial8250_startup,
  2298. .shutdown = serial8250_shutdown,
  2299. .set_termios = serial8250_set_termios,
  2300. .set_ldisc = serial8250_set_ldisc,
  2301. .pm = serial8250_pm,
  2302. .type = serial8250_type,
  2303. .release_port = serial8250_release_port,
  2304. .request_port = serial8250_request_port,
  2305. .config_port = serial8250_config_port,
  2306. .verify_port = serial8250_verify_port,
  2307. #ifdef CONFIG_CONSOLE_POLL
  2308. .poll_get_char = serial8250_get_poll_char,
  2309. .poll_put_char = serial8250_put_poll_char,
  2310. #endif
  2311. };
  2312. static struct uart_8250_port serial8250_ports[UART_NR];
  2313. static void (*serial8250_isa_config)(int port, struct uart_port *up,
  2314. unsigned short *capabilities);
  2315. void serial8250_set_isa_configurator(
  2316. void (*v)(int port, struct uart_port *up, unsigned short *capabilities))
  2317. {
  2318. serial8250_isa_config = v;
  2319. }
  2320. EXPORT_SYMBOL(serial8250_set_isa_configurator);
  2321. static void __init serial8250_isa_init_ports(void)
  2322. {
  2323. struct uart_8250_port *up;
  2324. static int first = 1;
  2325. int i, irqflag = 0;
  2326. if (!first)
  2327. return;
  2328. first = 0;
  2329. for (i = 0; i < nr_uarts; i++) {
  2330. struct uart_8250_port *up = &serial8250_ports[i];
  2331. struct uart_port *port = &up->port;
  2332. port->line = i;
  2333. spin_lock_init(&port->lock);
  2334. init_timer(&up->timer);
  2335. up->timer.function = serial8250_timeout;
  2336. /*
  2337. * ALPHA_KLUDGE_MCR needs to be killed.
  2338. */
  2339. up->mcr_mask = ~ALPHA_KLUDGE_MCR;
  2340. up->mcr_force = ALPHA_KLUDGE_MCR;
  2341. port->ops = &serial8250_pops;
  2342. }
  2343. if (share_irqs)
  2344. irqflag = IRQF_SHARED;
  2345. for (i = 0, up = serial8250_ports;
  2346. i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
  2347. i++, up++) {
  2348. struct uart_port *port = &up->port;
  2349. port->iobase = old_serial_port[i].port;
  2350. port->irq = irq_canonicalize(old_serial_port[i].irq);
  2351. port->irqflags = old_serial_port[i].irqflags;
  2352. port->uartclk = old_serial_port[i].baud_base * 16;
  2353. port->flags = old_serial_port[i].flags;
  2354. port->hub6 = old_serial_port[i].hub6;
  2355. port->membase = old_serial_port[i].iomem_base;
  2356. port->iotype = old_serial_port[i].io_type;
  2357. port->regshift = old_serial_port[i].iomem_reg_shift;
  2358. set_io_from_upio(port);
  2359. port->irqflags |= irqflag;
  2360. if (serial8250_isa_config != NULL)
  2361. serial8250_isa_config(i, &up->port, &up->capabilities);
  2362. }
  2363. }
  2364. static void
  2365. serial8250_init_fixed_type_port(struct uart_8250_port *up, unsigned int type)
  2366. {
  2367. up->port.type = type;
  2368. up->port.fifosize = uart_config[type].fifo_size;
  2369. up->capabilities = uart_config[type].flags;
  2370. up->tx_loadsz = uart_config[type].tx_loadsz;
  2371. }
  2372. static void __init
  2373. serial8250_register_ports(struct uart_driver *drv, struct device *dev)
  2374. {
  2375. int i;
  2376. for (i = 0; i < nr_uarts; i++) {
  2377. struct uart_8250_port *up = &serial8250_ports[i];
  2378. up->cur_iotype = 0xFF;
  2379. }
  2380. serial8250_isa_init_ports();
  2381. for (i = 0; i < nr_uarts; i++) {
  2382. struct uart_8250_port *up = &serial8250_ports[i];
  2383. up->port.dev = dev;
  2384. if (up->port.flags & UPF_FIXED_TYPE)
  2385. serial8250_init_fixed_type_port(up, up->port.type);
  2386. uart_add_one_port(drv, &up->port);
  2387. }
  2388. }
  2389. #ifdef CONFIG_SERIAL_8250_CONSOLE
  2390. static void serial8250_console_putchar(struct uart_port *port, int ch)
  2391. {
  2392. struct uart_8250_port *up =
  2393. container_of(port, struct uart_8250_port, port);
  2394. wait_for_xmitr(up, UART_LSR_THRE);
  2395. serial_port_out(port, UART_TX, ch);
  2396. }
  2397. /*
  2398. * Print a string to the serial port trying not to disturb
  2399. * any possible real use of the port...
  2400. *
  2401. * The console_lock must be held when we get here.
  2402. */
  2403. static void
  2404. serial8250_console_write(struct console *co, const char *s, unsigned int count)
  2405. {
  2406. struct uart_8250_port *up = &serial8250_ports[co->index];
  2407. struct uart_port *port = &up->port;
  2408. unsigned long flags;
  2409. unsigned int ier;
  2410. int locked = 1;
  2411. touch_nmi_watchdog();
  2412. local_irq_save(flags);
  2413. if (port->sysrq) {
  2414. /* serial8250_handle_irq() already took the lock */
  2415. locked = 0;
  2416. } else if (oops_in_progress) {
  2417. locked = spin_trylock(&port->lock);
  2418. } else
  2419. spin_lock(&port->lock);
  2420. /*
  2421. * First save the IER then disable the interrupts
  2422. */
  2423. ier = serial_port_in(port, UART_IER);
  2424. if (up->capabilities & UART_CAP_UUE)
  2425. serial_port_out(port, UART_IER, UART_IER_UUE);
  2426. else
  2427. serial_port_out(port, UART_IER, 0);
  2428. uart_console_write(port, s, count, serial8250_console_putchar);
  2429. /*
  2430. * Finally, wait for transmitter to become empty
  2431. * and restore the IER
  2432. */
  2433. wait_for_xmitr(up, BOTH_EMPTY);
  2434. serial_port_out(port, UART_IER, ier);
  2435. /*
  2436. * The receive handling will happen properly because the
  2437. * receive ready bit will still be set; it is not cleared
  2438. * on read. However, modem control will not, we must
  2439. * call it if we have saved something in the saved flags
  2440. * while processing with interrupts off.
  2441. */
  2442. if (up->msr_saved_flags)
  2443. serial8250_modem_status(up);
  2444. if (locked)
  2445. spin_unlock(&port->lock);
  2446. local_irq_restore(flags);
  2447. }
  2448. static int __init serial8250_console_setup(struct console *co, char *options)
  2449. {
  2450. struct uart_port *port;
  2451. int baud = 9600;
  2452. int bits = 8;
  2453. int parity = 'n';
  2454. int flow = 'n';
  2455. /*
  2456. * Check whether an invalid uart number has been specified, and
  2457. * if so, search for the first available port that does have
  2458. * console support.
  2459. */
  2460. if (co->index >= nr_uarts)
  2461. co->index = 0;
  2462. port = &serial8250_ports[co->index].port;
  2463. if (!port->iobase && !port->membase)
  2464. return -ENODEV;
  2465. if (options)
  2466. uart_parse_options(options, &baud, &parity, &bits, &flow);
  2467. return uart_set_options(port, co, baud, parity, bits, flow);
  2468. }
  2469. static int serial8250_console_early_setup(void)
  2470. {
  2471. return serial8250_find_port_for_earlycon();
  2472. }
  2473. static struct console serial8250_console = {
  2474. .name = "ttyS",
  2475. .write = serial8250_console_write,
  2476. .device = uart_console_device,
  2477. .setup = serial8250_console_setup,
  2478. .early_setup = serial8250_console_early_setup,
  2479. .flags = CON_PRINTBUFFER | CON_ANYTIME,
  2480. .index = -1,
  2481. .data = &serial8250_reg,
  2482. };
  2483. static int __init serial8250_console_init(void)
  2484. {
  2485. if (nr_uarts > UART_NR)
  2486. nr_uarts = UART_NR;
  2487. serial8250_isa_init_ports();
  2488. register_console(&serial8250_console);
  2489. return 0;
  2490. }
  2491. console_initcall(serial8250_console_init);
  2492. int serial8250_find_port(struct uart_port *p)
  2493. {
  2494. int line;
  2495. struct uart_port *port;
  2496. for (line = 0; line < nr_uarts; line++) {
  2497. port = &serial8250_ports[line].port;
  2498. if (uart_match_port(p, port))
  2499. return line;
  2500. }
  2501. return -ENODEV;
  2502. }
  2503. #define SERIAL8250_CONSOLE &serial8250_console
  2504. #else
  2505. #define SERIAL8250_CONSOLE NULL
  2506. #endif
  2507. static struct uart_driver serial8250_reg = {
  2508. .owner = THIS_MODULE,
  2509. .driver_name = "serial",
  2510. .dev_name = "ttyS",
  2511. .major = TTY_MAJOR,
  2512. .minor = 64,
  2513. .cons = SERIAL8250_CONSOLE,
  2514. };
  2515. /*
  2516. * early_serial_setup - early registration for 8250 ports
  2517. *
  2518. * Setup an 8250 port structure prior to console initialisation. Use
  2519. * after console initialisation will cause undefined behaviour.
  2520. */
  2521. int __init early_serial_setup(struct uart_port *port)
  2522. {
  2523. struct uart_port *p;
  2524. if (port->line >= ARRAY_SIZE(serial8250_ports))
  2525. return -ENODEV;
  2526. serial8250_isa_init_ports();
  2527. p = &serial8250_ports[port->line].port;
  2528. p->iobase = port->iobase;
  2529. p->membase = port->membase;
  2530. p->irq = port->irq;
  2531. p->irqflags = port->irqflags;
  2532. p->uartclk = port->uartclk;
  2533. p->fifosize = port->fifosize;
  2534. p->regshift = port->regshift;
  2535. p->iotype = port->iotype;
  2536. p->flags = port->flags;
  2537. p->mapbase = port->mapbase;
  2538. p->private_data = port->private_data;
  2539. p->type = port->type;
  2540. p->line = port->line;
  2541. set_io_from_upio(p);
  2542. if (port->serial_in)
  2543. p->serial_in = port->serial_in;
  2544. if (port->serial_out)
  2545. p->serial_out = port->serial_out;
  2546. if (port->handle_irq)
  2547. p->handle_irq = port->handle_irq;
  2548. else
  2549. p->handle_irq = serial8250_default_handle_irq;
  2550. return 0;
  2551. }
  2552. /**
  2553. * serial8250_suspend_port - suspend one serial port
  2554. * @line: serial line number
  2555. *
  2556. * Suspend one serial port.
  2557. */
  2558. void serial8250_suspend_port(int line)
  2559. {
  2560. uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
  2561. }
  2562. /**
  2563. * serial8250_resume_port - resume one serial port
  2564. * @line: serial line number
  2565. *
  2566. * Resume one serial port.
  2567. */
  2568. void serial8250_resume_port(int line)
  2569. {
  2570. struct uart_8250_port *up = &serial8250_ports[line];
  2571. struct uart_port *port = &up->port;
  2572. if (up->capabilities & UART_NATSEMI) {
  2573. /* Ensure it's still in high speed mode */
  2574. serial_port_out(port, UART_LCR, 0xE0);
  2575. ns16550a_goto_highspeed(up);
  2576. serial_port_out(port, UART_LCR, 0);
  2577. port->uartclk = 921600*16;
  2578. }
  2579. uart_resume_port(&serial8250_reg, port);
  2580. }
  2581. /*
  2582. * Register a set of serial devices attached to a platform device. The
  2583. * list is terminated with a zero flags entry, which means we expect
  2584. * all entries to have at least UPF_BOOT_AUTOCONF set.
  2585. */
  2586. static int __devinit serial8250_probe(struct platform_device *dev)
  2587. {
  2588. struct plat_serial8250_port *p = dev->dev.platform_data;
  2589. struct uart_port port;
  2590. int ret, i, irqflag = 0;
  2591. memset(&port, 0, sizeof(struct uart_port));
  2592. if (share_irqs)
  2593. irqflag = IRQF_SHARED;
  2594. for (i = 0; p && p->flags != 0; p++, i++) {
  2595. port.iobase = p->iobase;
  2596. port.membase = p->membase;
  2597. port.irq = p->irq;
  2598. port.irqflags = p->irqflags;
  2599. port.uartclk = p->uartclk;
  2600. port.regshift = p->regshift;
  2601. port.iotype = p->iotype;
  2602. port.flags = p->flags;
  2603. port.mapbase = p->mapbase;
  2604. port.hub6 = p->hub6;
  2605. port.private_data = p->private_data;
  2606. port.type = p->type;
  2607. port.serial_in = p->serial_in;
  2608. port.serial_out = p->serial_out;
  2609. port.handle_irq = p->handle_irq;
  2610. port.handle_break = p->handle_break;
  2611. port.set_termios = p->set_termios;
  2612. port.pm = p->pm;
  2613. port.dev = &dev->dev;
  2614. port.irqflags |= irqflag;
  2615. ret = serial8250_register_port(&port);
  2616. if (ret < 0) {
  2617. dev_err(&dev->dev, "unable to register port at index %d "
  2618. "(IO%lx MEM%llx IRQ%d): %d\n", i,
  2619. p->iobase, (unsigned long long)p->mapbase,
  2620. p->irq, ret);
  2621. }
  2622. }
  2623. return 0;
  2624. }
  2625. /*
  2626. * Remove serial ports registered against a platform device.
  2627. */
  2628. static int __devexit serial8250_remove(struct platform_device *dev)
  2629. {
  2630. int i;
  2631. for (i = 0; i < nr_uarts; i++) {
  2632. struct uart_8250_port *up = &serial8250_ports[i];
  2633. if (up->port.dev == &dev->dev)
  2634. serial8250_unregister_port(i);
  2635. }
  2636. return 0;
  2637. }
  2638. static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
  2639. {
  2640. int i;
  2641. for (i = 0; i < UART_NR; i++) {
  2642. struct uart_8250_port *up = &serial8250_ports[i];
  2643. if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
  2644. uart_suspend_port(&serial8250_reg, &up->port);
  2645. }
  2646. return 0;
  2647. }
  2648. static int serial8250_resume(struct platform_device *dev)
  2649. {
  2650. int i;
  2651. for (i = 0; i < UART_NR; i++) {
  2652. struct uart_8250_port *up = &serial8250_ports[i];
  2653. if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
  2654. serial8250_resume_port(i);
  2655. }
  2656. return 0;
  2657. }
  2658. static struct platform_driver serial8250_isa_driver = {
  2659. .probe = serial8250_probe,
  2660. .remove = __devexit_p(serial8250_remove),
  2661. .suspend = serial8250_suspend,
  2662. .resume = serial8250_resume,
  2663. .driver = {
  2664. .name = "serial8250",
  2665. .owner = THIS_MODULE,
  2666. },
  2667. };
  2668. /*
  2669. * This "device" covers _all_ ISA 8250-compatible serial devices listed
  2670. * in the table in include/asm/serial.h
  2671. */
  2672. static struct platform_device *serial8250_isa_devs;
  2673. /*
  2674. * serial8250_register_port and serial8250_unregister_port allows for
  2675. * 16x50 serial ports to be configured at run-time, to support PCMCIA
  2676. * modems and PCI multiport cards.
  2677. */
  2678. static DEFINE_MUTEX(serial_mutex);
  2679. static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
  2680. {
  2681. int i;
  2682. /*
  2683. * First, find a port entry which matches.
  2684. */
  2685. for (i = 0; i < nr_uarts; i++)
  2686. if (uart_match_port(&serial8250_ports[i].port, port))
  2687. return &serial8250_ports[i];
  2688. /*
  2689. * We didn't find a matching entry, so look for the first
  2690. * free entry. We look for one which hasn't been previously
  2691. * used (indicated by zero iobase).
  2692. */
  2693. for (i = 0; i < nr_uarts; i++)
  2694. if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
  2695. serial8250_ports[i].port.iobase == 0)
  2696. return &serial8250_ports[i];
  2697. /*
  2698. * That also failed. Last resort is to find any entry which
  2699. * doesn't have a real port associated with it.
  2700. */
  2701. for (i = 0; i < nr_uarts; i++)
  2702. if (serial8250_ports[i].port.type == PORT_UNKNOWN)
  2703. return &serial8250_ports[i];
  2704. return NULL;
  2705. }
  2706. /**
  2707. * serial8250_register_port - register a serial port
  2708. * @port: serial port template
  2709. *
  2710. * Configure the serial port specified by the request. If the
  2711. * port exists and is in use, it is hung up and unregistered
  2712. * first.
  2713. *
  2714. * The port is then probed and if necessary the IRQ is autodetected
  2715. * If this fails an error is returned.
  2716. *
  2717. * On success the port is ready to use and the line number is returned.
  2718. */
  2719. int serial8250_register_port(struct uart_port *port)
  2720. {
  2721. struct uart_8250_port *uart;
  2722. int ret = -ENOSPC;
  2723. if (port->uartclk == 0)
  2724. return -EINVAL;
  2725. mutex_lock(&serial_mutex);
  2726. uart = serial8250_find_match_or_unused(port);
  2727. if (uart) {
  2728. uart_remove_one_port(&serial8250_reg, &uart->port);
  2729. uart->port.iobase = port->iobase;
  2730. uart->port.membase = port->membase;
  2731. uart->port.irq = port->irq;
  2732. uart->port.irqflags = port->irqflags;
  2733. uart->port.uartclk = port->uartclk;
  2734. uart->port.fifosize = port->fifosize;
  2735. uart->port.regshift = port->regshift;
  2736. uart->port.iotype = port->iotype;
  2737. uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
  2738. uart->port.mapbase = port->mapbase;
  2739. uart->port.private_data = port->private_data;
  2740. if (port->dev)
  2741. uart->port.dev = port->dev;
  2742. if (port->flags & UPF_FIXED_TYPE)
  2743. serial8250_init_fixed_type_port(uart, port->type);
  2744. set_io_from_upio(&uart->port);
  2745. /* Possibly override default I/O functions. */
  2746. if (port->serial_in)
  2747. uart->port.serial_in = port->serial_in;
  2748. if (port->serial_out)
  2749. uart->port.serial_out = port->serial_out;
  2750. if (port->handle_irq)
  2751. uart->port.handle_irq = port->handle_irq;
  2752. /* Possibly override set_termios call */
  2753. if (port->set_termios)
  2754. uart->port.set_termios = port->set_termios;
  2755. if (port->pm)
  2756. uart->port.pm = port->pm;
  2757. if (port->handle_break)
  2758. uart->port.handle_break = port->handle_break;
  2759. if (serial8250_isa_config != NULL)
  2760. serial8250_isa_config(0, &uart->port,
  2761. &uart->capabilities);
  2762. ret = uart_add_one_port(&serial8250_reg, &uart->port);
  2763. if (ret == 0)
  2764. ret = uart->port.line;
  2765. }
  2766. mutex_unlock(&serial_mutex);
  2767. return ret;
  2768. }
  2769. EXPORT_SYMBOL(serial8250_register_port);
  2770. /**
  2771. * serial8250_unregister_port - remove a 16x50 serial port at runtime
  2772. * @line: serial line number
  2773. *
  2774. * Remove one serial port. This may not be called from interrupt
  2775. * context. We hand the port back to the our control.
  2776. */
  2777. void serial8250_unregister_port(int line)
  2778. {
  2779. struct uart_8250_port *uart = &serial8250_ports[line];
  2780. mutex_lock(&serial_mutex);
  2781. uart_remove_one_port(&serial8250_reg, &uart->port);
  2782. if (serial8250_isa_devs) {
  2783. uart->port.flags &= ~UPF_BOOT_AUTOCONF;
  2784. uart->port.type = PORT_UNKNOWN;
  2785. uart->port.dev = &serial8250_isa_devs->dev;
  2786. uart->capabilities = uart_config[uart->port.type].flags;
  2787. uart_add_one_port(&serial8250_reg, &uart->port);
  2788. } else {
  2789. uart->port.dev = NULL;
  2790. }
  2791. mutex_unlock(&serial_mutex);
  2792. }
  2793. EXPORT_SYMBOL(serial8250_unregister_port);
  2794. static int __init serial8250_init(void)
  2795. {
  2796. int ret;
  2797. if (nr_uarts > UART_NR)
  2798. nr_uarts = UART_NR;
  2799. printk(KERN_INFO "Serial: 8250/16550 driver, "
  2800. "%d ports, IRQ sharing %sabled\n", nr_uarts,
  2801. share_irqs ? "en" : "dis");
  2802. #ifdef CONFIG_SPARC
  2803. ret = sunserial_register_minors(&serial8250_reg, UART_NR);
  2804. #else
  2805. serial8250_reg.nr = UART_NR;
  2806. ret = uart_register_driver(&serial8250_reg);
  2807. #endif
  2808. if (ret)
  2809. goto out;
  2810. serial8250_isa_devs = platform_device_alloc("serial8250",
  2811. PLAT8250_DEV_LEGACY);
  2812. if (!serial8250_isa_devs) {
  2813. ret = -ENOMEM;
  2814. goto unreg_uart_drv;
  2815. }
  2816. ret = platform_device_add(serial8250_isa_devs);
  2817. if (ret)
  2818. goto put_dev;
  2819. serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
  2820. ret = platform_driver_register(&serial8250_isa_driver);
  2821. if (ret == 0)
  2822. goto out;
  2823. platform_device_del(serial8250_isa_devs);
  2824. put_dev:
  2825. platform_device_put(serial8250_isa_devs);
  2826. unreg_uart_drv:
  2827. #ifdef CONFIG_SPARC
  2828. sunserial_unregister_minors(&serial8250_reg, UART_NR);
  2829. #else
  2830. uart_unregister_driver(&serial8250_reg);
  2831. #endif
  2832. out:
  2833. return ret;
  2834. }
  2835. static void __exit serial8250_exit(void)
  2836. {
  2837. struct platform_device *isa_dev = serial8250_isa_devs;
  2838. /*
  2839. * This tells serial8250_unregister_port() not to re-register
  2840. * the ports (thereby making serial8250_isa_driver permanently
  2841. * in use.)
  2842. */
  2843. serial8250_isa_devs = NULL;
  2844. platform_driver_unregister(&serial8250_isa_driver);
  2845. platform_device_unregister(isa_dev);
  2846. #ifdef CONFIG_SPARC
  2847. sunserial_unregister_minors(&serial8250_reg, UART_NR);
  2848. #else
  2849. uart_unregister_driver(&serial8250_reg);
  2850. #endif
  2851. }
  2852. module_init(serial8250_init);
  2853. module_exit(serial8250_exit);
  2854. EXPORT_SYMBOL(serial8250_suspend_port);
  2855. EXPORT_SYMBOL(serial8250_resume_port);
  2856. MODULE_LICENSE("GPL");
  2857. MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
  2858. module_param(share_irqs, uint, 0644);
  2859. MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
  2860. " (unsafe)");
  2861. module_param(nr_uarts, uint, 0644);
  2862. MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
  2863. module_param(skip_txen_test, uint, 0644);
  2864. MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
  2865. #ifdef CONFIG_SERIAL_8250_RSA
  2866. module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
  2867. MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
  2868. #endif
  2869. MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);