8250.c 65 KB

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