8250.c 66 KB

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