8250.c 83 KB

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