8250.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662
  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 char status;
  1194. unsigned int ret;
  1195. status = serial_in(up, UART_MSR);
  1196. ret = 0;
  1197. if (status & UART_MSR_DCD)
  1198. ret |= TIOCM_CAR;
  1199. if (status & UART_MSR_RI)
  1200. ret |= TIOCM_RNG;
  1201. if (status & UART_MSR_DSR)
  1202. ret |= TIOCM_DSR;
  1203. if (status & UART_MSR_CTS)
  1204. ret |= TIOCM_CTS;
  1205. return ret;
  1206. }
  1207. static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1208. {
  1209. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1210. unsigned char mcr = 0;
  1211. if (mctrl & TIOCM_RTS)
  1212. mcr |= UART_MCR_RTS;
  1213. if (mctrl & TIOCM_DTR)
  1214. mcr |= UART_MCR_DTR;
  1215. if (mctrl & TIOCM_OUT1)
  1216. mcr |= UART_MCR_OUT1;
  1217. if (mctrl & TIOCM_OUT2)
  1218. mcr |= UART_MCR_OUT2;
  1219. if (mctrl & TIOCM_LOOP)
  1220. mcr |= UART_MCR_LOOP;
  1221. mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
  1222. serial_out(up, UART_MCR, mcr);
  1223. }
  1224. static void serial8250_break_ctl(struct uart_port *port, int break_state)
  1225. {
  1226. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1227. unsigned long flags;
  1228. spin_lock_irqsave(&up->port.lock, flags);
  1229. if (break_state == -1)
  1230. up->lcr |= UART_LCR_SBC;
  1231. else
  1232. up->lcr &= ~UART_LCR_SBC;
  1233. serial_out(up, UART_LCR, up->lcr);
  1234. spin_unlock_irqrestore(&up->port.lock, flags);
  1235. }
  1236. static int serial8250_startup(struct uart_port *port)
  1237. {
  1238. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1239. unsigned long flags;
  1240. unsigned char lsr, iir;
  1241. int retval;
  1242. up->capabilities = uart_config[up->port.type].flags;
  1243. up->mcr = 0;
  1244. if (up->port.type == PORT_16C950) {
  1245. /* Wake up and initialize UART */
  1246. up->acr = 0;
  1247. serial_outp(up, UART_LCR, 0xBF);
  1248. serial_outp(up, UART_EFR, UART_EFR_ECB);
  1249. serial_outp(up, UART_IER, 0);
  1250. serial_outp(up, UART_LCR, 0);
  1251. serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
  1252. serial_outp(up, UART_LCR, 0xBF);
  1253. serial_outp(up, UART_EFR, UART_EFR_ECB);
  1254. serial_outp(up, UART_LCR, 0);
  1255. }
  1256. #ifdef CONFIG_SERIAL_8250_RSA
  1257. /*
  1258. * If this is an RSA port, see if we can kick it up to the
  1259. * higher speed clock.
  1260. */
  1261. enable_rsa(up);
  1262. #endif
  1263. /*
  1264. * Clear the FIFO buffers and disable them.
  1265. * (they will be reeanbled in set_termios())
  1266. */
  1267. serial8250_clear_fifos(up);
  1268. /*
  1269. * Clear the interrupt registers.
  1270. */
  1271. (void) serial_inp(up, UART_LSR);
  1272. (void) serial_inp(up, UART_RX);
  1273. (void) serial_inp(up, UART_IIR);
  1274. (void) serial_inp(up, UART_MSR);
  1275. /*
  1276. * At this point, there's no way the LSR could still be 0xff;
  1277. * if it is, then bail out, because there's likely no UART
  1278. * here.
  1279. */
  1280. if (!(up->port.flags & UPF_BUGGY_UART) &&
  1281. (serial_inp(up, UART_LSR) == 0xff)) {
  1282. printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
  1283. return -ENODEV;
  1284. }
  1285. /*
  1286. * For a XR16C850, we need to set the trigger levels
  1287. */
  1288. if (up->port.type == PORT_16850) {
  1289. unsigned char fctr;
  1290. serial_outp(up, UART_LCR, 0xbf);
  1291. fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
  1292. serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
  1293. serial_outp(up, UART_TRG, UART_TRG_96);
  1294. serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
  1295. serial_outp(up, UART_TRG, UART_TRG_96);
  1296. serial_outp(up, UART_LCR, 0);
  1297. }
  1298. /*
  1299. * If the "interrupt" for this port doesn't correspond with any
  1300. * hardware interrupt, we use a timer-based system. The original
  1301. * driver used to do this with IRQ0.
  1302. */
  1303. if (!is_real_interrupt(up->port.irq)) {
  1304. unsigned int timeout = up->port.timeout;
  1305. timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
  1306. up->timer.data = (unsigned long)up;
  1307. mod_timer(&up->timer, jiffies + timeout);
  1308. } else {
  1309. retval = serial_link_irq_chain(up);
  1310. if (retval)
  1311. return retval;
  1312. }
  1313. /*
  1314. * Now, initialize the UART
  1315. */
  1316. serial_outp(up, UART_LCR, UART_LCR_WLEN8);
  1317. spin_lock_irqsave(&up->port.lock, flags);
  1318. if (up->port.flags & UPF_FOURPORT) {
  1319. if (!is_real_interrupt(up->port.irq))
  1320. up->port.mctrl |= TIOCM_OUT1;
  1321. } else
  1322. /*
  1323. * Most PC uarts need OUT2 raised to enable interrupts.
  1324. */
  1325. if (is_real_interrupt(up->port.irq))
  1326. up->port.mctrl |= TIOCM_OUT2;
  1327. serial8250_set_mctrl(&up->port, up->port.mctrl);
  1328. /*
  1329. * Do a quick test to see if we receive an
  1330. * interrupt when we enable the TX irq.
  1331. */
  1332. serial_outp(up, UART_IER, UART_IER_THRI);
  1333. lsr = serial_in(up, UART_LSR);
  1334. iir = serial_in(up, UART_IIR);
  1335. serial_outp(up, UART_IER, 0);
  1336. if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
  1337. if (!(up->bugs & UART_BUG_TXEN)) {
  1338. up->bugs |= UART_BUG_TXEN;
  1339. pr_debug("ttyS%d - enabling bad tx status workarounds\n",
  1340. port->line);
  1341. }
  1342. } else {
  1343. up->bugs &= ~UART_BUG_TXEN;
  1344. }
  1345. spin_unlock_irqrestore(&up->port.lock, flags);
  1346. /*
  1347. * Finally, enable interrupts. Note: Modem status interrupts
  1348. * are set via set_termios(), which will be occurring imminently
  1349. * anyway, so we don't enable them here.
  1350. */
  1351. up->ier = UART_IER_RLSI | UART_IER_RDI;
  1352. serial_outp(up, UART_IER, up->ier);
  1353. if (up->port.flags & UPF_FOURPORT) {
  1354. unsigned int icp;
  1355. /*
  1356. * Enable interrupts on the AST Fourport board
  1357. */
  1358. icp = (up->port.iobase & 0xfe0) | 0x01f;
  1359. outb_p(0x80, icp);
  1360. (void) inb_p(icp);
  1361. }
  1362. /*
  1363. * And clear the interrupt registers again for luck.
  1364. */
  1365. (void) serial_inp(up, UART_LSR);
  1366. (void) serial_inp(up, UART_RX);
  1367. (void) serial_inp(up, UART_IIR);
  1368. (void) serial_inp(up, UART_MSR);
  1369. return 0;
  1370. }
  1371. static void serial8250_shutdown(struct uart_port *port)
  1372. {
  1373. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1374. unsigned long flags;
  1375. /*
  1376. * Disable interrupts from this port
  1377. */
  1378. up->ier = 0;
  1379. serial_outp(up, UART_IER, 0);
  1380. spin_lock_irqsave(&up->port.lock, flags);
  1381. if (up->port.flags & UPF_FOURPORT) {
  1382. /* reset interrupts on the AST Fourport board */
  1383. inb((up->port.iobase & 0xfe0) | 0x1f);
  1384. up->port.mctrl |= TIOCM_OUT1;
  1385. } else
  1386. up->port.mctrl &= ~TIOCM_OUT2;
  1387. serial8250_set_mctrl(&up->port, up->port.mctrl);
  1388. spin_unlock_irqrestore(&up->port.lock, flags);
  1389. /*
  1390. * Disable break condition and FIFOs
  1391. */
  1392. serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
  1393. serial8250_clear_fifos(up);
  1394. #ifdef CONFIG_SERIAL_8250_RSA
  1395. /*
  1396. * Reset the RSA board back to 115kbps compat mode.
  1397. */
  1398. disable_rsa(up);
  1399. #endif
  1400. /*
  1401. * Read data port to reset things, and then unlink from
  1402. * the IRQ chain.
  1403. */
  1404. (void) serial_in(up, UART_RX);
  1405. if (!is_real_interrupt(up->port.irq))
  1406. del_timer_sync(&up->timer);
  1407. else
  1408. serial_unlink_irq_chain(up);
  1409. }
  1410. static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
  1411. {
  1412. unsigned int quot;
  1413. /*
  1414. * Handle magic divisors for baud rates above baud_base on
  1415. * SMSC SuperIO chips.
  1416. */
  1417. if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
  1418. baud == (port->uartclk/4))
  1419. quot = 0x8001;
  1420. else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
  1421. baud == (port->uartclk/8))
  1422. quot = 0x8002;
  1423. else
  1424. quot = uart_get_divisor(port, baud);
  1425. return quot;
  1426. }
  1427. static void
  1428. serial8250_set_termios(struct uart_port *port, struct termios *termios,
  1429. struct termios *old)
  1430. {
  1431. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1432. unsigned char cval, fcr = 0;
  1433. unsigned long flags;
  1434. unsigned int baud, quot;
  1435. switch (termios->c_cflag & CSIZE) {
  1436. case CS5:
  1437. cval = UART_LCR_WLEN5;
  1438. break;
  1439. case CS6:
  1440. cval = UART_LCR_WLEN6;
  1441. break;
  1442. case CS7:
  1443. cval = UART_LCR_WLEN7;
  1444. break;
  1445. default:
  1446. case CS8:
  1447. cval = UART_LCR_WLEN8;
  1448. break;
  1449. }
  1450. if (termios->c_cflag & CSTOPB)
  1451. cval |= UART_LCR_STOP;
  1452. if (termios->c_cflag & PARENB)
  1453. cval |= UART_LCR_PARITY;
  1454. if (!(termios->c_cflag & PARODD))
  1455. cval |= UART_LCR_EPAR;
  1456. #ifdef CMSPAR
  1457. if (termios->c_cflag & CMSPAR)
  1458. cval |= UART_LCR_SPAR;
  1459. #endif
  1460. /*
  1461. * Ask the core to calculate the divisor for us.
  1462. */
  1463. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  1464. quot = serial8250_get_divisor(port, baud);
  1465. /*
  1466. * Oxford Semi 952 rev B workaround
  1467. */
  1468. if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
  1469. quot ++;
  1470. if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
  1471. if (baud < 2400)
  1472. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
  1473. else
  1474. fcr = uart_config[up->port.type].fcr;
  1475. }
  1476. /*
  1477. * MCR-based auto flow control. When AFE is enabled, RTS will be
  1478. * deasserted when the receive FIFO contains more characters than
  1479. * the trigger, or the MCR RTS bit is cleared. In the case where
  1480. * the remote UART is not using CTS auto flow control, we must
  1481. * have sufficient FIFO entries for the latency of the remote
  1482. * UART to respond. IOW, at least 32 bytes of FIFO.
  1483. */
  1484. if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
  1485. up->mcr &= ~UART_MCR_AFE;
  1486. if (termios->c_cflag & CRTSCTS)
  1487. up->mcr |= UART_MCR_AFE;
  1488. }
  1489. /*
  1490. * Ok, we're now changing the port state. Do it with
  1491. * interrupts disabled.
  1492. */
  1493. spin_lock_irqsave(&up->port.lock, flags);
  1494. /*
  1495. * Update the per-port timeout.
  1496. */
  1497. uart_update_timeout(port, termios->c_cflag, baud);
  1498. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  1499. if (termios->c_iflag & INPCK)
  1500. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  1501. if (termios->c_iflag & (BRKINT | PARMRK))
  1502. up->port.read_status_mask |= UART_LSR_BI;
  1503. /*
  1504. * Characteres to ignore
  1505. */
  1506. up->port.ignore_status_mask = 0;
  1507. if (termios->c_iflag & IGNPAR)
  1508. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  1509. if (termios->c_iflag & IGNBRK) {
  1510. up->port.ignore_status_mask |= UART_LSR_BI;
  1511. /*
  1512. * If we're ignoring parity and break indicators,
  1513. * ignore overruns too (for real raw support).
  1514. */
  1515. if (termios->c_iflag & IGNPAR)
  1516. up->port.ignore_status_mask |= UART_LSR_OE;
  1517. }
  1518. /*
  1519. * ignore all characters if CREAD is not set
  1520. */
  1521. if ((termios->c_cflag & CREAD) == 0)
  1522. up->port.ignore_status_mask |= UART_LSR_DR;
  1523. /*
  1524. * CTS flow control flag and modem status interrupts
  1525. */
  1526. up->ier &= ~UART_IER_MSI;
  1527. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  1528. up->ier |= UART_IER_MSI;
  1529. if (up->capabilities & UART_CAP_UUE)
  1530. up->ier |= UART_IER_UUE | UART_IER_RTOIE;
  1531. serial_out(up, UART_IER, up->ier);
  1532. if (up->capabilities & UART_CAP_EFR) {
  1533. unsigned char efr = 0;
  1534. /*
  1535. * TI16C752/Startech hardware flow control. FIXME:
  1536. * - TI16C752 requires control thresholds to be set.
  1537. * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
  1538. */
  1539. if (termios->c_cflag & CRTSCTS)
  1540. efr |= UART_EFR_CTS;
  1541. serial_outp(up, UART_LCR, 0xBF);
  1542. serial_outp(up, UART_EFR, efr);
  1543. }
  1544. if (up->capabilities & UART_NATSEMI) {
  1545. /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
  1546. serial_outp(up, UART_LCR, 0xe0);
  1547. } else {
  1548. serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
  1549. }
  1550. serial_outp(up, UART_DLL, quot & 0xff); /* LS of divisor */
  1551. serial_outp(up, UART_DLM, quot >> 8); /* MS of divisor */
  1552. /*
  1553. * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
  1554. * is written without DLAB set, this mode will be disabled.
  1555. */
  1556. if (up->port.type == PORT_16750)
  1557. serial_outp(up, UART_FCR, fcr);
  1558. serial_outp(up, UART_LCR, cval); /* reset DLAB */
  1559. up->lcr = cval; /* Save LCR */
  1560. if (up->port.type != PORT_16750) {
  1561. if (fcr & UART_FCR_ENABLE_FIFO) {
  1562. /* emulated UARTs (Lucent Venus 167x) need two steps */
  1563. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  1564. }
  1565. serial_outp(up, UART_FCR, fcr); /* set fcr */
  1566. }
  1567. serial8250_set_mctrl(&up->port, up->port.mctrl);
  1568. spin_unlock_irqrestore(&up->port.lock, flags);
  1569. }
  1570. static void
  1571. serial8250_pm(struct uart_port *port, unsigned int state,
  1572. unsigned int oldstate)
  1573. {
  1574. struct uart_8250_port *p = (struct uart_8250_port *)port;
  1575. serial8250_set_sleep(p, state != 0);
  1576. if (p->pm)
  1577. p->pm(port, state, oldstate);
  1578. }
  1579. /*
  1580. * Resource handling.
  1581. */
  1582. static int serial8250_request_std_resource(struct uart_8250_port *up)
  1583. {
  1584. unsigned int size = 8 << up->port.regshift;
  1585. int ret = 0;
  1586. switch (up->port.iotype) {
  1587. case UPIO_MEM:
  1588. if (!up->port.mapbase)
  1589. break;
  1590. if (!request_mem_region(up->port.mapbase, size, "serial")) {
  1591. ret = -EBUSY;
  1592. break;
  1593. }
  1594. if (up->port.flags & UPF_IOREMAP) {
  1595. up->port.membase = ioremap(up->port.mapbase, size);
  1596. if (!up->port.membase) {
  1597. release_mem_region(up->port.mapbase, size);
  1598. ret = -ENOMEM;
  1599. }
  1600. }
  1601. break;
  1602. case UPIO_HUB6:
  1603. case UPIO_PORT:
  1604. if (!request_region(up->port.iobase, size, "serial"))
  1605. ret = -EBUSY;
  1606. break;
  1607. }
  1608. return ret;
  1609. }
  1610. static void serial8250_release_std_resource(struct uart_8250_port *up)
  1611. {
  1612. unsigned int size = 8 << up->port.regshift;
  1613. switch (up->port.iotype) {
  1614. case UPIO_MEM:
  1615. if (!up->port.mapbase)
  1616. break;
  1617. if (up->port.flags & UPF_IOREMAP) {
  1618. iounmap(up->port.membase);
  1619. up->port.membase = NULL;
  1620. }
  1621. release_mem_region(up->port.mapbase, size);
  1622. break;
  1623. case UPIO_HUB6:
  1624. case UPIO_PORT:
  1625. release_region(up->port.iobase, size);
  1626. break;
  1627. }
  1628. }
  1629. static int serial8250_request_rsa_resource(struct uart_8250_port *up)
  1630. {
  1631. unsigned long start = UART_RSA_BASE << up->port.regshift;
  1632. unsigned int size = 8 << up->port.regshift;
  1633. int ret = 0;
  1634. switch (up->port.iotype) {
  1635. case UPIO_MEM:
  1636. ret = -EINVAL;
  1637. break;
  1638. case UPIO_HUB6:
  1639. case UPIO_PORT:
  1640. start += up->port.iobase;
  1641. if (!request_region(start, size, "serial-rsa"))
  1642. ret = -EBUSY;
  1643. break;
  1644. }
  1645. return ret;
  1646. }
  1647. static void serial8250_release_rsa_resource(struct uart_8250_port *up)
  1648. {
  1649. unsigned long offset = UART_RSA_BASE << up->port.regshift;
  1650. unsigned int size = 8 << up->port.regshift;
  1651. switch (up->port.iotype) {
  1652. case UPIO_MEM:
  1653. break;
  1654. case UPIO_HUB6:
  1655. case UPIO_PORT:
  1656. release_region(up->port.iobase + offset, size);
  1657. break;
  1658. }
  1659. }
  1660. static void serial8250_release_port(struct uart_port *port)
  1661. {
  1662. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1663. serial8250_release_std_resource(up);
  1664. if (up->port.type == PORT_RSA)
  1665. serial8250_release_rsa_resource(up);
  1666. }
  1667. static int serial8250_request_port(struct uart_port *port)
  1668. {
  1669. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1670. int ret = 0;
  1671. ret = serial8250_request_std_resource(up);
  1672. if (ret == 0 && up->port.type == PORT_RSA) {
  1673. ret = serial8250_request_rsa_resource(up);
  1674. if (ret < 0)
  1675. serial8250_release_std_resource(up);
  1676. }
  1677. return ret;
  1678. }
  1679. static void serial8250_config_port(struct uart_port *port, int flags)
  1680. {
  1681. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1682. int probeflags = PROBE_ANY;
  1683. int ret;
  1684. /*
  1685. * Don't probe for MCA ports on non-MCA machines.
  1686. */
  1687. if (up->port.flags & UPF_BOOT_ONLYMCA && !MCA_bus)
  1688. return;
  1689. /*
  1690. * Find the region that we can probe for. This in turn
  1691. * tells us whether we can probe for the type of port.
  1692. */
  1693. ret = serial8250_request_std_resource(up);
  1694. if (ret < 0)
  1695. return;
  1696. ret = serial8250_request_rsa_resource(up);
  1697. if (ret < 0)
  1698. probeflags &= ~PROBE_RSA;
  1699. if (flags & UART_CONFIG_TYPE)
  1700. autoconfig(up, probeflags);
  1701. if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
  1702. autoconfig_irq(up);
  1703. if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
  1704. serial8250_release_rsa_resource(up);
  1705. if (up->port.type == PORT_UNKNOWN)
  1706. serial8250_release_std_resource(up);
  1707. }
  1708. static int
  1709. serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
  1710. {
  1711. if (ser->irq >= NR_IRQS || ser->irq < 0 ||
  1712. ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
  1713. ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
  1714. ser->type == PORT_STARTECH)
  1715. return -EINVAL;
  1716. return 0;
  1717. }
  1718. static const char *
  1719. serial8250_type(struct uart_port *port)
  1720. {
  1721. int type = port->type;
  1722. if (type >= ARRAY_SIZE(uart_config))
  1723. type = 0;
  1724. return uart_config[type].name;
  1725. }
  1726. static struct uart_ops serial8250_pops = {
  1727. .tx_empty = serial8250_tx_empty,
  1728. .set_mctrl = serial8250_set_mctrl,
  1729. .get_mctrl = serial8250_get_mctrl,
  1730. .stop_tx = serial8250_stop_tx,
  1731. .start_tx = serial8250_start_tx,
  1732. .stop_rx = serial8250_stop_rx,
  1733. .enable_ms = serial8250_enable_ms,
  1734. .break_ctl = serial8250_break_ctl,
  1735. .startup = serial8250_startup,
  1736. .shutdown = serial8250_shutdown,
  1737. .set_termios = serial8250_set_termios,
  1738. .pm = serial8250_pm,
  1739. .type = serial8250_type,
  1740. .release_port = serial8250_release_port,
  1741. .request_port = serial8250_request_port,
  1742. .config_port = serial8250_config_port,
  1743. .verify_port = serial8250_verify_port,
  1744. };
  1745. static struct uart_8250_port serial8250_ports[UART_NR];
  1746. static void __init serial8250_isa_init_ports(void)
  1747. {
  1748. struct uart_8250_port *up;
  1749. static int first = 1;
  1750. int i;
  1751. if (!first)
  1752. return;
  1753. first = 0;
  1754. for (i = 0; i < UART_NR; i++) {
  1755. struct uart_8250_port *up = &serial8250_ports[i];
  1756. up->port.line = i;
  1757. spin_lock_init(&up->port.lock);
  1758. init_timer(&up->timer);
  1759. up->timer.function = serial8250_timeout;
  1760. /*
  1761. * ALPHA_KLUDGE_MCR needs to be killed.
  1762. */
  1763. up->mcr_mask = ~ALPHA_KLUDGE_MCR;
  1764. up->mcr_force = ALPHA_KLUDGE_MCR;
  1765. up->port.ops = &serial8250_pops;
  1766. }
  1767. for (i = 0, up = serial8250_ports; i < ARRAY_SIZE(old_serial_port);
  1768. i++, up++) {
  1769. up->port.iobase = old_serial_port[i].port;
  1770. up->port.irq = irq_canonicalize(old_serial_port[i].irq);
  1771. up->port.uartclk = old_serial_port[i].baud_base * 16;
  1772. up->port.flags = old_serial_port[i].flags;
  1773. up->port.hub6 = old_serial_port[i].hub6;
  1774. up->port.membase = old_serial_port[i].iomem_base;
  1775. up->port.iotype = old_serial_port[i].io_type;
  1776. up->port.regshift = old_serial_port[i].iomem_reg_shift;
  1777. if (share_irqs)
  1778. up->port.flags |= UPF_SHARE_IRQ;
  1779. }
  1780. }
  1781. static void __init
  1782. serial8250_register_ports(struct uart_driver *drv, struct device *dev)
  1783. {
  1784. int i;
  1785. serial8250_isa_init_ports();
  1786. for (i = 0; i < UART_NR; i++) {
  1787. struct uart_8250_port *up = &serial8250_ports[i];
  1788. up->port.dev = dev;
  1789. uart_add_one_port(drv, &up->port);
  1790. }
  1791. }
  1792. #ifdef CONFIG_SERIAL_8250_CONSOLE
  1793. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  1794. /*
  1795. * Wait for transmitter & holding register to empty
  1796. */
  1797. static inline void wait_for_xmitr(struct uart_8250_port *up)
  1798. {
  1799. unsigned int status, tmout = 10000;
  1800. /* Wait up to 10ms for the character(s) to be sent. */
  1801. do {
  1802. status = serial_in(up, UART_LSR);
  1803. if (status & UART_LSR_BI)
  1804. up->lsr_break_flag = UART_LSR_BI;
  1805. if (--tmout == 0)
  1806. break;
  1807. udelay(1);
  1808. } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
  1809. /* Wait up to 1s for flow control if necessary */
  1810. if (up->port.flags & UPF_CONS_FLOW) {
  1811. tmout = 1000000;
  1812. while (--tmout &&
  1813. ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
  1814. udelay(1);
  1815. }
  1816. }
  1817. /*
  1818. * Print a string to the serial port trying not to disturb
  1819. * any possible real use of the port...
  1820. *
  1821. * The console_lock must be held when we get here.
  1822. */
  1823. static void
  1824. serial8250_console_write(struct console *co, const char *s, unsigned int count)
  1825. {
  1826. struct uart_8250_port *up = &serial8250_ports[co->index];
  1827. unsigned int ier;
  1828. int i;
  1829. /*
  1830. * First save the UER then disable the interrupts
  1831. */
  1832. ier = serial_in(up, UART_IER);
  1833. if (up->capabilities & UART_CAP_UUE)
  1834. serial_out(up, UART_IER, UART_IER_UUE);
  1835. else
  1836. serial_out(up, UART_IER, 0);
  1837. /*
  1838. * Now, do each character
  1839. */
  1840. for (i = 0; i < count; i++, s++) {
  1841. wait_for_xmitr(up);
  1842. /*
  1843. * Send the character out.
  1844. * If a LF, also do CR...
  1845. */
  1846. serial_out(up, UART_TX, *s);
  1847. if (*s == 10) {
  1848. wait_for_xmitr(up);
  1849. serial_out(up, UART_TX, 13);
  1850. }
  1851. }
  1852. /*
  1853. * Finally, wait for transmitter to become empty
  1854. * and restore the IER
  1855. */
  1856. wait_for_xmitr(up);
  1857. serial_out(up, UART_IER, ier);
  1858. }
  1859. static int serial8250_console_setup(struct console *co, char *options)
  1860. {
  1861. struct uart_port *port;
  1862. int baud = 9600;
  1863. int bits = 8;
  1864. int parity = 'n';
  1865. int flow = 'n';
  1866. /*
  1867. * Check whether an invalid uart number has been specified, and
  1868. * if so, search for the first available port that does have
  1869. * console support.
  1870. */
  1871. if (co->index >= UART_NR)
  1872. co->index = 0;
  1873. port = &serial8250_ports[co->index].port;
  1874. if (!port->iobase && !port->membase)
  1875. return -ENODEV;
  1876. if (options)
  1877. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1878. return uart_set_options(port, co, baud, parity, bits, flow);
  1879. }
  1880. static struct uart_driver serial8250_reg;
  1881. static struct console serial8250_console = {
  1882. .name = "ttyS",
  1883. .write = serial8250_console_write,
  1884. .device = uart_console_device,
  1885. .setup = serial8250_console_setup,
  1886. .flags = CON_PRINTBUFFER,
  1887. .index = -1,
  1888. .data = &serial8250_reg,
  1889. };
  1890. static int __init serial8250_console_init(void)
  1891. {
  1892. serial8250_isa_init_ports();
  1893. register_console(&serial8250_console);
  1894. return 0;
  1895. }
  1896. console_initcall(serial8250_console_init);
  1897. static int __init find_port(struct uart_port *p)
  1898. {
  1899. int line;
  1900. struct uart_port *port;
  1901. for (line = 0; line < UART_NR; line++) {
  1902. port = &serial8250_ports[line].port;
  1903. if (p->iotype == port->iotype &&
  1904. p->iobase == port->iobase &&
  1905. p->membase == port->membase)
  1906. return line;
  1907. }
  1908. return -ENODEV;
  1909. }
  1910. int __init serial8250_start_console(struct uart_port *port, char *options)
  1911. {
  1912. int line;
  1913. line = find_port(port);
  1914. if (line < 0)
  1915. return -ENODEV;
  1916. add_preferred_console("ttyS", line, options);
  1917. printk("Adding console on ttyS%d at %s 0x%lx (options '%s')\n",
  1918. line, port->iotype == UPIO_MEM ? "MMIO" : "I/O port",
  1919. port->iotype == UPIO_MEM ? (unsigned long) port->mapbase :
  1920. (unsigned long) port->iobase, options);
  1921. if (!(serial8250_console.flags & CON_ENABLED)) {
  1922. serial8250_console.flags &= ~CON_PRINTBUFFER;
  1923. register_console(&serial8250_console);
  1924. }
  1925. return line;
  1926. }
  1927. #define SERIAL8250_CONSOLE &serial8250_console
  1928. #else
  1929. #define SERIAL8250_CONSOLE NULL
  1930. #endif
  1931. static struct uart_driver serial8250_reg = {
  1932. .owner = THIS_MODULE,
  1933. .driver_name = "serial",
  1934. .devfs_name = "tts/",
  1935. .dev_name = "ttyS",
  1936. .major = TTY_MAJOR,
  1937. .minor = 64,
  1938. .nr = UART_NR,
  1939. .cons = SERIAL8250_CONSOLE,
  1940. };
  1941. int __init early_serial_setup(struct uart_port *port)
  1942. {
  1943. if (port->line >= ARRAY_SIZE(serial8250_ports))
  1944. return -ENODEV;
  1945. serial8250_isa_init_ports();
  1946. serial8250_ports[port->line].port = *port;
  1947. serial8250_ports[port->line].port.ops = &serial8250_pops;
  1948. return 0;
  1949. }
  1950. /**
  1951. * serial8250_suspend_port - suspend one serial port
  1952. * @line: serial line number
  1953. * @level: the level of port suspension, as per uart_suspend_port
  1954. *
  1955. * Suspend one serial port.
  1956. */
  1957. void serial8250_suspend_port(int line)
  1958. {
  1959. uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
  1960. }
  1961. /**
  1962. * serial8250_resume_port - resume one serial port
  1963. * @line: serial line number
  1964. * @level: the level of port resumption, as per uart_resume_port
  1965. *
  1966. * Resume one serial port.
  1967. */
  1968. void serial8250_resume_port(int line)
  1969. {
  1970. uart_resume_port(&serial8250_reg, &serial8250_ports[line].port);
  1971. }
  1972. /*
  1973. * Register a set of serial devices attached to a platform device. The
  1974. * list is terminated with a zero flags entry, which means we expect
  1975. * all entries to have at least UPF_BOOT_AUTOCONF set.
  1976. */
  1977. static int __devinit serial8250_probe(struct device *dev)
  1978. {
  1979. struct plat_serial8250_port *p = dev->platform_data;
  1980. struct uart_port port;
  1981. int ret, i;
  1982. memset(&port, 0, sizeof(struct uart_port));
  1983. for (i = 0; p && p->flags != 0; p++, i++) {
  1984. port.iobase = p->iobase;
  1985. port.membase = p->membase;
  1986. port.irq = p->irq;
  1987. port.uartclk = p->uartclk;
  1988. port.regshift = p->regshift;
  1989. port.iotype = p->iotype;
  1990. port.flags = p->flags;
  1991. port.mapbase = p->mapbase;
  1992. port.hub6 = p->hub6;
  1993. port.dev = dev;
  1994. if (share_irqs)
  1995. port.flags |= UPF_SHARE_IRQ;
  1996. ret = serial8250_register_port(&port);
  1997. if (ret < 0) {
  1998. dev_err(dev, "unable to register port at index %d "
  1999. "(IO%lx MEM%lx IRQ%d): %d\n", i,
  2000. p->iobase, p->mapbase, p->irq, ret);
  2001. }
  2002. }
  2003. return 0;
  2004. }
  2005. /*
  2006. * Remove serial ports registered against a platform device.
  2007. */
  2008. static int __devexit serial8250_remove(struct device *dev)
  2009. {
  2010. int i;
  2011. for (i = 0; i < UART_NR; i++) {
  2012. struct uart_8250_port *up = &serial8250_ports[i];
  2013. if (up->port.dev == dev)
  2014. serial8250_unregister_port(i);
  2015. }
  2016. return 0;
  2017. }
  2018. static int serial8250_suspend(struct device *dev, pm_message_t state, u32 level)
  2019. {
  2020. int i;
  2021. if (level != SUSPEND_DISABLE)
  2022. return 0;
  2023. for (i = 0; i < UART_NR; i++) {
  2024. struct uart_8250_port *up = &serial8250_ports[i];
  2025. if (up->port.type != PORT_UNKNOWN && up->port.dev == dev)
  2026. uart_suspend_port(&serial8250_reg, &up->port);
  2027. }
  2028. return 0;
  2029. }
  2030. static int serial8250_resume(struct device *dev, u32 level)
  2031. {
  2032. int i;
  2033. if (level != RESUME_ENABLE)
  2034. return 0;
  2035. for (i = 0; i < UART_NR; i++) {
  2036. struct uart_8250_port *up = &serial8250_ports[i];
  2037. if (up->port.type != PORT_UNKNOWN && up->port.dev == dev)
  2038. uart_resume_port(&serial8250_reg, &up->port);
  2039. }
  2040. return 0;
  2041. }
  2042. static struct device_driver serial8250_isa_driver = {
  2043. .name = "serial8250",
  2044. .bus = &platform_bus_type,
  2045. .probe = serial8250_probe,
  2046. .remove = __devexit_p(serial8250_remove),
  2047. .suspend = serial8250_suspend,
  2048. .resume = serial8250_resume,
  2049. };
  2050. /*
  2051. * This "device" covers _all_ ISA 8250-compatible serial devices listed
  2052. * in the table in include/asm/serial.h
  2053. */
  2054. static struct platform_device *serial8250_isa_devs;
  2055. /*
  2056. * serial8250_register_port and serial8250_unregister_port allows for
  2057. * 16x50 serial ports to be configured at run-time, to support PCMCIA
  2058. * modems and PCI multiport cards.
  2059. */
  2060. static DECLARE_MUTEX(serial_sem);
  2061. static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
  2062. {
  2063. int i;
  2064. /*
  2065. * First, find a port entry which matches.
  2066. */
  2067. for (i = 0; i < UART_NR; i++)
  2068. if (uart_match_port(&serial8250_ports[i].port, port))
  2069. return &serial8250_ports[i];
  2070. /*
  2071. * We didn't find a matching entry, so look for the first
  2072. * free entry. We look for one which hasn't been previously
  2073. * used (indicated by zero iobase).
  2074. */
  2075. for (i = 0; i < UART_NR; i++)
  2076. if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
  2077. serial8250_ports[i].port.iobase == 0)
  2078. return &serial8250_ports[i];
  2079. /*
  2080. * That also failed. Last resort is to find any entry which
  2081. * doesn't have a real port associated with it.
  2082. */
  2083. for (i = 0; i < UART_NR; i++)
  2084. if (serial8250_ports[i].port.type == PORT_UNKNOWN)
  2085. return &serial8250_ports[i];
  2086. return NULL;
  2087. }
  2088. /**
  2089. * serial8250_register_port - register a serial port
  2090. * @port: serial port template
  2091. *
  2092. * Configure the serial port specified by the request. If the
  2093. * port exists and is in use, it is hung up and unregistered
  2094. * first.
  2095. *
  2096. * The port is then probed and if necessary the IRQ is autodetected
  2097. * If this fails an error is returned.
  2098. *
  2099. * On success the port is ready to use and the line number is returned.
  2100. */
  2101. int serial8250_register_port(struct uart_port *port)
  2102. {
  2103. struct uart_8250_port *uart;
  2104. int ret = -ENOSPC;
  2105. if (port->uartclk == 0)
  2106. return -EINVAL;
  2107. down(&serial_sem);
  2108. uart = serial8250_find_match_or_unused(port);
  2109. if (uart) {
  2110. uart_remove_one_port(&serial8250_reg, &uart->port);
  2111. uart->port.iobase = port->iobase;
  2112. uart->port.membase = port->membase;
  2113. uart->port.irq = port->irq;
  2114. uart->port.uartclk = port->uartclk;
  2115. uart->port.fifosize = port->fifosize;
  2116. uart->port.regshift = port->regshift;
  2117. uart->port.iotype = port->iotype;
  2118. uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
  2119. uart->port.mapbase = port->mapbase;
  2120. if (port->dev)
  2121. uart->port.dev = port->dev;
  2122. ret = uart_add_one_port(&serial8250_reg, &uart->port);
  2123. if (ret == 0)
  2124. ret = uart->port.line;
  2125. }
  2126. up(&serial_sem);
  2127. return ret;
  2128. }
  2129. EXPORT_SYMBOL(serial8250_register_port);
  2130. /**
  2131. * serial8250_unregister_port - remove a 16x50 serial port at runtime
  2132. * @line: serial line number
  2133. *
  2134. * Remove one serial port. This may not be called from interrupt
  2135. * context. We hand the port back to the our control.
  2136. */
  2137. void serial8250_unregister_port(int line)
  2138. {
  2139. struct uart_8250_port *uart = &serial8250_ports[line];
  2140. down(&serial_sem);
  2141. uart_remove_one_port(&serial8250_reg, &uart->port);
  2142. if (serial8250_isa_devs) {
  2143. uart->port.flags &= ~UPF_BOOT_AUTOCONF;
  2144. uart->port.type = PORT_UNKNOWN;
  2145. uart->port.dev = &serial8250_isa_devs->dev;
  2146. uart_add_one_port(&serial8250_reg, &uart->port);
  2147. } else {
  2148. uart->port.dev = NULL;
  2149. }
  2150. up(&serial_sem);
  2151. }
  2152. EXPORT_SYMBOL(serial8250_unregister_port);
  2153. static int __init serial8250_init(void)
  2154. {
  2155. int ret, i;
  2156. printk(KERN_INFO "Serial: 8250/16550 driver $Revision: 1.90 $ "
  2157. "%d ports, IRQ sharing %sabled\n", (int) UART_NR,
  2158. share_irqs ? "en" : "dis");
  2159. for (i = 0; i < NR_IRQS; i++)
  2160. spin_lock_init(&irq_lists[i].lock);
  2161. ret = uart_register_driver(&serial8250_reg);
  2162. if (ret)
  2163. goto out;
  2164. serial8250_isa_devs = platform_device_register_simple("serial8250",
  2165. -1, NULL, 0);
  2166. if (IS_ERR(serial8250_isa_devs)) {
  2167. ret = PTR_ERR(serial8250_isa_devs);
  2168. goto unreg;
  2169. }
  2170. serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
  2171. ret = driver_register(&serial8250_isa_driver);
  2172. if (ret == 0)
  2173. goto out;
  2174. platform_device_unregister(serial8250_isa_devs);
  2175. unreg:
  2176. uart_unregister_driver(&serial8250_reg);
  2177. out:
  2178. return ret;
  2179. }
  2180. static void __exit serial8250_exit(void)
  2181. {
  2182. struct platform_device *isa_dev = serial8250_isa_devs;
  2183. /*
  2184. * This tells serial8250_unregister_port() not to re-register
  2185. * the ports (thereby making serial8250_isa_driver permanently
  2186. * in use.)
  2187. */
  2188. serial8250_isa_devs = NULL;
  2189. driver_unregister(&serial8250_isa_driver);
  2190. platform_device_unregister(isa_dev);
  2191. uart_unregister_driver(&serial8250_reg);
  2192. }
  2193. module_init(serial8250_init);
  2194. module_exit(serial8250_exit);
  2195. EXPORT_SYMBOL(serial8250_suspend_port);
  2196. EXPORT_SYMBOL(serial8250_resume_port);
  2197. MODULE_LICENSE("GPL");
  2198. MODULE_DESCRIPTION("Generic 8250/16x50 serial driver $Revision: 1.90 $");
  2199. module_param(share_irqs, uint, 0644);
  2200. MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
  2201. " (unsafe)");
  2202. #ifdef CONFIG_SERIAL_8250_RSA
  2203. module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
  2204. MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
  2205. #endif
  2206. MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
  2207. /**
  2208. * register_serial - configure a 16x50 serial port at runtime
  2209. * @req: request structure
  2210. *
  2211. * Configure the serial port specified by the request. If the
  2212. * port exists and is in use an error is returned. If the port
  2213. * is not currently in the table it is added.
  2214. *
  2215. * The port is then probed and if necessary the IRQ is autodetected
  2216. * If this fails an error is returned.
  2217. *
  2218. * On success the port is ready to use and the line number is returned.
  2219. *
  2220. * Note: this function is deprecated - use serial8250_register_port
  2221. * instead.
  2222. */
  2223. int register_serial(struct serial_struct *req)
  2224. {
  2225. struct uart_port port;
  2226. port.iobase = req->port;
  2227. port.membase = req->iomem_base;
  2228. port.irq = req->irq;
  2229. port.uartclk = req->baud_base * 16;
  2230. port.fifosize = req->xmit_fifo_size;
  2231. port.regshift = req->iomem_reg_shift;
  2232. port.iotype = req->io_type;
  2233. port.flags = req->flags | UPF_BOOT_AUTOCONF;
  2234. port.mapbase = req->iomap_base;
  2235. port.dev = NULL;
  2236. if (share_irqs)
  2237. port.flags |= UPF_SHARE_IRQ;
  2238. if (HIGH_BITS_OFFSET)
  2239. port.iobase |= (long) req->port_high << HIGH_BITS_OFFSET;
  2240. /*
  2241. * If a clock rate wasn't specified by the low level driver, then
  2242. * default to the standard clock rate. This should be 115200 (*16)
  2243. * and should not depend on the architecture's BASE_BAUD definition.
  2244. * However, since this API will be deprecated, it's probably a
  2245. * better idea to convert the drivers to use the new API
  2246. * (serial8250_register_port and serial8250_unregister_port).
  2247. */
  2248. if (port.uartclk == 0) {
  2249. printk(KERN_WARNING
  2250. "Serial: registering port at [%08x,%08lx,%p] irq %d with zero baud_base\n",
  2251. port.iobase, port.mapbase, port.membase, port.irq);
  2252. printk(KERN_WARNING "Serial: see %s:%d for more information\n",
  2253. __FILE__, __LINE__);
  2254. dump_stack();
  2255. /*
  2256. * Fix it up for now, but this is only a temporary measure.
  2257. */
  2258. port.uartclk = BASE_BAUD * 16;
  2259. }
  2260. return serial8250_register_port(&port);
  2261. }
  2262. EXPORT_SYMBOL(register_serial);
  2263. /**
  2264. * unregister_serial - remove a 16x50 serial port at runtime
  2265. * @line: serial line number
  2266. *
  2267. * Remove one serial port. This may not be called from interrupt
  2268. * context. We hand the port back to our local PM control.
  2269. *
  2270. * Note: this function is deprecated - use serial8250_unregister_port
  2271. * instead.
  2272. */
  2273. void unregister_serial(int line)
  2274. {
  2275. serial8250_unregister_port(line);
  2276. }
  2277. EXPORT_SYMBOL(unregister_serial);