8250.c 67 KB

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