8250.c 75 KB

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