8250.c 86 KB

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