8250.c 66 KB

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