8250.c 84 KB

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