8250.c 75 KB

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