8250.c 66 KB

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