8250.c 76 KB

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