8250.c 67 KB

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