8250.c 65 KB

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