8250.c 80 KB

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