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