8250.c 66 KB

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