8250.c 75 KB

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