8250.c 65 KB

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