8250.c 71 KB

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