8250.c 82 KB

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