8250.c 80 KB

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