8250.c 84 KB

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