xr17v35x.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540
  1. /*****************************************************************************/
  2. /*
  3. * xr17v35x.c -- MaxLinear multiport serial driver.
  4. *
  5. *
  6. *****************************************************************************
  7. * Copyright (c) 2010, MaxLinear, Inc.
  8. *****************************************************************************
  9. *
  10. * Based on Linux 2.6.37 Kernel's drivers/serial/8250.c and /8250_pci.c
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * Multiport Serial Driver for MaxLinear's PCI Family of UARTs (XR17V258/254/252/358/354/352/8358/4358/8354)
  28. * (XR17D158/154/152)
  29. * ChangeLog:
  30. * for : LINUX 2.6.32 and newer (Tested on various kernel versions from 2.6.32 to 4.15)
  31. * date : July 2019
  32. * version : 2.6
  33. * Note: XR_17v35x_UART_RHR was not defined. Fixed
  34. * Check Release Notes for information on what has changed in the new version.
  35. *
  36. */
  37. #include <linux/module.h>
  38. #include <linux/tty.h>
  39. #include <linux/ioport.h>
  40. #include <linux/init.h>
  41. #include <linux/console.h>
  42. #include <linux/sysrq.h>
  43. #include <linux/delay.h>
  44. #include <linux/device.h>
  45. #include <linux/pci.h>
  46. #include <linux/sched.h>
  47. #include <linux/string.h>
  48. #include <linux/kernel.h>
  49. #include <linux/slab.h>
  50. #include <linux/export.h>
  51. #include <linux/kthread.h>
  52. #include <linux/mutex.h>
  53. #include <linux/sched.h>
  54. #include <linux/file.h>
  55. #include <linux/fs.h>
  56. #include <linux/uaccess.h>
  57. #include <linux/tty_flip.h>
  58. #include <linux/serial_reg.h>
  59. #include <linux/serial.h>
  60. #include <linux/serial_core.h>
  61. #include <asm/io.h>
  62. #include <asm/irq.h>
  63. #include <asm/irq.h>
  64. #include <asm/bitops.h>
  65. #include <asm/byteorder.h>
  66. #include <asm/serial.h>
  67. #include <asm/io.h>
  68. #include <asm/uaccess.h>
  69. #include "linux/version.h"
  70. #define _INLINE_ inline
  71. #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 8, 0)
  72. #define __devinitdata
  73. #define __devinit
  74. #define __devexit
  75. #define __devexit_p
  76. #endif
  77. #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 8, 0)
  78. struct serial_uart_config {
  79. char *name;
  80. int dfl_xmit_fifo_size;
  81. int flags;
  82. };
  83. #endif
  84. //void tty_flip_buffer_push(struct tty_port *port);
  85. /*
  86. * Definitions for PCI support.
  87. */
  88. #define FL_BASE_MASK 0x0007
  89. #define FL_BASE0 0x0000
  90. #define FL_GET_BASE(x) (x & FL_BASE_MASK)
  91. #define NR_PORTS 256
  92. #define XR_MAJOR 30
  93. #define XR_MINOR 0
  94. /*
  95. Set this parameter to 1 to enable Debug mode
  96. The Driver enables the internal loopback under debug mode
  97. To disable internal loopback go to serialxr_set_termios
  98. */
  99. #ifndef DEBUG
  100. #define DEBUG 0
  101. #endif
  102. /*
  103. * The special register set for XR17V35x UARTs.
  104. */
  105. #define XR_17v35x_UART_RHR 0
  106. #define XR_17v35x_UART_THR 0
  107. #define XR_17V35X_UART_DLD 2
  108. #define XR_17V35X_UART_MSR 6
  109. #define XR_17V35X_EXTENDED_FCTR 8
  110. #define XR_17V35X_EXTENDED_EFR 9
  111. #define XR_17V35X_TXFIFO_CNT 10
  112. #define XR_17V35X_EXTENDED_TXTRG 10
  113. #define XR_17V35X_RXFIFO_CNT 11
  114. #define XR_17V35X_EXTENDED_RXTRG 11
  115. #define XR_17V35X_UART_XOFF2 13
  116. #define XR_17V35X_UART_XOFF1 0xC0
  117. #define XR_17V35X_UART_XON1 0xE0
  118. #define XR_17V35X_FCTR_RTS_8DELAY 0x03
  119. #define XR_17V35X_FCTR_TRGD 192
  120. #define XR_17V35x_FCTR_RS485 0x20
  121. #define XR_17V35x_MPIOLVL_7_0 0x90
  122. #define XR_17V35x_MPIO3T_7_0 0x91
  123. #define XR_17V35x_MPIOSEL_7_0 0x93
  124. #define XR_17V35x_MPIOLVL_15_8 0x96
  125. #define XR_17V35x_MPIO3T_15_8 0x97
  126. #define XR_17V35x_MPIOSEL_15_8 0x99
  127. // Set this parameter to 1 to enable RS485 mode
  128. #define ENABLE_RS485 1
  129. //Set this parameter to 1 to enable DTR RS-485 half duplex direction control
  130. #define USE_DTR_RS485 0
  131. // Set this parameter to 1 to enabled internal loopback
  132. #define ENABLE_INTERNAL_LOOPBACK 0
  133. #define UART_17V35X_RX_OFFSET 0x100
  134. #define UART_17V35X_TX_OFFSET 0x100
  135. #define XR_17V35X_IER_RTSDTR 0x40
  136. #define XR_17V35X_IER_CTSDSR 0x80
  137. #define XR_17V35X_8XMODE 0x88
  138. #define XR_17V35X_4XMODE 0x89
  139. #define DIVISOR_CHANGED 0
  140. #define PCI_NUM_BAR_RESOURCES 6
  141. #define CAPTURE_SERIAL_INDEX 1
  142. struct serial_private {
  143. struct pci_dev *dev;
  144. unsigned int nr;
  145. void __iomem *remapped_bar[PCI_NUM_BAR_RESOURCES];
  146. struct pci_serial_quirk *quirk;
  147. int uart_index[NR_PORTS];
  148. int line[0];
  149. };
  150. struct pciserial_board {
  151. unsigned int flags;
  152. unsigned int num_ports;
  153. unsigned int base_baud;
  154. unsigned int uart_offset;
  155. unsigned int reg_shift;
  156. unsigned int first_offset;
  157. };
  158. /*
  159. * init function returns:
  160. * > 0 - number of ports
  161. * = 0 - use board->num_ports
  162. * < 0 - error
  163. */
  164. struct pci_serial_quirk {
  165. u32 vendor;
  166. u32 device;
  167. u32 subvendor;
  168. u32 subdevice;
  169. int (*init)(struct pci_dev *dev);
  170. int (*setup)(struct serial_private *,
  171. const struct pciserial_board *,
  172. struct uart_port *, int);
  173. void (*exit)(struct pci_dev *dev);
  174. };
  175. /*
  176. * This is the configuration table for all of the PCI serial boards
  177. * which we support. It is directly indexed by the xrpci_board_num_t enum
  178. * value, which is encoded in the pci_device_id PCI probe table's
  179. * driver_data member.
  180. *
  181. * The makeup of these names are:
  182. * pbn_bn{_bt}_n_baud
  183. *
  184. * bn = PCI BAR number
  185. * bt = Index using PCI BARs
  186. * n = number of serial ports
  187. * baud = baud rate
  188. */
  189. enum xrpci_board_num_t {
  190. xr_8port = 0,
  191. xr_4port,
  192. xr_2port,
  193. xr_4354port,
  194. xr_8354port,
  195. xr_4358port,
  196. xr_8358port,
  197. xr_258port,
  198. xr_254port,
  199. xr_252port,
  200. xr_158port,
  201. xr_154port,
  202. xr_152port,
  203. };
  204. static struct pciserial_board xrpciserial_boards[] __devinitdata = {
  205. [xr_8port] = {
  206. .flags = FL_BASE0,
  207. .num_ports = 8,
  208. .base_baud = 7812500*4,
  209. .uart_offset = 0x400,
  210. .reg_shift = 0,
  211. .first_offset = 0,
  212. },
  213. [xr_4port] = {
  214. .flags = FL_BASE0,
  215. .num_ports = 4,
  216. .base_baud = 7812500*4,
  217. .uart_offset = 0x400,
  218. .reg_shift = 0,
  219. .first_offset = 0,
  220. },
  221. [xr_2port] = {
  222. .flags = FL_BASE0,
  223. .num_ports = 2,
  224. .base_baud = 7812500*4,
  225. .uart_offset = 0x400,
  226. .reg_shift = 0,
  227. .first_offset = 0,
  228. },
  229. [xr_4354port] = {
  230. .flags = FL_BASE0,
  231. .num_ports = 8,
  232. .base_baud = 7812500*4,
  233. .uart_offset = 0x400,
  234. .reg_shift = 0,
  235. .first_offset = 0,
  236. },
  237. [xr_8354port] = {
  238. .flags = FL_BASE0,
  239. .num_ports = 12,
  240. .base_baud = 7812500*4,
  241. .uart_offset = 0x400,
  242. .reg_shift = 0,
  243. .first_offset = 0,
  244. },
  245. [xr_4358port] = {
  246. .flags = FL_BASE0,
  247. .num_ports = 12,
  248. .base_baud = 7812500*4,
  249. .uart_offset = 0x400,
  250. .reg_shift = 0,
  251. .first_offset = 0,
  252. },
  253. [xr_8358port] = {
  254. .flags = FL_BASE0,
  255. .num_ports = 16,
  256. .base_baud = 7812500*4,
  257. .uart_offset = 0x400,
  258. .reg_shift = 0,
  259. .first_offset = 0,
  260. },
  261. [xr_258port] = {
  262. .flags = FL_BASE0,
  263. .num_ports = 8,
  264. .base_baud = 1500000,
  265. .uart_offset = 0x200,
  266. .reg_shift = 0,
  267. .first_offset = 0,
  268. },
  269. [xr_254port] = {
  270. .flags = FL_BASE0,
  271. .num_ports = 4,
  272. .base_baud = 1500000,
  273. .uart_offset = 0x200,
  274. .reg_shift = 0,
  275. .first_offset = 0,
  276. },
  277. [xr_252port] = {
  278. .flags = FL_BASE0,
  279. .num_ports = 2,
  280. .base_baud = 1500000,
  281. .uart_offset = 0x200,
  282. .reg_shift = 0,
  283. .first_offset = 0,
  284. },
  285. [xr_158port] = {
  286. .flags = FL_BASE0,
  287. .num_ports = 8,
  288. .base_baud = 921600,
  289. .uart_offset = 0x200,
  290. .reg_shift = 0,
  291. .first_offset = 0,
  292. },
  293. [xr_154port] = {
  294. .flags = FL_BASE0,
  295. .num_ports = 4,
  296. .base_baud = 921600,
  297. .uart_offset = 0x200,
  298. .reg_shift = 0,
  299. .first_offset = 0,
  300. },
  301. [xr_152port] = {
  302. .flags = FL_BASE0,
  303. .num_ports = 2,
  304. .base_baud = 921600,
  305. .uart_offset = 0x200,
  306. .reg_shift = 0,
  307. .first_offset = 0,
  308. },
  309. };
  310. /*
  311. * Configuration:
  312. * share_irqs - whether we pass SA_SHIRQ to request_irq(). This option
  313. * is unsafe when used on edge-triggered interrupts.
  314. */
  315. #define SERIALEXAR_SHARE_IRQS 1
  316. unsigned int share_irqs = SERIALEXAR_SHARE_IRQS;
  317. /*
  318. * Debugging.
  319. */
  320. #if 0
  321. #define DEBUG_AUTOCONF(fmt...) printk(fmt)
  322. #else
  323. #define DEBUG_AUTOCONF(fmt...) do { } while (0)
  324. #endif
  325. #if DEBUG
  326. #define DEBUG_INTR(fmt...) printk(fmt)
  327. #else
  328. #define DEBUG_INTR(fmt...) do { } while (0)
  329. #endif
  330. #define PASS_LIMIT 256
  331. /*
  332. * We default to IRQ0 for the "no irq" hack. Some
  333. * machine types want others as well - they're free
  334. * to redefine this in their header file.
  335. */
  336. #define is_real_interrupt(irq) ((irq) != 0)
  337. #define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
  338. #define LSR_SAVE_FLAGS UART_LSR_BRK_ERROR_BITS
  339. struct uart_xr_port {
  340. struct uart_port port;
  341. struct timer_list timer; /* "no irq" timer */
  342. struct list_head list; /* ports on this IRQ */
  343. unsigned short capabilities; /* port capabilities */
  344. unsigned short bugs; /* port bugs */
  345. unsigned int tx_loadsz; /* transmit fifo load size */
  346. unsigned char acr;
  347. unsigned char ier;
  348. unsigned char lcr;
  349. unsigned char mcr;
  350. unsigned char mcr_mask; /* mask of user bits */
  351. unsigned char mcr_force; /* mask of forced bits */
  352. unsigned char lsr_saved_flags;
  353. unsigned char msr_saved_flags;
  354. unsigned short deviceid;
  355. unsigned char channelnum;
  356. unsigned char multidrop_address;
  357. unsigned char multidrop_mode;
  358. unsigned char is_match_address;
  359. /*
  360. * We provide a per-port pm hook.
  361. */
  362. void (*pm)(struct uart_port *port,
  363. unsigned int state, unsigned int old);
  364. };
  365. struct irq_info {
  366. struct hlist_node node;
  367. int irq;
  368. spinlock_t lock; /* Protects list not the hash */
  369. struct list_head *head;
  370. };
  371. #define NR_IRQ_HASH 32 /* Can be adjusted later */
  372. static struct hlist_head irq_lists[NR_IRQ_HASH];
  373. static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
  374. /*
  375. * Here we define the default xmit fifo size used for each type of UART.
  376. */
  377. #define PORT_MAX_XR 2
  378. #define XRPCIe_TYPE 1 // the second entry that is [1] in the array
  379. #define XRPCI25x_TYPE 2 // the third entry that is [2] in the array
  380. static const struct serial_uart_config uart_config[PORT_MAX_XR+1] = {
  381. { "Unknown", 1, 0 },
  382. { "XR17v35x", 256, 0 },
  383. { "XR17v25x", 64, 0 },
  384. };
  385. static int
  386. setup_port(struct serial_private *priv, struct uart_port *port,
  387. int bar, int offset, int regshift)
  388. {
  389. struct pci_dev *dev = priv->dev;
  390. unsigned long base, len;
  391. if (bar >= PCI_NUM_BAR_RESOURCES)
  392. return -EINVAL;
  393. base = pci_resource_start(dev, bar);
  394. if (pci_resource_flags(dev, bar) & IORESOURCE_MEM) {
  395. len = pci_resource_len(dev, bar);
  396. if (!priv->remapped_bar[bar])
  397. priv->remapped_bar[bar] = ioremap(base, len);
  398. if (!priv->remapped_bar[bar])
  399. return -ENOMEM;
  400. port->iotype = UPIO_MEM;
  401. port->iobase = 0;
  402. port->mapbase = base + offset;
  403. port->membase = priv->remapped_bar[bar] + offset;
  404. port->regshift = regshift;
  405. } else {
  406. //Exar's got to be memory mapped. some hardware reading error?
  407. return -EINVAL;
  408. }
  409. return 0;
  410. }
  411. static int
  412. pci_default_setup(struct serial_private *priv,
  413. const struct pciserial_board *board,
  414. struct uart_port *port, int idx)
  415. {
  416. unsigned int bar, offset = board->first_offset;
  417. bar = FL_GET_BASE(board->flags);
  418. offset += idx * board->uart_offset;
  419. //printk(KERN_INFO "Exar PCIe device 0x%x\n", priv->dev->device);
  420. if((priv->dev->device == 0x4354 || priv->dev->device == 0x8354) && (idx >= 4))
  421. {
  422. offset += 0x1000; // the ports on expansion device for 0x(4/8)354 sit at bar0+0x2000 offset.
  423. // So we need to add 0x1000 here as 4*0x400
  424. }
  425. return setup_port(priv, port, bar, offset, board->reg_shift);
  426. }
  427. /*
  428. * Master list of serial port init/setup/exit quirks.
  429. * This does not describe the general nature of the port.
  430. * (ie, baud base, number and location of ports, etc)
  431. *
  432. * This list is ordered alphabetically by vendor then device.
  433. * Specific entries must come before more generic entries.
  434. */
  435. static struct pci_serial_quirk pci_serial_quirks[] = {
  436. {
  437. .vendor = 0x13a8,
  438. .device = 0x358,
  439. .subvendor = PCI_ANY_ID,
  440. .subdevice = PCI_ANY_ID,
  441. .setup = pci_default_setup,
  442. },
  443. {
  444. .vendor = 0x13a8,
  445. .device = 0x354,
  446. .subvendor = PCI_ANY_ID,
  447. .subdevice = PCI_ANY_ID,
  448. .setup = pci_default_setup,
  449. },
  450. {
  451. .vendor = 0x13a8,
  452. .device = 0x352,
  453. .subvendor = PCI_ANY_ID,
  454. .subdevice = PCI_ANY_ID,
  455. .setup = pci_default_setup,
  456. },
  457. {
  458. .vendor = 0x13a8,
  459. .device = 0x4354,
  460. .subvendor = PCI_ANY_ID,
  461. .subdevice = PCI_ANY_ID,
  462. .setup = pci_default_setup,
  463. },
  464. {
  465. .vendor = 0x13a8,
  466. .device = 0x8354,
  467. .subvendor = PCI_ANY_ID,
  468. .subdevice = PCI_ANY_ID,
  469. .setup = pci_default_setup,
  470. },
  471. {
  472. .vendor = 0x13a8,
  473. .device = 0x4358,
  474. .subvendor = PCI_ANY_ID,
  475. .subdevice = PCI_ANY_ID,
  476. .setup = pci_default_setup,
  477. },
  478. {
  479. .vendor = 0x13a8,
  480. .device = 0x8358,
  481. .subvendor = PCI_ANY_ID,
  482. .subdevice = PCI_ANY_ID,
  483. .setup = pci_default_setup,
  484. },
  485. {
  486. .vendor = 0x13a8,
  487. .device = 0x258,
  488. .subvendor = PCI_ANY_ID,
  489. .subdevice = PCI_ANY_ID,
  490. .setup = pci_default_setup,
  491. },
  492. {
  493. .vendor = 0x13a8,
  494. .device = 0x254,
  495. .subvendor = PCI_ANY_ID,
  496. .subdevice = PCI_ANY_ID,
  497. .setup = pci_default_setup,
  498. },
  499. {
  500. .vendor = 0x13a8,
  501. .device = 0x252,
  502. .subvendor = PCI_ANY_ID,
  503. .subdevice = PCI_ANY_ID,
  504. .setup = pci_default_setup,
  505. },
  506. {
  507. .vendor = 0x13a8,
  508. .device = 0x158,
  509. .subvendor = PCI_ANY_ID,
  510. .subdevice = PCI_ANY_ID,
  511. .setup = pci_default_setup,
  512. },
  513. {
  514. .vendor = 0x13a8,
  515. .device = 0x154,
  516. .subvendor = PCI_ANY_ID,
  517. .subdevice = PCI_ANY_ID,
  518. .setup = pci_default_setup,
  519. },
  520. {
  521. .vendor = 0x13a8,
  522. .device = 0x152,
  523. .subvendor = PCI_ANY_ID,
  524. .subdevice = PCI_ANY_ID,
  525. .setup = pci_default_setup,
  526. },
  527. };
  528. static inline int quirk_id_matches(u32 quirk_id, u32 dev_id)
  529. {
  530. return quirk_id == PCI_ANY_ID || quirk_id == dev_id;
  531. }
  532. static struct pci_serial_quirk *find_quirk(struct pci_dev *dev)
  533. {
  534. struct pci_serial_quirk *quirk;
  535. for (quirk = pci_serial_quirks; ; quirk++)
  536. if (quirk_id_matches(quirk->vendor, dev->vendor) &&
  537. quirk_id_matches(quirk->device, dev->device))
  538. break;
  539. return quirk;
  540. }
  541. static _INLINE_ unsigned int serial_in(struct uart_xr_port *up, int offset)
  542. {
  543. return readb(up->port.membase + offset);
  544. }
  545. static _INLINE_ void
  546. serial_out(struct uart_xr_port *up, int offset, int value)
  547. {
  548. writeb(value, up->port.membase + offset);
  549. }
  550. static void serialxr_stop_tx(struct uart_port *port)
  551. {
  552. struct uart_xr_port *up = (struct uart_xr_port *)port;
  553. int lcr;
  554. if (up->ier & UART_IER_THRI) {
  555. up->ier &= ~UART_IER_THRI;
  556. lcr = serial_in(up, UART_LCR);
  557. if (lcr & 0x80) {
  558. printk(KERN_INFO "channelnum %d: serialxr stop tx - LCR = 0x%x", up->channelnum, lcr);
  559. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  560. }
  561. serial_out(up, UART_IER, up->ier);
  562. }
  563. }
  564. static void serialxr_start_tx(struct uart_port *port)
  565. {
  566. struct uart_xr_port *up = (struct uart_xr_port *)port;
  567. int lcr;
  568. if (!(up->ier & UART_IER_THRI)) {
  569. up->ier |= UART_IER_THRI;
  570. lcr = serial_in(up, UART_LCR);
  571. if (lcr & 0x80) {
  572. printk(KERN_INFO"channelnum %d: serialxr start tx - LCR = 0x%x", up->channelnum, lcr);
  573. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  574. }
  575. serial_out(up, UART_IER, up->ier);
  576. }
  577. }
  578. static void serialxr_stop_rx(struct uart_port *port)
  579. {
  580. struct uart_xr_port *up = (struct uart_xr_port *)port;
  581. int lcr;
  582. up->ier &= ~UART_IER_RLSI;
  583. lcr = serial_in(up, UART_LCR);
  584. if (lcr & 0x80) {
  585. printk(KERN_INFO"channelnum %d: serialxr stop rx - LCR = 0x%x", up->channelnum, lcr);
  586. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  587. }
  588. up->port.read_status_mask &= ~UART_LSR_DR;
  589. serial_out(up, UART_IER, up->ier);
  590. }
  591. static void serialxr_enable_ms(struct uart_port *port)
  592. {
  593. struct uart_xr_port *up = (struct uart_xr_port *)port;
  594. int lcr;
  595. up->ier |= UART_IER_MSI;
  596. lcr = serial_in(up, UART_LCR);
  597. if (lcr & 0x80) {
  598. printk(KERN_INFO"channelnum %d: serialxr enable ms - LCR = 0x%x", up->channelnum, lcr);
  599. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  600. }
  601. serial_out(up, UART_IER, up->ier);
  602. }
  603. static void
  604. receive_chars(struct uart_xr_port *up, unsigned int *status)
  605. {
  606. #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
  607. struct uart_port *port = &up->port;
  608. #else
  609. struct tty_struct *tty = up->port.state->port.tty;
  610. #endif
  611. unsigned char ch[256], lsr = *status;
  612. char flag;
  613. int i, lcr, datasize_in_fifo, port_index;
  614. unsigned char tmp;
  615. datasize_in_fifo = serial_in(up, XR_17V35X_RXFIFO_CNT);
  616. while(datasize_in_fifo!=serial_in(up, XR_17V35X_RXFIFO_CNT))
  617. /*Read Receive Fifo count until we get read same value twice*/
  618. datasize_in_fifo=serial_in(up, XR_17V35X_RXFIFO_CNT);
  619. port_index = up->port.line;
  620. flag = TTY_NORMAL;
  621. if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE | UART_LSR_FE | UART_LSR_OE)))
  622. {
  623. /*
  624. * Mask off conditions which should be ignored.
  625. */
  626. lsr &= up->port.read_status_mask;
  627. if (lsr & UART_LSR_OE)
  628. {
  629. printk("OverRun Happen....");
  630. }
  631. if (lsr & UART_LSR_BI)
  632. {
  633. DEBUG_INTR("handling break....");
  634. flag = TTY_BREAK;
  635. }
  636. else if (lsr & UART_LSR_PE)
  637. {
  638. flag = TTY_PARITY;
  639. if(up->multidrop_mode == 1)
  640. {
  641. //memcpy_fromio(ch, up->port.membase + UART_17V35X_RX_OFFSET, datasize_in_fifo);
  642. for(i=0;i<datasize_in_fifo;i++)
  643. {
  644. lcr = serial_in(up, UART_LCR);
  645. if (lcr & 0x80) {
  646. printk(KERN_INFO"channelnum %d: receive chars (multidrop mode) - LCR = 0x%x", up->channelnum, lcr);
  647. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  648. }
  649. ch[i]= serial_in(up, XR_17v35x_UART_RHR);
  650. }
  651. up->port.icount.rx+=datasize_in_fifo;
  652. //up->port.icount.rx+=datasize_in_fifo;
  653. DEBUG_INTR("Receive address byte:%02x\n",ch[0]);
  654. if(up->is_match_address == 0)
  655. {
  656. if(ch[0] == up->multidrop_address)
  657. {
  658. DEBUG_INTR(" Enable the receiver\n");
  659. //set EFR[4] = 1; enable the shaded bits
  660. tmp = serial_in(up, XR_17V35X_EXTENDED_EFR);
  661. tmp |=0x10;
  662. serial_out(up, XR_17V35X_EXTENDED_EFR, tmp);
  663. serial_out(up,XR_17V35X_UART_MSR, 0);//Enable the receiver
  664. //set EFR[4] = 0; disable the shaded bits
  665. tmp = serial_in(up, XR_17V35X_EXTENDED_EFR);
  666. tmp &=~0x10;
  667. serial_out(up, XR_17V35X_EXTENDED_EFR, tmp);
  668. up->is_match_address = 1;
  669. }
  670. else
  671. {
  672. //do nothing
  673. }
  674. }
  675. else
  676. {
  677. if(ch[0] == up->multidrop_address)
  678. {
  679. //do dothing
  680. }
  681. else
  682. {
  683. DEBUG_INTR(" Disable the receiver\n");
  684. //set EFR[4] = 1; enable the shaded bits
  685. tmp = serial_in(up, XR_17V35X_EXTENDED_EFR);
  686. tmp |=0x10;
  687. serial_out(up, XR_17V35X_EXTENDED_EFR, tmp);
  688. serial_out(up,XR_17V35X_UART_MSR, 0x04);//Disable the receiver
  689. //set EFR[4] = 0; disable the shaded bits
  690. tmp = serial_in(up, XR_17V35X_EXTENDED_EFR);
  691. tmp &=~0x10;
  692. serial_out(up, XR_17V35X_EXTENDED_EFR, tmp);
  693. up->is_match_address = 0;
  694. }
  695. }
  696. return;
  697. }
  698. if(up->multidrop_mode == 0)
  699. {
  700. printk("handling port<%d> Parity error....(%d)\n",port_index,up->multidrop_mode);
  701. }
  702. }
  703. else if (lsr & UART_LSR_FE)
  704. {
  705. DEBUG_INTR("handling Frame error....\n");
  706. flag = TTY_FRAME;
  707. }
  708. }
  709. //memcpy_fromio(ch, up->port.membase + UART_17V35X_RX_OFFSET, datasize_in_fifo);
  710. //print_hex_dump(KERN_DEBUG,"R:",DUMP_PREFIX_NONE,16,1,ch,datasize_in_fifo,1);
  711. for(i=0;i<datasize_in_fifo;i++)
  712. {
  713. lcr = serial_in(up, UART_LCR);
  714. if (lcr & 0x80) {
  715. printk(KERN_INFO"channelnum %d: receive chars - LCR = 0x%x", up->channelnum, lcr);
  716. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  717. }
  718. ch[i]= serial_in(up, XR_17v35x_UART_RHR);
  719. }
  720. up->port.icount.rx+=datasize_in_fifo;
  721. for(i = 0; i < datasize_in_fifo; i++)
  722. {
  723. if (uart_handle_sysrq_char(&up->port, ch[i]))
  724. continue;
  725. uart_insert_char(&up->port, lsr, UART_LSR_OE, ch[i], flag);
  726. }
  727. spin_unlock(&up->port.lock);
  728. #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
  729. tty_flip_buffer_push(&port->state->port);
  730. #else
  731. tty_flip_buffer_push(tty);
  732. #endif
  733. spin_lock(&up->port.lock);
  734. DEBUG_INTR(" LSR_DR...");
  735. }
  736. static void transmit_chars(struct uart_xr_port *up)
  737. {
  738. struct circ_buf *xmit = &up->port.state->xmit;
  739. int count, bytes_in_fifo, tmp;
  740. int i, lcr;
  741. unsigned char *ch;
  742. if (up->port.x_char) {
  743. serial_out(up, UART_TX, up->port.x_char);
  744. up->port.icount.tx++;
  745. up->port.x_char = 0;
  746. return;
  747. }
  748. if (uart_tx_stopped(&up->port)) {
  749. serialxr_stop_tx(&up->port);
  750. return;
  751. }
  752. if (uart_circ_empty(xmit)) {
  753. serialxr_stop_tx(&up->port);
  754. return;
  755. }
  756. bytes_in_fifo = serial_in(up, XR_17V35X_TXFIFO_CNT);
  757. // read the fifo count untill we get the same value twice
  758. while (bytes_in_fifo != serial_in(up, XR_17V35X_TXFIFO_CNT))
  759. bytes_in_fifo = serial_in(up, XR_17V35X_TXFIFO_CNT);
  760. // how much buffer is availabe now to write?
  761. count = up->port.fifosize - bytes_in_fifo;
  762. if (uart_circ_chars_pending(xmit) < count)
  763. count = uart_circ_chars_pending(xmit);
  764. do
  765. {
  766. // if the count is more than (tail to end of the buffer), transmit only the rest here.
  767. // tail+tmp&(UART_XMIT_SIZE-1) will reset the tail to the starting of the circular buffer
  768. if( ((xmit->tail + count) & (UART_XMIT_SIZE-1)) < xmit->tail)
  769. {
  770. tmp = UART_XMIT_SIZE - xmit->tail;
  771. //memcpy_toio(up->port.membase + UART_17V35X_TX_OFFSET, &(xmit->buf[xmit->tail]), tmp);
  772. ch = (unsigned char *)&(xmit->buf[xmit->tail]);
  773. //print_hex_dump(KERN_DEBUG,"T:",DUMP_PREFIX_NONE,16,1,&(xmit->buf[xmit->tail]),tmp,1);
  774. for(i=0;i<tmp;i++)
  775. {
  776. lcr = serial_in(up, UART_LCR);
  777. if (lcr & 0x80) {
  778. printk(KERN_INFO"channelnum %d: transmit_chars1 - LCR = 0x%x", up->channelnum, lcr);
  779. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  780. }
  781. serial_out(up, XR_17v35x_UART_THR, ch[i]);
  782. }
  783. xmit->tail += tmp;
  784. xmit->tail &= (UART_XMIT_SIZE-1);
  785. up->port.icount.tx += tmp;
  786. count -= tmp;
  787. }
  788. else
  789. {
  790. ch = (unsigned char *)&(xmit->buf[xmit->tail]);
  791. //memcpy_toio(up->port.membase + UART_17V35X_TX_OFFSET, &(xmit->buf[xmit->tail]), count);
  792. for(i=0;i < count;i++)
  793. {
  794. lcr = serial_in(up, UART_LCR);
  795. if (lcr & 0x80) {
  796. printk(KERN_INFO"channelnum %d: transmit_chars2 - LCR = 0x%x", up->channelnum, lcr);
  797. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  798. }
  799. serial_out(up, XR_17v35x_UART_THR, ch[i]);
  800. }
  801. xmit->tail += count;
  802. xmit->tail &= UART_XMIT_SIZE - 1;
  803. up->port.icount.tx += count;
  804. count = 0;
  805. }
  806. }while (count > 0);
  807. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  808. uart_write_wakeup(&up->port);
  809. DEBUG_INTR("THRE...");
  810. if (uart_circ_empty(xmit))
  811. serialxr_stop_tx(&up->port);
  812. }
  813. static unsigned int check_modem_status(struct uart_xr_port *up)
  814. {
  815. unsigned int status = serial_in(up, UART_MSR);
  816. status |= up->msr_saved_flags;
  817. up->msr_saved_flags = 0;
  818. if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
  819. up->port.state != NULL) {
  820. if (status & UART_MSR_TERI)
  821. up->port.icount.rng++;
  822. if (status & UART_MSR_DDSR)
  823. up->port.icount.dsr++;
  824. if (status & UART_MSR_DDCD)
  825. uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
  826. if (status & UART_MSR_DCTS)
  827. uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
  828. wake_up_interruptible(&up->port.state->port.delta_msr_wait);
  829. }
  830. return status;
  831. }
  832. /*
  833. * This handles the interrupt from one port.
  834. */
  835. static void serialxr_handle_port(struct uart_xr_port *up)
  836. {
  837. unsigned int status;
  838. unsigned long flags;
  839. spin_lock_irqsave(&up->port.lock, flags);
  840. status = serial_in(up, UART_LSR);
  841. DEBUG_INTR("status = %x...", status);
  842. if (status & (UART_LSR_DR | UART_LSR_BI))
  843. receive_chars(up, &status);
  844. check_modem_status(up);
  845. if (status & UART_LSR_THRE)
  846. transmit_chars(up);
  847. spin_unlock_irqrestore(&up->port.lock, flags);
  848. }
  849. /*
  850. * This is the serial driver's interrupt routine.
  851. *
  852. * Arjan thinks the old way was overly complex, so it got simplified.
  853. * Alan disagrees, saying that need the complexity to handle the weird
  854. * nature of ISA shared interrupts. (This is a special exception.)
  855. *
  856. * In order to handle ISA shared interrupts properly, we need to check
  857. * that all ports have been serviced, and therefore the ISA interrupt
  858. * line has been de-asserted.
  859. *
  860. * This means we need to loop through all ports. checking that they
  861. * don't have an interrupt pending.
  862. */
  863. static irqreturn_t serialxr_interrupt(int irq, void *dev_id)
  864. {
  865. struct irq_info *i = dev_id;
  866. struct list_head *l, *end = NULL;
  867. int pass_counter = 0, handled = 0;
  868. DEBUG_INTR("serialxr_interrupt(%d)...", irq);
  869. spin_lock(&i->lock);
  870. l = i->head;
  871. do {
  872. struct uart_xr_port *up;
  873. unsigned int iir, lcr;
  874. up = list_entry(l, struct uart_xr_port, list);
  875. lcr = serial_in(up, UART_LCR); // store value of LCR
  876. if (lcr & 0x80) {
  877. printk(KERN_INFO"channelnum %d: serialxr interrupt - LCR = 0x%x", up->channelnum, lcr);
  878. serial_out(up, UART_LCR, lcr & 0x7F); // ensure LCR bit-7=0 before reading UART_IIR
  879. }
  880. iir = serial_in(up, UART_IIR);
  881. if (!(iir & UART_IIR_NO_INT)) {
  882. serialxr_handle_port(up);
  883. handled = 1;
  884. end = NULL;
  885. } else if (end == NULL)
  886. end = l;
  887. serial_out(up, UART_LCR, lcr); // restore LCR
  888. l = l->next;
  889. /* add INT0 clear */
  890. serial_in(up,0x80);
  891. if (l == i->head && pass_counter++ > 256) {
  892. /* If we hit this, we're dead. */
  893. printk(KERN_ERR "serialxr: too much work for "
  894. "irq%d\n", irq);
  895. break;
  896. }
  897. } while (l != end);
  898. spin_unlock(&i->lock);
  899. DEBUG_INTR("end.\n");
  900. return IRQ_RETVAL(handled);
  901. }
  902. /*
  903. * To support ISA shared interrupts, we need to have one interrupt
  904. * handler that ensures that the IRQ line has been deasserted
  905. * before returning. Failing to do this will result in the IRQ
  906. * line being stuck active, and, since ISA irqs are edge triggered,
  907. * no more IRQs will be seen.
  908. */
  909. static void serial_do_unlink(struct irq_info *i, struct uart_xr_port *up)
  910. {
  911. spin_lock_irq(&i->lock);
  912. if (!list_empty(i->head)) {
  913. if (i->head == &up->list)
  914. i->head = i->head->next;
  915. list_del(&up->list);
  916. } else {
  917. BUG_ON(i->head != &up->list);
  918. i->head = NULL;
  919. }
  920. spin_unlock_irq(&i->lock);
  921. /* List empty so throw away the hash node */
  922. if (i->head == NULL) {
  923. hlist_del(&i->node);
  924. kfree(i);
  925. }
  926. }
  927. static int serial_link_irq_chain(struct uart_xr_port *up)
  928. {
  929. struct hlist_head *h;
  930. struct hlist_node *n;
  931. struct irq_info *i;
  932. int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
  933. mutex_lock(&hash_mutex);
  934. h = &irq_lists[up->port.irq % NR_IRQ_HASH];
  935. hlist_for_each(n, h) {
  936. i = hlist_entry(n, struct irq_info, node);
  937. if (i->irq == up->port.irq)
  938. break;
  939. }
  940. if (n == NULL) {
  941. i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
  942. if (i == NULL) {
  943. mutex_unlock(&hash_mutex);
  944. return -ENOMEM;
  945. }
  946. spin_lock_init(&i->lock);
  947. i->irq = up->port.irq;
  948. hlist_add_head(&i->node, h);
  949. }
  950. mutex_unlock(&hash_mutex);
  951. spin_lock_irq(&i->lock);
  952. if (i->head) {
  953. list_add_tail(&up->list, i->head);
  954. spin_unlock_irq(&i->lock);
  955. ret = 0;
  956. } else {
  957. INIT_LIST_HEAD(&up->list);
  958. i->head = &up->list;
  959. spin_unlock_irq(&i->lock);
  960. irq_flags |= up->port.irqflags;
  961. ret = request_irq(up->port.irq, serialxr_interrupt,
  962. irq_flags, "xrserial", i);
  963. if (ret < 0)
  964. serial_do_unlink(i, up);
  965. }
  966. return ret;
  967. }
  968. static void serial_unlink_irq_chain(struct uart_xr_port *up)
  969. {
  970. struct irq_info *i;
  971. struct hlist_node *n;
  972. struct hlist_head *h;
  973. mutex_lock(&hash_mutex);
  974. h = &irq_lists[up->port.irq % NR_IRQ_HASH];
  975. hlist_for_each(n, h) {
  976. i = hlist_entry(n, struct irq_info, node);
  977. if (i->irq == up->port.irq)
  978. break;
  979. }
  980. BUG_ON(n == NULL);
  981. BUG_ON(i->head == NULL);
  982. if (list_empty(i->head))
  983. free_irq(up->port.irq, i);
  984. serial_do_unlink(i, up);
  985. mutex_unlock(&hash_mutex);
  986. }
  987. static inline int poll_timeout(int timeout)
  988. {
  989. return timeout > 6 ? (timeout / 2 - 2) : 1;
  990. }
  991. /*
  992. * This function is used to handle ports that do not have an
  993. * interrupt. This doesn't work very well for 16450's, but gives
  994. * barely passable results for a 16550A. (Although at the expense
  995. * of much CPU overhead).
  996. */
  997. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
  998. static void serialxr_timeout(struct timer_list *p_tl)
  999. #else
  1000. static void serialxr_timeout(unsigned long data)
  1001. #endif
  1002. {
  1003. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
  1004. struct uart_xr_port *up = from_timer(up, p_tl, timer);
  1005. #else
  1006. struct uart_xr_port *up = (struct uart_xr_port *)data;
  1007. #endif
  1008. unsigned int iir;
  1009. int lcr;
  1010. lcr = serial_in(up, UART_LCR); // check value of LCR
  1011. if (lcr & 0x80) {
  1012. serial_out(up, UART_LCR, lcr & 0x7F); // ensure LCR bit-7=0 before reading UART_IIR
  1013. }
  1014. iir = serial_in(up, UART_IIR);
  1015. if (!(iir & UART_IIR_NO_INT))
  1016. serialxr_handle_port(up);
  1017. mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
  1018. }
  1019. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  1020. static unsigned int serialxr_tx_empty(struct uart_port *port)
  1021. {
  1022. struct uart_xr_port *up = (struct uart_xr_port *)port;
  1023. unsigned long flags;
  1024. unsigned int lsr;
  1025. spin_lock_irqsave(&up->port.lock, flags);
  1026. lsr = serial_in(up, UART_LSR);
  1027. up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
  1028. spin_unlock_irqrestore(&up->port.lock, flags);
  1029. return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
  1030. }
  1031. static unsigned int serialxr_get_mctrl(struct uart_port *port)
  1032. {
  1033. struct uart_xr_port *up = (struct uart_xr_port *)port;
  1034. unsigned int status;
  1035. unsigned int ret;
  1036. status = check_modem_status(up);
  1037. ret = 0;
  1038. if (status & UART_MSR_DCD)
  1039. ret |= TIOCM_CAR;
  1040. if (status & UART_MSR_RI)
  1041. ret |= TIOCM_RNG;
  1042. if (status & UART_MSR_DSR)
  1043. ret |= TIOCM_DSR;
  1044. if (status & UART_MSR_CTS)
  1045. ret |= TIOCM_CTS;
  1046. return ret;
  1047. }
  1048. static void serialxr_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1049. {
  1050. struct uart_xr_port *up = (struct uart_xr_port *)port;
  1051. unsigned char mcr = 0, efr;
  1052. if (mctrl & TIOCM_RTS)
  1053. mcr |= UART_MCR_RTS;
  1054. if (mctrl & TIOCM_DTR)
  1055. mcr |= UART_MCR_DTR;
  1056. if (mctrl & TIOCM_OUT1)
  1057. mcr |= UART_MCR_OUT1;
  1058. if (mctrl & TIOCM_OUT2)
  1059. mcr |= UART_MCR_OUT2;
  1060. if (mctrl & TIOCM_LOOP)
  1061. mcr |= UART_MCR_LOOP;
  1062. mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
  1063. efr = serial_in(up, UART_EFR);
  1064. efr = efr & 0xEF; // clear access to shaded registers so that write to MCR does not change from using DTR to RTS for RS-485 control
  1065. #if USE_DTR_RS485
  1066. mcr |= 0x04;
  1067. printk(KERN_INFO "serialxr_set_mctrl mcr=%02x\n",mcr);
  1068. #endif
  1069. serial_out(up, UART_EFR, efr);
  1070. serial_out(up, UART_MCR, mcr);
  1071. }
  1072. static void serialxr_break_ctl(struct uart_port *port, int break_state)
  1073. {
  1074. struct uart_xr_port *up = (struct uart_xr_port *)port;
  1075. unsigned long flags;
  1076. spin_lock_irqsave(&up->port.lock, flags);
  1077. if (break_state == -1)
  1078. up->lcr |= UART_LCR_SBC;
  1079. else
  1080. up->lcr &= ~UART_LCR_SBC;
  1081. serial_out(up, UART_LCR, up->lcr);
  1082. spin_unlock_irqrestore(&up->port.lock, flags);
  1083. }
  1084. static int serialxr_startup(struct uart_port *port)
  1085. {
  1086. struct uart_xr_port *up = (struct uart_xr_port *)port;
  1087. unsigned long flags;
  1088. unsigned int fctr_reg=0;
  1089. int retval, lcr;
  1090. up->capabilities = uart_config[up->port.type].flags;
  1091. serial_out(up, XR_17V35X_EXTENDED_EFR, UART_EFR_ECB);
  1092. lcr = serial_in(up, UART_LCR);
  1093. if (lcr & 0x80) {
  1094. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  1095. }
  1096. serial_out(up, UART_IER, 0);
  1097. /* Set the RX/TX trigger levels */
  1098. /* These are some default values, the OEMs can change these values
  1099. * according to their best case scenarios */
  1100. if(up->deviceid > 0x258) // PCIe device
  1101. {
  1102. serial_out(up, XR_17V35X_EXTENDED_RXTRG, 32);
  1103. serial_out(up, XR_17V35X_EXTENDED_TXTRG, 64);
  1104. }
  1105. else // for 25x
  1106. {
  1107. serial_out(up, XR_17V35X_EXTENDED_RXTRG, 32); // 25x
  1108. serial_out(up, XR_17V35X_EXTENDED_TXTRG, 32);
  1109. }
  1110. /* Hysteresis level of 8, Enable Auto RS-485 Mode */
  1111. fctr_reg=serial_in(up,XR_17V35X_EXTENDED_FCTR);
  1112. DEBUG_INTR(KERN_INFO "serialxr_startup: FCTR=0x%x",fctr_reg);
  1113. #if ENABLE_RS485
  1114. serial_out(up, XR_17V35X_EXTENDED_FCTR, fctr_reg|XR_17V35X_FCTR_TRGD | XR_17V35X_FCTR_RTS_8DELAY | XR_17V35x_FCTR_RS485);
  1115. #if USE_DTR_RS485
  1116. serial_out(up, UART_MCR, 0x04); //use DTR for Auto RS-485 Control
  1117. #endif
  1118. #else
  1119. serial_out(up, XR_17V35X_EXTENDED_FCTR, (fctr_reg|XR_17V35X_FCTR_TRGD | XR_17V35X_FCTR_RTS_8DELAY)&0xDF);
  1120. #endif
  1121. serial_out(up, UART_LCR, 0);
  1122. /* Wake up and initialize UART */
  1123. serial_out(up, XR_17V35X_EXTENDED_EFR, UART_EFR_ECB | 0x10/*Enable Shaded bits access*/);
  1124. serial_out(up,XR_17V35X_UART_MSR, 0);
  1125. serial_out(up, UART_LCR, 0); // Do LCR first to avoid LCR bit-7=1 before writing to IER
  1126. serial_out(up, UART_IER, 0);
  1127. /*
  1128. * Clear the FIFO buffers and disable them.
  1129. * (they will be reeanbled in set_termios())
  1130. */
  1131. lcr = serial_in(up, UART_LCR);
  1132. if (lcr & 0x80) {
  1133. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  1134. }
  1135. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  1136. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  1137. lcr = serial_in(up, UART_LCR);
  1138. if (lcr & 0x80) {
  1139. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  1140. }
  1141. serial_out(up, UART_FCR, 0);
  1142. /*
  1143. * Clear the interrupt registers.
  1144. */
  1145. (void) serial_in(up, UART_LSR);
  1146. (void) serial_in(up, UART_RX);
  1147. (void) serial_in(up, UART_IIR);
  1148. (void) serial_in(up, UART_MSR);
  1149. /* add INT0 clear */
  1150. serial_in(up,0x80);
  1151. if(port->irq) {
  1152. retval = serial_link_irq_chain(up);
  1153. if(retval)
  1154. return retval;
  1155. }
  1156. /*
  1157. * Now, initialize the UART
  1158. */
  1159. serial_out(up, UART_LCR, UART_LCR_WLEN8);
  1160. spin_lock_irqsave(&up->port.lock, flags);
  1161. /*
  1162. * Most PC uarts need OUT2 raised to enable interrupts.
  1163. */
  1164. if (is_real_interrupt(up->port.irq))
  1165. up->port.mctrl |= TIOCM_OUT2;
  1166. //to enable intenal loop, uncomment the line below
  1167. //up->port.mctrl |= TIOCM_LOOP;
  1168. serialxr_set_mctrl(&up->port, up->port.mctrl);
  1169. spin_unlock_irqrestore(&up->port.lock, flags);
  1170. /*
  1171. * Finally, enable interrupts. Note: Modem status interrupts
  1172. * are set via set_termios(), which will be occurring imminently
  1173. * anyway, so we don't enable them here.
  1174. */
  1175. up->ier = UART_IER_RLSI | UART_IER_RDI;
  1176. lcr = serial_in(up, UART_LCR);
  1177. if (lcr & 0x80) {
  1178. printk(KERN_INFO"channelnum %d: serialxr startup - LCR = 0x%x", up->channelnum, lcr);
  1179. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  1180. }
  1181. serial_out(up, UART_IER, up->ier);
  1182. /*
  1183. * And clear the interrupt registers again for luck.
  1184. */
  1185. (void) serial_in(up, UART_LSR);
  1186. (void) serial_in(up, UART_RX);
  1187. (void) serial_in(up, UART_IIR);
  1188. (void) serial_in(up, UART_MSR);
  1189. /* add INT0 clear */
  1190. serial_in(up,0x80);
  1191. return 0;
  1192. }
  1193. static void serialxr_shutdown(struct uart_port *port)
  1194. {
  1195. struct uart_xr_port *up = (struct uart_xr_port *)port;
  1196. unsigned long flags;
  1197. unsigned char lsr;
  1198. int i = 0;
  1199. int lcr;
  1200. while(1)
  1201. {
  1202. i++;
  1203. lsr = serial_in(up, UART_LSR);
  1204. if((lsr&0x60) != 0x60)
  1205. printk("serialxr_shutdown wait TXFIFO Empty %02x",lsr);
  1206. else
  1207. break;
  1208. msleep(1);
  1209. if(i>1000) break;
  1210. }
  1211. /*
  1212. * Disable interrupts from this port
  1213. */
  1214. up->ier = 0;
  1215. lcr = serial_in(up, UART_LCR);
  1216. if (lcr & 0x80) {
  1217. printk(KERN_INFO"channelnum %d: serialxr_shutdown1 - LCR = 0x%x", up->channelnum, lcr);
  1218. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  1219. }
  1220. serial_out(up, UART_IER, 0);
  1221. spin_lock_irqsave(&up->port.lock, flags);
  1222. up->port.mctrl &= ~TIOCM_OUT2;
  1223. serialxr_set_mctrl(&up->port, up->port.mctrl);
  1224. spin_unlock_irqrestore(&up->port.lock, flags);
  1225. /*
  1226. * Disable break condition and FIFOs
  1227. */
  1228. serial_out(up, UART_LCR, serial_in(up, UART_LCR) & (~UART_LCR_SBC) & 0x7f);
  1229. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  1230. UART_FCR_CLEAR_RCVR |
  1231. UART_FCR_CLEAR_XMIT);
  1232. lcr = serial_in(up, UART_LCR);
  1233. if (lcr & 0x80) {
  1234. printk(KERN_INFO"channelnum %d: serialxr_shutdown2 - LCR = 0x%x", up->channelnum, lcr);
  1235. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  1236. }
  1237. serial_out(up, UART_FCR, 0);
  1238. /*
  1239. * Read data port to reset things, and then unlink from
  1240. * the IRQ chain.
  1241. */
  1242. (void) serial_in(up, UART_RX);
  1243. #if 1
  1244. if (port->irq)
  1245. serial_unlink_irq_chain(up);
  1246. #endif
  1247. }
  1248. static int quot_coeff = 16 ;
  1249. static unsigned char low_baudrate_mode = 0;
  1250. static unsigned int uart_get_divisor_exar(struct uart_port *port, unsigned int baud)
  1251. {
  1252. unsigned int quot;
  1253. /*
  1254. * Old custom speed handling.
  1255. */
  1256. if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
  1257. quot = port->custom_divisor;
  1258. else
  1259. {
  1260. if(low_baudrate_mode)
  1261. quot = DIV_ROUND_CLOSEST(port->uartclk/4, quot_coeff * baud);
  1262. else
  1263. quot = DIV_ROUND_CLOSEST(port->uartclk, quot_coeff * baud);
  1264. }
  1265. // DEBUG_INTR(KERN_INFO "uart_get_divisor_exar:UartClk=%d QuotCoeff=0x%x",port->uartclk,quot_coeff);
  1266. return quot;
  1267. }
  1268. static unsigned int serialxr_get_divisor(struct uart_port *port, unsigned int baud)
  1269. {
  1270. unsigned int quot;
  1271. quot = uart_get_divisor_exar(port, baud);
  1272. return quot;
  1273. }
  1274. static void
  1275. serialxr_set_special_baudrate(struct uart_port *port,unsigned int special_baudrate)
  1276. {
  1277. struct uart_xr_port *up = (struct uart_xr_port *)port;
  1278. signed int baud, quot;
  1279. signed int quot_fraction = 0;
  1280. unsigned char val_4xmode;
  1281. unsigned char val_8xmode;
  1282. unsigned char lcr_bak;
  1283. unsigned int reg_read;
  1284. int port_index = up->channelnum;
  1285. printk(KERN_INFO "Enter in serialxr_set non-standard baudrate:%d channelnum:%d\n",special_baudrate,up->channelnum);
  1286. baud = special_baudrate/*uart_get_baud_rate(port, termios, old, 0, port->uartclk/4)*/;
  1287. lcr_bak = serial_in(up, UART_LCR);
  1288. val_4xmode = serial_in(up, XR_17V35X_4XMODE);
  1289. val_8xmode = serial_in(up, XR_17V35X_8XMODE);
  1290. if((port_index > 15)||(port_index < 0))
  1291. {
  1292. return;
  1293. }
  1294. switch(up->deviceid)
  1295. {
  1296. case 0x4354:
  1297. case 0x8354:
  1298. if(port_index >= 4) port_index = port_index - 4;
  1299. break;
  1300. case 0x4358:
  1301. case 0x8358:
  1302. if(port_index >= 8) port_index = port_index - 8;
  1303. break;
  1304. default:
  1305. //Do nothing
  1306. break;
  1307. }
  1308. if(baud < 12500000/16)
  1309. {//using the 16x mode
  1310. val_4xmode &=~(1 << port_index);
  1311. val_8xmode &=~(1 << port_index);
  1312. quot_coeff = 16;
  1313. printk(KERN_INFO "Using the 16x Mode\n");
  1314. }
  1315. else if((baud >= 12500000/16)&&(baud < 12500000/4))
  1316. {//using the 8x mode
  1317. val_4xmode &=~(1 << port_index);
  1318. val_8xmode |=(1 << port_index);
  1319. quot_coeff = 8;
  1320. printk(KERN_INFO "Using the 8x Mode\n");
  1321. }
  1322. else
  1323. {//using the 4x mode
  1324. val_4xmode |=(1 << port_index);
  1325. val_8xmode &=~(1 << port_index);
  1326. quot_coeff = 4;
  1327. printk(KERN_INFO "Using the 4x Mode\n");
  1328. }
  1329. serial_out(up, XR_17V35X_8XMODE, val_8xmode);
  1330. serial_out(up, XR_17V35X_4XMODE, val_4xmode);
  1331. quot = serialxr_get_divisor(port, baud);
  1332. if(!((up->deviceid == 0x152)||(up->deviceid == 0x154)||(up->deviceid == 0x158)))
  1333. {
  1334. unsigned int quot_16;
  1335. DEBUG_INTR(KERN_INFO "XR_17V35X uartclk:%d Quot=0x%x\n",port->uartclk,quot);
  1336. if(quot_coeff == 16)
  1337. {
  1338. quot_16 = DIV_ROUND_CLOSEST(port->uartclk, baud);
  1339. quot_fraction = quot_16 & 0x0f;
  1340. quot = (quot_16 >>4);
  1341. }
  1342. else if(quot_coeff == 8)
  1343. {
  1344. quot_16 = DIV_ROUND_CLOSEST(port->uartclk*2, baud);
  1345. quot_fraction = quot_16 & 0x0f;
  1346. quot = (quot_16 >>4);
  1347. }
  1348. else if(quot_coeff == 4)
  1349. {
  1350. quot_16 = DIV_ROUND_CLOSEST(port->uartclk*4, baud);
  1351. quot_fraction = quot_16 & 0x0f;
  1352. quot = (quot_16 >>4);
  1353. }
  1354. else
  1355. {
  1356. }
  1357. }
  1358. serial_out(up, UART_LCR, lcr_bak | UART_LCR_DLAB);/* set DLAB */
  1359. serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
  1360. serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
  1361. //Fractional baud rate support
  1362. if((up->deviceid == 0x152)||(up->deviceid == 0x154)||(up->deviceid == 0x158))
  1363. {
  1364. //nothing to do , because these devices do not have support for the DLD register.
  1365. }
  1366. else
  1367. {
  1368. reg_read=(serial_in(up, XR_17V35X_UART_DLD)&0xF0);
  1369. DEBUG_INTR(KERN_INFO "serialxr_set_special_baudrate: quot =0x%x quot_fraction=0x%x DLD_reg=0x%x\n",quot,quot_fraction,reg_read);
  1370. serial_out(up, XR_17V35X_UART_DLD, quot_fraction | reg_read);
  1371. reg_read=serial_in(up, XR_17V35X_UART_DLD);
  1372. }
  1373. serial_out(up, UART_LCR, lcr_bak); /* reset DLAB */
  1374. }
  1375. static void
  1376. serialxr_set_termios(struct uart_port *port, struct ktermios *termios,
  1377. struct ktermios *old)
  1378. {
  1379. struct uart_xr_port *up = (struct uart_xr_port *)port;
  1380. unsigned char cval;
  1381. unsigned long flags;
  1382. signed int baud, quot;
  1383. signed int quot_fraction = 0;
  1384. unsigned char val_4xmode;
  1385. unsigned char val_8xmode;
  1386. unsigned int reg_read;
  1387. unsigned char efr,mcr;
  1388. int lcr;
  1389. int port_index = up->channelnum;
  1390. switch (termios->c_cflag & CSIZE)
  1391. {
  1392. case CS5:
  1393. cval = 0x00;
  1394. break;
  1395. case CS6:
  1396. cval = 0x01;
  1397. break;
  1398. case CS7:
  1399. cval = 0x02;
  1400. break;
  1401. default:
  1402. case CS8:
  1403. cval = 0x03;
  1404. break;
  1405. }
  1406. if (termios->c_cflag & CSTOPB)
  1407. cval |= 0x04;
  1408. if (termios->c_cflag & PARENB)
  1409. cval |= UART_LCR_PARITY;
  1410. if (!(termios->c_cflag & PARODD))
  1411. cval |= UART_LCR_EPAR;
  1412. #ifdef CMSPAR
  1413. if (termios->c_cflag & CMSPAR)
  1414. cval |= UART_LCR_SPAR;
  1415. #endif
  1416. /*
  1417. * Ask the core to calculate the divisor for us.
  1418. */
  1419. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/4);
  1420. printk(KERN_INFO "\nserialxr_set_termios: Port Index:%d c_ispeed:%d c_ospeed:%d baud=%d",port_index,termios->c_ispeed,termios->c_ospeed,baud);
  1421. val_4xmode = serial_in(up, XR_17V35X_4XMODE);
  1422. val_8xmode = serial_in(up, XR_17V35X_8XMODE);
  1423. if((port_index > 15)||(port_index < 0))
  1424. {
  1425. return;
  1426. }
  1427. switch(up->deviceid)
  1428. {
  1429. case 0x4354:
  1430. case 0x8354:
  1431. if(port_index >= 4) port_index = port_index - 4;
  1432. break;
  1433. case 0x4358:
  1434. case 0x8358:
  1435. if(port_index >= 8) port_index = port_index - 8;
  1436. break;
  1437. default:
  1438. //Do nothing
  1439. break;
  1440. }
  1441. if(baud < 120)
  1442. {
  1443. //set EFR[4] = 1; enable the shaded bits
  1444. efr = serial_in(up, XR_17V35X_EXTENDED_EFR);
  1445. serial_out(up, XR_17V35X_EXTENDED_EFR, efr | 0x10);
  1446. mcr=serial_in(up, UART_MCR);
  1447. serial_out(up, UART_MCR, mcr | 0x80 );//set the prescaler (MCR bit-7 = 1, requires EFR bit-4 = 1) to divide the clock by 4.
  1448. //Restore the EFR Value
  1449. serial_out(up, XR_17V35X_EXTENDED_EFR, efr);
  1450. low_baudrate_mode = 1;
  1451. }
  1452. else
  1453. {
  1454. //set EFR[4] = 1; enable the shaded bits
  1455. efr = serial_in(up, XR_17V35X_EXTENDED_EFR);
  1456. serial_out(up, XR_17V35X_EXTENDED_EFR, efr | 0x10);
  1457. mcr=serial_in(up, UART_MCR);
  1458. serial_out(up, UART_MCR, mcr & 0x7f );//clr the prescaler (MCR bit-7 = 1, requires EFR bit-4 = 1) to divide the clock by 1.
  1459. //Restore the EFR Value
  1460. serial_out(up, XR_17V35X_EXTENDED_EFR, efr);
  1461. low_baudrate_mode = 0;
  1462. }
  1463. if(baud < 12500000/16)
  1464. {//using the 16x mode
  1465. val_4xmode &=~(1 << port_index);
  1466. val_8xmode &=~(1 << port_index);
  1467. quot_coeff = 16;
  1468. printk(KERN_INFO "Using the 16x Mode\n");
  1469. }
  1470. else if((baud >= 12500000/16)&&(baud < 12500000/4))
  1471. {//using the 8x mode
  1472. val_4xmode &=~(1 << port_index);
  1473. val_8xmode |=(1 << port_index);
  1474. quot_coeff = 8;
  1475. printk(KERN_INFO "Using the 8x Mode\n");
  1476. }
  1477. else
  1478. {//using the 4x mode
  1479. val_4xmode |=(1 << port_index);
  1480. val_8xmode &=~(1 << port_index);
  1481. quot_coeff = 4;
  1482. printk(KERN_INFO "Using the 4x Mode\n");
  1483. }
  1484. serial_out(up, XR_17V35X_8XMODE, val_8xmode);
  1485. serial_out(up, XR_17V35X_4XMODE, val_4xmode);
  1486. DEBUG_INTR(KERN_INFO "XR_17V35X_4XMODE:%d \n",serial_in(up, XR_17V35X_4XMODE));
  1487. DEBUG_INTR(KERN_INFO "XR_17V35X_8XMODE:%d \n",serial_in(up, XR_17V35X_8XMODE));
  1488. quot = serialxr_get_divisor(port, baud);
  1489. if(!((up->deviceid == 0x152)||(up->deviceid == 0x154)||(up->deviceid == 0x158)))
  1490. {
  1491. DEBUG_INTR(KERN_INFO "XR_17V35X uartclk:%d Quot=0x%x\n",port->uartclk,quot);
  1492. //#ifdef DIVISOR_CHANGED
  1493. if((port->uartclk/baud) > (quot_coeff*quot))
  1494. {
  1495. if(quot_coeff==16) quot_fraction = ( (port->uartclk/baud) - (quot_coeff*quot));
  1496. else if(quot_coeff==8) quot_fraction = ( (port->uartclk/baud) - (quot_coeff*quot))*2;
  1497. else if(quot_coeff==4) quot_fraction = ( (port->uartclk/baud) - (quot_coeff*quot))*4;
  1498. }
  1499. else if(quot > 1)
  1500. {
  1501. quot--;
  1502. if(quot_coeff==16) quot_fraction = ( (port->uartclk/baud) - (quot_coeff*quot));
  1503. else if(quot_coeff==8) quot_fraction = ( (port->uartclk/baud) - (quot_coeff*quot))*2;
  1504. else if(quot_coeff==4) quot_fraction = ( (port->uartclk/baud) - (quot_coeff*quot))*4;
  1505. }
  1506. else
  1507. {
  1508. quot_fraction = 0;
  1509. }
  1510. if(quot_fraction>=0x10) quot_fraction=0x0f;
  1511. }
  1512. //#endif
  1513. /*
  1514. * Ok, we're now changing the port state. Do it with
  1515. * interrupts disabled.
  1516. */
  1517. spin_lock_irqsave(&up->port.lock, flags);
  1518. /*
  1519. * Update the per-port timeout.
  1520. */
  1521. uart_update_timeout(port, termios->c_cflag, baud);
  1522. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_PE | UART_LSR_DR;
  1523. if (termios->c_iflag & INPCK)
  1524. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  1525. if (termios->c_iflag & (BRKINT | PARMRK))
  1526. up->port.read_status_mask |= UART_LSR_BI;
  1527. /*
  1528. * Characteres to ignore
  1529. */
  1530. up->port.ignore_status_mask = 0;
  1531. if (termios->c_iflag & IGNPAR)
  1532. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  1533. if (termios->c_iflag & IGNBRK) {
  1534. up->port.ignore_status_mask |= UART_LSR_BI;
  1535. /*
  1536. * If we're ignoring parity and break indicators,
  1537. * ignore overruns too (for real raw support).
  1538. */
  1539. if (termios->c_iflag & IGNPAR)
  1540. up->port.ignore_status_mask |= UART_LSR_OE;
  1541. }
  1542. /*
  1543. * ignore all characters if CREAD is not set
  1544. */
  1545. if ((termios->c_cflag & CREAD) == 0)
  1546. up->port.ignore_status_mask |= UART_LSR_DR;
  1547. /*
  1548. * CTS flow control flag and modem status interrupts
  1549. */
  1550. up->ier &= ~UART_IER_MSI;
  1551. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  1552. up->ier |= UART_IER_MSI;
  1553. lcr = serial_in(up, UART_LCR);
  1554. if (lcr & 0x80) {
  1555. printk(KERN_INFO"channelnum %d: serialxr_set_termios1 - LCR = 0x%x", up->channelnum, lcr);
  1556. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  1557. }
  1558. serial_out(up, UART_IER, up->ier);
  1559. reg_read=serial_in(up, XR_17V35X_EXTENDED_EFR);
  1560. if(termios->c_cflag & CRTSCTS)
  1561. {
  1562. serial_out(up, XR_17V35X_EXTENDED_EFR, reg_read|0xC0);
  1563. printk(KERN_INFO "Hardware Flow Control Enabled");
  1564. }
  1565. else
  1566. {
  1567. serial_out(up, XR_17V35X_EXTENDED_EFR, reg_read & 0x3F);
  1568. printk(KERN_INFO "Hardware Flow Control Disabled\n");
  1569. }
  1570. /*
  1571. * Auto XON/XOFF software flow control flags
  1572. */
  1573. serial_out(up, XR_17V35X_UART_XON1,0x11); //Initializing XON1
  1574. serial_out(up, XR_17V35X_UART_XOFF1,0x13); //Initializing XOFF1
  1575. if(((termios->c_iflag) & IXOFF)&&((termios->c_iflag) & IXON))
  1576. {
  1577. serial_out(up, XR_17V35X_EXTENDED_EFR, (reg_read) | 0x0A );
  1578. printk(KERN_INFO "Software Flow Control Enabled\n");
  1579. }
  1580. else
  1581. {
  1582. serial_out(up, XR_17V35X_EXTENDED_EFR, (reg_read) & 0xF0 );
  1583. printk(KERN_INFO "No Software Flow Control\n");
  1584. }
  1585. reg_read=serial_in(up, XR_17V35X_EXTENDED_EFR);
  1586. if((termios->c_iflag) & IXANY)
  1587. {
  1588. serial_out(up, XR_17V35X_EXTENDED_EFR, ((termios->c_iflag) & IXOFF)||((termios->c_iflag) & IXON)?((reg_read) | 0x1A):((reg_read) | 0x10));
  1589. reg_read=serial_in(up, UART_MCR);
  1590. serial_out(up, UART_MCR, (reg_read) | 0x20 );
  1591. serial_out(up, XR_17V35X_EXTENDED_EFR, (reg_read) & 0xEF );
  1592. printk(KERN_INFO "AUTO XANY Enabled\n");
  1593. }
  1594. else
  1595. {
  1596. serial_out(up, XR_17V35X_EXTENDED_EFR, (reg_read) | 0x10 );
  1597. reg_read=serial_in(up, UART_MCR);
  1598. serial_out(up, UART_MCR, (reg_read) & 0xDF );
  1599. reg_read=serial_in(up,XR_17V35X_EXTENDED_EFR);
  1600. serial_out(up, XR_17V35X_EXTENDED_EFR, (reg_read) & 0xEF );
  1601. printk(KERN_INFO "AUTO XANY NOT Enabled\n");
  1602. }
  1603. //---------------------------------------------------------------------------//
  1604. serial_out(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
  1605. serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
  1606. serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
  1607. //Fractional baud rate support
  1608. if((up->deviceid == 0x152)||(up->deviceid == 0x154)||(up->deviceid == 0x158))
  1609. {
  1610. //nothing to do , because these devices do not have support for the DLD register.
  1611. }
  1612. else
  1613. {
  1614. reg_read=(serial_in(up, XR_17V35X_UART_DLD)&0xF0);
  1615. DEBUG_INTR(KERN_INFO "serialxr_set_termios: quot =0x%x quot_fraction=0x%x DLD_reg=0x%x\n",quot,quot_fraction,reg_read);
  1616. serial_out(up, XR_17V35X_UART_DLD, quot_fraction | reg_read);
  1617. reg_read=serial_in(up, XR_17V35X_UART_DLD);
  1618. }
  1619. serial_out(up, UART_LCR, cval); /* reset DLAB */
  1620. up->lcr = cval; /* Save LCR */
  1621. lcr = serial_in(up, UART_LCR);
  1622. if (lcr & 0x80) {
  1623. printk(KERN_INFO"channelnum %d: serialxr_set_termios2 - LCR = 0x%x", up->channelnum, lcr);
  1624. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  1625. }
  1626. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);/* set fcr */
  1627. /*
  1628. Configuring MPIO as inputs
  1629. */
  1630. if((up->deviceid == 0x354)||(up->deviceid == 0x4354)||(up->deviceid == 0x8354))
  1631. {
  1632. serial_out(up, XR_17V35x_MPIOSEL_7_0,0x0FF); //0x0ff= ALL INPUTS
  1633. }
  1634. else if((up->deviceid == 0x358)||(up->deviceid == 0x4358)||(up->deviceid == 0x8358))
  1635. {
  1636. serial_out(up, XR_17V35x_MPIOSEL_7_0,0x0FF); //0x0ff= ALL INPUTS
  1637. serial_out(up, XR_17V35x_MPIOSEL_15_8,0x0FF); //0x0ff= ALL INPUTS
  1638. }
  1639. serialxr_set_mctrl(&up->port, up->port.mctrl);
  1640. #if ENABLE_INTERNAL_LOOPBACK
  1641. reg_read=serial_in(up, UART_MCR);
  1642. serial_out(up, UART_MCR, (reg_read) | 0x10);
  1643. printk(KERN_INFO "Enabling Internal Loopback\n");
  1644. #endif
  1645. spin_unlock_irqrestore(&up->port.lock, flags);
  1646. }
  1647. /*
  1648. * EXAR ioctls
  1649. */
  1650. //#define FIOQSIZE 0x5460
  1651. #define EXAR_READ_REG (FIOQSIZE + 1)
  1652. #define EXAR_WRITE_REG (FIOQSIZE + 2)
  1653. #define EXAR_SET_MULTIDROP_MODE_NORMAL (FIOQSIZE + 3)
  1654. #define EXAR_SET_MULTIDROP_MODE_AUTO (FIOQSIZE + 4)
  1655. #define EXAR_SET_REMOVE_MULTIDROP_MODE (FIOQSIZE + 5)
  1656. #define EXAR_SET_NON_STANDARD_BAUDRATE (FIOQSIZE + 6)
  1657. struct xrioctl_rw_reg {
  1658. unsigned char reg;
  1659. unsigned char regvalue;
  1660. };
  1661. /*
  1662. * This function is used to handle Exar Device specific ioctl calls
  1663. * The user level application should have defined the above ioctl
  1664. * commands with the above values to access these ioctls and the
  1665. * input parameters for these ioctls should be struct xrioctl_rw_reg
  1666. * The Ioctl functioning is pretty much self explanatory here in the code,
  1667. * and the register values should be between 0 to XR_17V35X_EXTENDED_RXTRG
  1668. */
  1669. static int
  1670. serialxr_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
  1671. {
  1672. struct uart_xr_port *up = (struct uart_xr_port *)port;
  1673. int ret = -ENOIOCTLCMD;
  1674. struct xrioctl_rw_reg ioctlrwarg;
  1675. unsigned char address;
  1676. unsigned char tmp,lcr_bak,dld,efr;
  1677. unsigned int any_baudrate = 0;
  1678. switch (cmd)
  1679. {
  1680. case EXAR_READ_REG:
  1681. if (copy_from_user(&ioctlrwarg, (void *)arg, sizeof(ioctlrwarg)))
  1682. return -EFAULT;
  1683. ioctlrwarg.regvalue = serial_in(up, ioctlrwarg.reg);
  1684. if (copy_to_user((void *)arg, &ioctlrwarg, sizeof(ioctlrwarg)))
  1685. return -EFAULT;
  1686. DEBUG_INTR(KERN_INFO "serialxr_ioctl read reg[0x%02x]=0x%02x \n",ioctlrwarg.reg,ioctlrwarg.regvalue);
  1687. ret = 0;
  1688. break;
  1689. case EXAR_WRITE_REG:
  1690. if (copy_from_user(&ioctlrwarg, (void *)arg, sizeof(ioctlrwarg)))
  1691. return -EFAULT;
  1692. serial_out(up, ioctlrwarg.reg, ioctlrwarg.regvalue);
  1693. DEBUG_INTR(KERN_INFO "serialxr_ioctl write reg[0x%02x]=0x%02x \n",ioctlrwarg.reg,ioctlrwarg.regvalue);
  1694. ret = 0;
  1695. break;
  1696. case EXAR_SET_MULTIDROP_MODE_NORMAL:
  1697. if (copy_from_user(&address, (void *)arg, 1))
  1698. return -EFAULT;
  1699. up->multidrop_address = address;
  1700. //set EFR[4] = 1; enable the shaded bits
  1701. efr = serial_in(up, XR_17V35X_EXTENDED_EFR);
  1702. efr |=0x10;
  1703. serial_out(up, XR_17V35X_EXTENDED_EFR, efr);
  1704. serial_out(up,XR_17V35X_UART_MSR, 0x04);//Disable the receiver with mode=0
  1705. //set EFR[4] =0; disable the shaded bits
  1706. efr = serial_in(up, XR_17V35X_EXTENDED_EFR);
  1707. efr &=~0x10;
  1708. serial_out(up, XR_17V35X_EXTENDED_EFR, efr);
  1709. lcr_bak = serial_in(up, UART_LCR);
  1710. tmp = 0x80 | lcr_bak | 0x38; //LCR[7]=1 for access DLD LCR[5:3] = '111' for Forced parity to space "0"
  1711. serial_out(up, UART_LCR, tmp);
  1712. //set the DLD[6] = 1 enable Multidrop mode
  1713. dld = serial_in(up, XR_17V35X_UART_DLD);
  1714. dld |= 0x40;
  1715. serial_out(up, XR_17V35X_UART_DLD, dld);
  1716. //set EFR[5] = 0; disable the special char Select
  1717. efr = serial_in(up, XR_17V35X_EXTENDED_EFR);
  1718. efr &=~0x20;
  1719. serial_out(up, XR_17V35X_EXTENDED_EFR, efr);
  1720. lcr_bak = serial_in(up, UART_LCR);//set LCR[7]=0
  1721. lcr_bak &=~0x80;
  1722. serial_out(up, UART_LCR, lcr_bak);
  1723. ret = 0;
  1724. up->multidrop_mode = 1;//for enable multidrop normal mode
  1725. up->is_match_address = 0;
  1726. DEBUG_INTR(KERN_INFO "User request EXAR_SET_MULTIDROP_MODE_NORMAL addr:%d \n",up->multidrop_address);
  1727. break;
  1728. case EXAR_SET_MULTIDROP_MODE_AUTO:
  1729. if (copy_from_user(&address, (void *)arg, 1))
  1730. return -EFAULT;
  1731. up->multidrop_address = address;
  1732. serial_out(up, XR_17V35X_UART_XOFF2,address);
  1733. //set EFR[4] = 1; enable the shaded bits
  1734. efr = serial_in(up, XR_17V35X_EXTENDED_EFR);
  1735. efr |=0x10;
  1736. serial_out(up, XR_17V35X_EXTENDED_EFR, efr);
  1737. serial_out(up,XR_17V35X_UART_MSR, 0x04);//Disable the receiver with mode=0
  1738. //set EFR[4] =0; disable the shaded bits
  1739. efr = serial_in(up, XR_17V35X_EXTENDED_EFR);
  1740. efr &=~0x10;
  1741. serial_out(up, XR_17V35X_EXTENDED_EFR, efr);
  1742. lcr_bak = serial_in(up, UART_LCR);
  1743. tmp = 0x80 | lcr_bak | 0x38; //LCR[7]=1 for access DLD LCR[5:3] = '111' for Forced parity to space "0"
  1744. serial_out(up, UART_LCR, tmp);
  1745. //set the DLD[6] = 1 enable Multidrop mode
  1746. dld = serial_in(up, XR_17V35X_UART_DLD);
  1747. dld |= 0x40;
  1748. serial_out(up, XR_17V35X_UART_DLD, dld);
  1749. //set EFR[5] = 1; enable the special char Select
  1750. efr = serial_in(up, XR_17V35X_EXTENDED_EFR);
  1751. efr |=0x20;
  1752. serial_out(up, XR_17V35X_EXTENDED_EFR, efr);
  1753. //printk(KERN_INFO "UART_EFR=0x%02x\n",serial_in(up, XR_17V35X_EXTENDED_EFR));
  1754. up->multidrop_mode = 2;//for enable multidrop auto mode
  1755. lcr_bak = serial_in(up, UART_LCR);//set LCR[7]=0
  1756. lcr_bak &=~0x80;
  1757. serial_out(up, UART_LCR, lcr_bak);
  1758. DEBUG_INTR(KERN_INFO "User request EXAR_SET_MULTIDROP_MODE_AUTO addr:%d \n",up->multidrop_address);
  1759. ret = 0;
  1760. break;
  1761. case EXAR_SET_REMOVE_MULTIDROP_MODE:
  1762. //set the DLD[6] = 0 disable Multidrop mode
  1763. lcr_bak = serial_in(up, UART_LCR);
  1764. tmp = 0x80 | lcr_bak; //LCR[7]=1 for access DLD
  1765. dld = serial_in(up, XR_17V35X_UART_DLD);
  1766. dld &=~0x40;//Disable Multidrop mode
  1767. serial_out(up, XR_17V35X_UART_DLD, tmp);
  1768. efr = serial_in(up, XR_17V35X_EXTENDED_EFR);
  1769. efr &=~0x20;//disable the special char select
  1770. efr |= 0x10; //enable the shaded bits
  1771. serial_out(up, XR_17V35X_EXTENDED_EFR, efr);
  1772. serial_out(up,XR_17V35X_UART_MSR, 0x00);//Enable the receiver with mode=0
  1773. //set EFR[4] =0; disable the shaded bits
  1774. efr = serial_in(up, XR_17V35X_EXTENDED_EFR);
  1775. efr &=~0x10;
  1776. serial_out(up, XR_17V35X_EXTENDED_EFR, efr);
  1777. lcr_bak &=~0x38;//LCR[5:3] = '000'
  1778. lcr_bak &=~0x80;//Set LCR[7] = 0
  1779. serial_out(up, UART_LCR, lcr_bak);
  1780. up->multidrop_mode = 0;
  1781. up->is_match_address = 0;
  1782. ret = 0;
  1783. break;
  1784. case EXAR_SET_NON_STANDARD_BAUDRATE:
  1785. if (copy_from_user(&any_baudrate, (void *)arg, sizeof(unsigned int)))
  1786. {
  1787. return -EFAULT;
  1788. }
  1789. serialxr_set_special_baudrate(port,any_baudrate);
  1790. break;
  1791. }
  1792. return ret;
  1793. }
  1794. static void
  1795. serialxr_pm(struct uart_port *port, unsigned int state,
  1796. unsigned int oldstate)
  1797. {
  1798. int lcr;
  1799. struct uart_xr_port *up = (struct uart_xr_port *)port;
  1800. if (state) {
  1801. /* sleep */
  1802. serial_out(up, XR_17V35X_EXTENDED_EFR, UART_EFR_ECB);
  1803. lcr = serial_in(up, UART_LCR);
  1804. if (lcr & 0x80) {
  1805. printk(KERN_INFO"channelnum %d: serialxr_pm sleep - LCR = 0x%x", up->channelnum, lcr);
  1806. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  1807. }
  1808. serial_out(up, UART_IER, UART_IERX_SLEEP);
  1809. serial_out(up, XR_17V35X_EXTENDED_EFR, 0);
  1810. } else {
  1811. /* wake */
  1812. serial_out(up, XR_17V35X_EXTENDED_EFR, UART_EFR_ECB);
  1813. lcr = serial_in(up, UART_LCR);
  1814. if (lcr & 0x80) {
  1815. printk(KERN_INFO"channelnum %d: serialxr_pm wake - LCR = 0x%x", up->channelnum, lcr);
  1816. serial_out(up, UART_LCR, lcr & 0x7f); // Set LCR bit-7=0 when accessing RHR/THR/IER/ISR to avoid incorrect register access
  1817. }
  1818. serial_out(up, UART_IER, 0);
  1819. serial_out(up, XR_17V35X_EXTENDED_EFR, 0);
  1820. }
  1821. if (up->pm)
  1822. up->pm(port, state, oldstate);
  1823. }
  1824. static void serialxr_release_port(struct uart_port *port)
  1825. {
  1826. }
  1827. static int serialxr_request_port(struct uart_port *port)
  1828. {
  1829. return 0;
  1830. }
  1831. static void serialxr_config_port(struct uart_port *port, int flags)
  1832. {
  1833. struct uart_xr_port *up = (struct uart_xr_port *)port;
  1834. if (flags & UART_CONFIG_TYPE)
  1835. {
  1836. if(up->deviceid > 0x258) // PCIe device
  1837. {
  1838. up->port.type = XRPCIe_TYPE;
  1839. }
  1840. else
  1841. {
  1842. up->port.type = XRPCI25x_TYPE;
  1843. }
  1844. up->port.fifosize = uart_config[up->port.type].dfl_xmit_fifo_size;
  1845. up->capabilities = uart_config[up->port.type].flags;
  1846. }
  1847. }
  1848. static const char *
  1849. serialxr_type(struct uart_port *port)
  1850. {
  1851. int type = port->type;
  1852. if (type >= ARRAY_SIZE(uart_config))
  1853. type = 0;
  1854. return uart_config[type].name;
  1855. }
  1856. static struct uart_ops serialxr_pops = {
  1857. .tx_empty = serialxr_tx_empty,
  1858. .set_mctrl = serialxr_set_mctrl,
  1859. .get_mctrl = serialxr_get_mctrl,
  1860. .stop_tx = serialxr_stop_tx,
  1861. .start_tx = serialxr_start_tx,
  1862. .stop_rx = serialxr_stop_rx,
  1863. .enable_ms = serialxr_enable_ms,
  1864. .break_ctl = serialxr_break_ctl,
  1865. .startup = serialxr_startup,
  1866. .shutdown = serialxr_shutdown,
  1867. .set_termios = serialxr_set_termios,
  1868. .pm = serialxr_pm,
  1869. .type = serialxr_type,
  1870. .release_port = serialxr_release_port,
  1871. .request_port = serialxr_request_port,
  1872. .config_port = serialxr_config_port,
  1873. .ioctl = serialxr_ioctl,
  1874. };
  1875. static DEFINE_MUTEX(serial_mutex);
  1876. static struct uart_xr_port serialxr_ports[NR_PORTS];
  1877. #define SERIALXR_CONSOLE NULL
  1878. static struct uart_driver xr_uart_driver = {
  1879. .owner = THIS_MODULE,
  1880. .driver_name = "xrserial",
  1881. .dev_name = "ttyXR",
  1882. .major = XR_MAJOR,
  1883. .minor = XR_MINOR,
  1884. .nr = NR_PORTS,
  1885. .cons = SERIALXR_CONSOLE,
  1886. };
  1887. static struct uart_xr_port *serialxr_find_match_or_unused(struct uart_port *port)
  1888. {
  1889. int i;
  1890. /*
  1891. * First, find a port entry which matches.
  1892. */
  1893. for (i = 0; i < NR_PORTS; i++)
  1894. if (uart_match_port(&serialxr_ports[i].port, port))
  1895. return &serialxr_ports[i];
  1896. /*
  1897. * We didn't find a matching entry, so look for the first
  1898. * free entry. We look for one which hasn't been previously
  1899. * used (indicated by zero iobase).
  1900. */
  1901. for (i = 0; i < NR_PORTS; i++)
  1902. if (serialxr_ports[i].port.type == PORT_UNKNOWN &&
  1903. serialxr_ports[i].port.iobase == 0)
  1904. {
  1905. port->line = i;
  1906. return &serialxr_ports[i];
  1907. }
  1908. /*
  1909. * That also failed. Last resort is to find any entry which
  1910. * doesn't have a real port associated with it.
  1911. */
  1912. for (i = 0; i < NR_PORTS; i++)
  1913. if (serialxr_ports[i].port.type == PORT_UNKNOWN)
  1914. return &serialxr_ports[i];
  1915. return NULL;
  1916. }
  1917. /*
  1918. * serialxr_register_port - register a serial port
  1919. * @port: serial port template
  1920. *
  1921. * Configure the serial port specified by the request. If the
  1922. * port exists and is in use, it is hung up and unregistered
  1923. * first.
  1924. *
  1925. * The port is then probed and if necessary the IRQ is autodetected
  1926. * If this fails an error is returned.
  1927. *
  1928. * On success the port is ready to use and the line number is returned.
  1929. */
  1930. int serialxr_register_port(struct uart_port *port, unsigned short deviceid, unsigned char channelnum)
  1931. {
  1932. struct uart_xr_port *uart;
  1933. int ret = -ENOSPC;
  1934. if (port->uartclk == 0)
  1935. return -EINVAL;
  1936. mutex_lock(&serial_mutex);
  1937. uart = serialxr_find_match_or_unused(port);
  1938. if (uart) {
  1939. uart->port.iobase = port->iobase;
  1940. uart->port.membase = port->membase;
  1941. uart->port.irq = port->irq;
  1942. uart->port.uartclk = port->uartclk;
  1943. uart->port.fifosize = port->fifosize;
  1944. uart->port.regshift = port->regshift;
  1945. uart->port.iotype = port->iotype;
  1946. uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
  1947. uart->port.mapbase = port->mapbase;
  1948. if (port->dev)
  1949. uart->port.dev = port->dev;
  1950. uart->deviceid = deviceid;
  1951. uart->channelnum = channelnum;
  1952. uart->port.line = port->line;
  1953. spin_lock_init(&uart->port.lock);
  1954. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
  1955. timer_setup(&uart->timer, serialxr_timeout, 0);
  1956. #else
  1957. init_timer(&uart->timer);
  1958. uart->timer.function = serialxr_timeout;
  1959. #endif
  1960. /*
  1961. * ALPHA_KLUDGE_MCR needs to be killed.
  1962. */
  1963. uart->mcr_mask = ~(0x0); //~ALPHA_KLUDGE_MCR;
  1964. uart->mcr_force = 0; // ALPHA_KLUDGE_MCR;
  1965. uart->port.ops = &serialxr_pops;
  1966. ret = uart_add_one_port(&xr_uart_driver, &uart->port);
  1967. #if 0
  1968. if (ret == 0)
  1969. {
  1970. ret = uart->port.line;
  1971. if (is_real_interrupt(uart->port.irq)) {
  1972. serial_link_irq_chain(uart);
  1973. }
  1974. }
  1975. #endif
  1976. }
  1977. mutex_unlock(&serial_mutex);
  1978. return ret;
  1979. }
  1980. /*
  1981. * Probe one serial board. Unfortunately, there is no rhyme nor reason
  1982. * to the arrangement of serial ports on a PCI card.
  1983. */
  1984. static int __devinit
  1985. init_one_xrpciserialcard(struct pci_dev *dev, const struct pci_device_id *ent)
  1986. {
  1987. struct serial_private *priv;
  1988. struct pciserial_board *board;
  1989. struct pci_serial_quirk *quirk;
  1990. struct uart_port serial_port;
  1991. int rc, nr_ports, i;
  1992. if (ent->driver_data >= ARRAY_SIZE(xrpciserial_boards)) {
  1993. printk(KERN_INFO "pci_init_one: invalid driver_data: %ld\n",
  1994. ent->driver_data);
  1995. return -EINVAL;
  1996. }
  1997. board = &xrpciserial_boards[ent->driver_data];
  1998. rc = pci_enable_device(dev);
  1999. if (rc)
  2000. return rc;
  2001. nr_ports = board->num_ports;
  2002. /*
  2003. * Find an init and setup quirks.
  2004. */
  2005. quirk = find_quirk(dev);
  2006. /*
  2007. * Run the new-style initialization function.
  2008. * The initialization function returns:
  2009. * <0 - error
  2010. * 0 - use board->num_ports
  2011. * >0 - number of ports
  2012. */
  2013. if (quirk->init) {
  2014. rc = quirk->init(dev);
  2015. if (rc < 0)
  2016. goto disable;
  2017. if (rc)
  2018. nr_ports = rc;
  2019. }
  2020. priv = kmalloc(sizeof(struct serial_private) +
  2021. sizeof(unsigned int) * nr_ports,
  2022. GFP_KERNEL);
  2023. if (!priv) {
  2024. rc = -ENOMEM;
  2025. goto deinit;
  2026. }
  2027. memset(priv, 0, sizeof(struct serial_private) +
  2028. sizeof(unsigned int) * nr_ports);
  2029. priv->dev = dev;
  2030. priv->quirk = quirk;
  2031. memset(&serial_port, 0, sizeof(struct uart_port));
  2032. serial_port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ;
  2033. if((priv->dev->device == 0x152) ||(priv->dev->device == 0x154)||(priv->dev->device == 0x158))
  2034. serial_port.uartclk = board->base_baud * 16;
  2035. else
  2036. serial_port.uartclk = board->base_baud * 4;
  2037. serial_port.irq = dev->irq;
  2038. serial_port.dev = &dev->dev;
  2039. for (i = 0; i < nr_ports; i++) {
  2040. if (quirk->setup(priv, board, &serial_port, i))
  2041. break;
  2042. // setup the uartclock for the devices on expansion slot
  2043. switch(priv->dev->device)
  2044. {
  2045. case 0x4354:
  2046. case 0x8354:
  2047. if(i >= 4)
  2048. serial_port.uartclk = 62500000; // half the clock speed of the main chip (which is 125MHz)
  2049. break;
  2050. case 0x4358:
  2051. case 0x8358:
  2052. if(i >= 8) // epansions slot ports
  2053. serial_port.uartclk = 62500000; // half the clock speed of the main chip (which is 125MHz)
  2054. break;
  2055. default: //0x358/354/352/258/254/252
  2056. break;
  2057. }
  2058. rc = serialxr_register_port(&serial_port, dev->device,i);
  2059. if (rc < 0) {
  2060. printk(KERN_WARNING "Couldn't register serial port %s: %d\n", pci_name(dev), i);
  2061. break;
  2062. }
  2063. printk(KERN_WARNING "init_one_xrpciserialcard line:%d\n",serial_port.line);
  2064. priv->uart_index[i] = serial_port.line;
  2065. priv->line[i] = rc;
  2066. }
  2067. priv->nr = i;
  2068. if (!IS_ERR(priv)) {
  2069. pci_set_drvdata(dev, priv);
  2070. return 0;
  2071. }
  2072. deinit:
  2073. if (quirk->exit)
  2074. quirk->exit(dev);
  2075. disable:
  2076. pci_disable_device(dev);
  2077. return rc;
  2078. }
  2079. /*
  2080. * serialxr_unregister_port - remove a serial port at runtime
  2081. * @line: serial line number
  2082. *
  2083. * Remove one serial port. This may not be called from interrupt
  2084. * context. We hand the port back to the our control.
  2085. */
  2086. void serialxr_unregister_port(int line)
  2087. {
  2088. struct uart_xr_port *uart = &serialxr_ports[line];
  2089. mutex_lock(&serial_mutex);
  2090. #if 0
  2091. if (is_real_interrupt(uart->port.irq))
  2092. serial_unlink_irq_chain(uart);
  2093. #endif
  2094. uart_remove_one_port(&xr_uart_driver, &uart->port);
  2095. uart->port.dev = NULL;
  2096. mutex_unlock(&serial_mutex);
  2097. }
  2098. void pciserial_remove_ports(struct serial_private *priv)
  2099. {
  2100. struct pci_serial_quirk *quirk;
  2101. int i;
  2102. for (i = 0; i < priv->nr; i++)
  2103. {
  2104. printk(KERN_WARNING "pciserial_remove_ports dev:%p port_num:%d\n",priv->dev,priv->uart_index[i]);
  2105. //serialxr_unregister_port(priv->line[i]);
  2106. serialxr_unregister_port(priv->uart_index[i]);
  2107. }
  2108. for (i = 0; i < PCI_NUM_BAR_RESOURCES; i++) {
  2109. if (priv->remapped_bar[i])
  2110. iounmap(priv->remapped_bar[i]);
  2111. priv->remapped_bar[i] = NULL;
  2112. }
  2113. /*
  2114. * Find the exit quirks.
  2115. */
  2116. quirk = find_quirk(priv->dev);
  2117. if (quirk->exit)
  2118. quirk->exit(priv->dev);
  2119. kfree(priv);
  2120. }
  2121. static void __devexit remove_one_xrpciserialcard(struct pci_dev *dev)
  2122. {
  2123. struct serial_private *priv = pci_get_drvdata(dev);
  2124. pci_set_drvdata(dev, NULL);
  2125. pciserial_remove_ports(priv);
  2126. pci_disable_device(dev);
  2127. }
  2128. static struct pci_device_id xrserial_pci_tbl[] = {
  2129. { 0x13a8, 0x358,
  2130. PCI_ANY_ID, PCI_ANY_ID,
  2131. 0, 0, xr_8port },
  2132. { 0x13a8, 0x354,
  2133. PCI_ANY_ID, PCI_ANY_ID,
  2134. 0, 0, xr_4port },
  2135. { 0x13a8, 0x352,
  2136. PCI_ANY_ID, PCI_ANY_ID,
  2137. 0, 0, xr_2port },
  2138. { 0x13a8, 0x4354,
  2139. PCI_ANY_ID, PCI_ANY_ID,
  2140. 0, 0, xr_4354port },
  2141. { 0x13a8, 0x8354,
  2142. PCI_ANY_ID, PCI_ANY_ID,
  2143. 0, 0, xr_8354port },
  2144. { 0x13a8, 0x4358,
  2145. PCI_ANY_ID, PCI_ANY_ID,
  2146. 0, 0, xr_4358port },
  2147. { 0x13a8, 0x8358,
  2148. PCI_ANY_ID, PCI_ANY_ID,
  2149. 0, 0, xr_8358port },
  2150. { 0x13a8, 0x258,
  2151. PCI_ANY_ID, PCI_ANY_ID,
  2152. 0, 0, xr_258port },
  2153. { 0x13a8, 0x254,
  2154. PCI_ANY_ID, PCI_ANY_ID,
  2155. 0, 0, xr_254port },
  2156. { 0x13a8, 0x252,
  2157. PCI_ANY_ID, PCI_ANY_ID,
  2158. 0, 0, xr_252port },
  2159. { 0x13a8, 0x158,
  2160. PCI_ANY_ID, PCI_ANY_ID,
  2161. 0, 0, xr_158port },
  2162. { 0x13a8, 0x154,
  2163. PCI_ANY_ID, PCI_ANY_ID,
  2164. 0, 0, xr_154port },
  2165. { 0x13a8, 0x152,
  2166. PCI_ANY_ID, PCI_ANY_ID,
  2167. 0, 0, xr_152port },
  2168. { 0, }
  2169. };
  2170. static struct pci_driver xrserial_pci_driver = {
  2171. .name = "xrserial",
  2172. .probe = init_one_xrpciserialcard,
  2173. .remove = __devexit_p(remove_one_xrpciserialcard),
  2174. .id_table = xrserial_pci_tbl,
  2175. };
  2176. static int __init serialxr_init(void)
  2177. {
  2178. int ret;
  2179. printk(KERN_INFO "Exar PCIe (XR17V35x) serial driver Revision: 2.6\n");
  2180. ret = uart_register_driver(&xr_uart_driver);
  2181. if (ret)
  2182. return ret;
  2183. ret = pci_register_driver(&xrserial_pci_driver);
  2184. if (ret < 0)
  2185. uart_unregister_driver(&xr_uart_driver);
  2186. return ret;
  2187. }
  2188. static void __exit serialxr_exit(void)
  2189. {
  2190. pci_unregister_driver(&xrserial_pci_driver);
  2191. uart_unregister_driver(&xr_uart_driver);
  2192. }
  2193. module_init(serialxr_init);
  2194. module_exit(serialxr_exit);
  2195. MODULE_LICENSE("GPL");
  2196. MODULE_DESCRIPTION("Exar PCIe specific serial driver for XR17V35x- Revision: 2.6");