ioc3_serial.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2005 Silicon Graphics, Inc. All Rights Reserved.
  4. */
  5. /*
  6. * This file contains a module version of the ioc3 serial driver. This
  7. * includes all the support functions needed (support functions, etc.)
  8. * and the serial driver itself.
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/tty.h>
  12. #include <linux/tty_flip.h>
  13. #include <linux/serial.h>
  14. #include <linux/circ_buf.h>
  15. #include <linux/serial_reg.h>
  16. #include <linux/module.h>
  17. #include <linux/pci.h>
  18. #include <linux/serial_core.h>
  19. #include <linux/ioc3.h>
  20. #include <linux/slab.h>
  21. /*
  22. * Interesting things about the ioc3
  23. */
  24. #define LOGICAL_PORTS 2 /* rs232(0) and rs422(1) */
  25. #define PORTS_PER_CARD 2
  26. #define LOGICAL_PORTS_PER_CARD (PORTS_PER_CARD * LOGICAL_PORTS)
  27. #define MAX_CARDS 8
  28. #define MAX_LOGICAL_PORTS (LOGICAL_PORTS_PER_CARD * MAX_CARDS)
  29. /* determine given the sio_ir what port it applies to */
  30. #define GET_PORT_FROM_SIO_IR(_x) (_x & SIO_IR_SA) ? 0 : 1
  31. /*
  32. * we have 2 logical ports (rs232, rs422) for each physical port
  33. * evens are rs232, odds are rs422
  34. */
  35. #define GET_PHYSICAL_PORT(_x) ((_x) >> 1)
  36. #define GET_LOGICAL_PORT(_x) ((_x) & 1)
  37. #define IS_PHYSICAL_PORT(_x) !((_x) & 1)
  38. #define IS_RS232(_x) !((_x) & 1)
  39. static unsigned int Num_of_ioc3_cards;
  40. static unsigned int Submodule_slot;
  41. /* defining this will get you LOTS of great debug info */
  42. //#define DEBUG_INTERRUPTS
  43. #define DPRINT_CONFIG(_x...) ;
  44. //#define DPRINT_CONFIG(_x...) printk _x
  45. #define NOT_PROGRESS() ;
  46. //#define NOT_PROGRESS() printk("%s : fails %d\n", __func__, __LINE__)
  47. /* number of characters we want to transmit to the lower level at a time */
  48. #define MAX_CHARS 256
  49. #define FIFO_SIZE (MAX_CHARS-1) /* it's a uchar */
  50. /* Device name we're using */
  51. #define DEVICE_NAME "ttySIOC"
  52. #define DEVICE_MAJOR 204
  53. #define DEVICE_MINOR 116
  54. /* flags for next_char_state */
  55. #define NCS_BREAK 0x1
  56. #define NCS_PARITY 0x2
  57. #define NCS_FRAMING 0x4
  58. #define NCS_OVERRUN 0x8
  59. /* cause we need SOME parameters ... */
  60. #define MIN_BAUD_SUPPORTED 1200
  61. #define MAX_BAUD_SUPPORTED 115200
  62. /* protocol types supported */
  63. #define PROTO_RS232 0
  64. #define PROTO_RS422 1
  65. /* Notification types */
  66. #define N_DATA_READY 0x01
  67. #define N_OUTPUT_LOWAT 0x02
  68. #define N_BREAK 0x04
  69. #define N_PARITY_ERROR 0x08
  70. #define N_FRAMING_ERROR 0x10
  71. #define N_OVERRUN_ERROR 0x20
  72. #define N_DDCD 0x40
  73. #define N_DCTS 0x80
  74. #define N_ALL_INPUT (N_DATA_READY | N_BREAK \
  75. | N_PARITY_ERROR | N_FRAMING_ERROR \
  76. | N_OVERRUN_ERROR | N_DDCD | N_DCTS)
  77. #define N_ALL_OUTPUT N_OUTPUT_LOWAT
  78. #define N_ALL_ERRORS (N_PARITY_ERROR | N_FRAMING_ERROR \
  79. | N_OVERRUN_ERROR)
  80. #define N_ALL (N_DATA_READY | N_OUTPUT_LOWAT | N_BREAK \
  81. | N_PARITY_ERROR | N_FRAMING_ERROR \
  82. | N_OVERRUN_ERROR | N_DDCD | N_DCTS)
  83. #define SER_CLK_SPEED(prediv) ((22000000 << 1) / prediv)
  84. #define SER_DIVISOR(x, clk) (((clk) + (x) * 8) / ((x) * 16))
  85. #define DIVISOR_TO_BAUD(div, clk) ((clk) / 16 / (div))
  86. /* Some masks */
  87. #define LCR_MASK_BITS_CHAR (UART_LCR_WLEN5 | UART_LCR_WLEN6 \
  88. | UART_LCR_WLEN7 | UART_LCR_WLEN8)
  89. #define LCR_MASK_STOP_BITS (UART_LCR_STOP)
  90. #define PENDING(_a, _p) (readl(&(_p)->vma->sio_ir) & (_a)->ic_enable)
  91. #define RING_BUF_SIZE 4096
  92. #define BUF_SIZE_BIT SBBR_L_SIZE
  93. #define PROD_CONS_MASK PROD_CONS_PTR_4K
  94. #define TOTAL_RING_BUF_SIZE (RING_BUF_SIZE * 4)
  95. /* driver specific - one per card */
  96. struct ioc3_card {
  97. struct {
  98. /* uart ports are allocated here */
  99. struct uart_port icp_uart_port[LOGICAL_PORTS];
  100. /* the ioc3_port used for this port */
  101. struct ioc3_port *icp_port;
  102. } ic_port[PORTS_PER_CARD];
  103. /* currently enabled interrupts */
  104. uint32_t ic_enable;
  105. };
  106. /* Local port info for each IOC3 serial port */
  107. struct ioc3_port {
  108. /* handy reference material */
  109. struct uart_port *ip_port;
  110. struct ioc3_card *ip_card;
  111. struct ioc3_driver_data *ip_idd;
  112. struct ioc3_submodule *ip_is;
  113. /* pci mem addresses for this port */
  114. struct ioc3_serialregs __iomem *ip_serial_regs;
  115. struct ioc3_uartregs __iomem *ip_uart_regs;
  116. /* Ring buffer page for this port */
  117. dma_addr_t ip_dma_ringbuf;
  118. /* vaddr of ring buffer */
  119. struct ring_buffer *ip_cpu_ringbuf;
  120. /* Rings for this port */
  121. struct ring *ip_inring;
  122. struct ring *ip_outring;
  123. /* Hook to port specific values */
  124. struct port_hooks *ip_hooks;
  125. spinlock_t ip_lock;
  126. /* Various rx/tx parameters */
  127. int ip_baud;
  128. int ip_tx_lowat;
  129. int ip_rx_timeout;
  130. /* Copy of notification bits */
  131. int ip_notify;
  132. /* Shadow copies of various registers so we don't need to PIO
  133. * read them constantly
  134. */
  135. uint32_t ip_sscr;
  136. uint32_t ip_tx_prod;
  137. uint32_t ip_rx_cons;
  138. unsigned char ip_flags;
  139. };
  140. /* tx low water mark. We need to notify the driver whenever tx is getting
  141. * close to empty so it can refill the tx buffer and keep things going.
  142. * Let's assume that if we interrupt 1 ms before the tx goes idle, we'll
  143. * have no trouble getting in more chars in time (I certainly hope so).
  144. */
  145. #define TX_LOWAT_LATENCY 1000
  146. #define TX_LOWAT_HZ (1000000 / TX_LOWAT_LATENCY)
  147. #define TX_LOWAT_CHARS(baud) (baud / 10 / TX_LOWAT_HZ)
  148. /* Flags per port */
  149. #define INPUT_HIGH 0x01
  150. /* used to signify that we have turned off the rx_high
  151. * temporarily - we need to drain the fifo and don't
  152. * want to get blasted with interrupts.
  153. */
  154. #define DCD_ON 0x02
  155. /* DCD state is on */
  156. #define LOWAT_WRITTEN 0x04
  157. #define READ_ABORTED 0x08
  158. /* the read was aborted - used to avaoid infinate looping
  159. * in the interrupt handler
  160. */
  161. #define INPUT_ENABLE 0x10
  162. /* Since each port has different register offsets and bitmasks
  163. * for everything, we'll store those that we need in tables so we
  164. * don't have to be constantly checking the port we are dealing with.
  165. */
  166. struct port_hooks {
  167. uint32_t intr_delta_dcd;
  168. uint32_t intr_delta_cts;
  169. uint32_t intr_tx_mt;
  170. uint32_t intr_rx_timer;
  171. uint32_t intr_rx_high;
  172. uint32_t intr_tx_explicit;
  173. uint32_t intr_clear;
  174. uint32_t intr_all;
  175. char rs422_select_pin;
  176. };
  177. static struct port_hooks hooks_array[PORTS_PER_CARD] = {
  178. /* values for port A */
  179. {
  180. .intr_delta_dcd = SIO_IR_SA_DELTA_DCD,
  181. .intr_delta_cts = SIO_IR_SA_DELTA_CTS,
  182. .intr_tx_mt = SIO_IR_SA_TX_MT,
  183. .intr_rx_timer = SIO_IR_SA_RX_TIMER,
  184. .intr_rx_high = SIO_IR_SA_RX_HIGH,
  185. .intr_tx_explicit = SIO_IR_SA_TX_EXPLICIT,
  186. .intr_clear = (SIO_IR_SA_TX_MT | SIO_IR_SA_RX_FULL
  187. | SIO_IR_SA_RX_HIGH
  188. | SIO_IR_SA_RX_TIMER
  189. | SIO_IR_SA_DELTA_DCD
  190. | SIO_IR_SA_DELTA_CTS
  191. | SIO_IR_SA_INT
  192. | SIO_IR_SA_TX_EXPLICIT
  193. | SIO_IR_SA_MEMERR),
  194. .intr_all = SIO_IR_SA,
  195. .rs422_select_pin = GPPR_UARTA_MODESEL_PIN,
  196. },
  197. /* values for port B */
  198. {
  199. .intr_delta_dcd = SIO_IR_SB_DELTA_DCD,
  200. .intr_delta_cts = SIO_IR_SB_DELTA_CTS,
  201. .intr_tx_mt = SIO_IR_SB_TX_MT,
  202. .intr_rx_timer = SIO_IR_SB_RX_TIMER,
  203. .intr_rx_high = SIO_IR_SB_RX_HIGH,
  204. .intr_tx_explicit = SIO_IR_SB_TX_EXPLICIT,
  205. .intr_clear = (SIO_IR_SB_TX_MT | SIO_IR_SB_RX_FULL
  206. | SIO_IR_SB_RX_HIGH
  207. | SIO_IR_SB_RX_TIMER
  208. | SIO_IR_SB_DELTA_DCD
  209. | SIO_IR_SB_DELTA_CTS
  210. | SIO_IR_SB_INT
  211. | SIO_IR_SB_TX_EXPLICIT
  212. | SIO_IR_SB_MEMERR),
  213. .intr_all = SIO_IR_SB,
  214. .rs422_select_pin = GPPR_UARTB_MODESEL_PIN,
  215. }
  216. };
  217. struct ring_entry {
  218. union {
  219. struct {
  220. uint32_t alldata;
  221. uint32_t allsc;
  222. } all;
  223. struct {
  224. char data[4]; /* data bytes */
  225. char sc[4]; /* status/control */
  226. } s;
  227. } u;
  228. };
  229. /* Test the valid bits in any of the 4 sc chars using "allsc" member */
  230. #define RING_ANY_VALID \
  231. ((uint32_t)(RXSB_MODEM_VALID | RXSB_DATA_VALID) * 0x01010101)
  232. #define ring_sc u.s.sc
  233. #define ring_data u.s.data
  234. #define ring_allsc u.all.allsc
  235. /* Number of entries per ring buffer. */
  236. #define ENTRIES_PER_RING (RING_BUF_SIZE / (int) sizeof(struct ring_entry))
  237. /* An individual ring */
  238. struct ring {
  239. struct ring_entry entries[ENTRIES_PER_RING];
  240. };
  241. /* The whole enchilada */
  242. struct ring_buffer {
  243. struct ring TX_A;
  244. struct ring RX_A;
  245. struct ring TX_B;
  246. struct ring RX_B;
  247. };
  248. /* Get a ring from a port struct */
  249. #define RING(_p, _wh) &(((struct ring_buffer *)((_p)->ip_cpu_ringbuf))->_wh)
  250. /* for Infinite loop detection */
  251. #define MAXITER 10000000
  252. /**
  253. * set_baud - Baud rate setting code
  254. * @port: port to set
  255. * @baud: baud rate to use
  256. */
  257. static int set_baud(struct ioc3_port *port, int baud)
  258. {
  259. int divisor;
  260. int actual_baud;
  261. int diff;
  262. int lcr, prediv;
  263. struct ioc3_uartregs __iomem *uart;
  264. for (prediv = 6; prediv < 64; prediv++) {
  265. divisor = SER_DIVISOR(baud, SER_CLK_SPEED(prediv));
  266. if (!divisor)
  267. continue; /* invalid divisor */
  268. actual_baud = DIVISOR_TO_BAUD(divisor, SER_CLK_SPEED(prediv));
  269. diff = actual_baud - baud;
  270. if (diff < 0)
  271. diff = -diff;
  272. /* if we're within 1% we've found a match */
  273. if (diff * 100 <= actual_baud)
  274. break;
  275. }
  276. /* if the above loop completed, we didn't match
  277. * the baud rate. give up.
  278. */
  279. if (prediv == 64) {
  280. NOT_PROGRESS();
  281. return 1;
  282. }
  283. uart = port->ip_uart_regs;
  284. lcr = readb(&uart->iu_lcr);
  285. writeb(lcr | UART_LCR_DLAB, &uart->iu_lcr);
  286. writeb((unsigned char)divisor, &uart->iu_dll);
  287. writeb((unsigned char)(divisor >> 8), &uart->iu_dlm);
  288. writeb((unsigned char)prediv, &uart->iu_scr);
  289. writeb((unsigned char)lcr, &uart->iu_lcr);
  290. return 0;
  291. }
  292. /**
  293. * get_ioc3_port - given a uart port, return the control structure
  294. * @the_port: uart port to find
  295. */
  296. static struct ioc3_port *get_ioc3_port(struct uart_port *the_port)
  297. {
  298. struct ioc3_driver_data *idd = dev_get_drvdata(the_port->dev);
  299. struct ioc3_card *card_ptr = idd->data[Submodule_slot];
  300. int ii, jj;
  301. if (!card_ptr) {
  302. NOT_PROGRESS();
  303. return NULL;
  304. }
  305. for (ii = 0; ii < PORTS_PER_CARD; ii++) {
  306. for (jj = 0; jj < LOGICAL_PORTS; jj++) {
  307. if (the_port == &card_ptr->ic_port[ii].icp_uart_port[jj])
  308. return card_ptr->ic_port[ii].icp_port;
  309. }
  310. }
  311. NOT_PROGRESS();
  312. return NULL;
  313. }
  314. /**
  315. * port_init - Initialize the sio and ioc3 hardware for a given port
  316. * called per port from attach...
  317. * @port: port to initialize
  318. */
  319. static inline int port_init(struct ioc3_port *port)
  320. {
  321. uint32_t sio_cr;
  322. struct port_hooks *hooks = port->ip_hooks;
  323. struct ioc3_uartregs __iomem *uart;
  324. int reset_loop_counter = 0xfffff;
  325. struct ioc3_driver_data *idd = port->ip_idd;
  326. /* Idle the IOC3 serial interface */
  327. writel(SSCR_RESET, &port->ip_serial_regs->sscr);
  328. /* Wait until any pending bus activity for this port has ceased */
  329. do {
  330. sio_cr = readl(&idd->vma->sio_cr);
  331. if (reset_loop_counter-- <= 0) {
  332. printk(KERN_WARNING
  333. "IOC3 unable to come out of reset"
  334. " scr 0x%x\n", sio_cr);
  335. return -1;
  336. }
  337. } while (!(sio_cr & SIO_CR_ARB_DIAG_IDLE) &&
  338. (((sio_cr &= SIO_CR_ARB_DIAG) == SIO_CR_ARB_DIAG_TXA)
  339. || sio_cr == SIO_CR_ARB_DIAG_TXB
  340. || sio_cr == SIO_CR_ARB_DIAG_RXA
  341. || sio_cr == SIO_CR_ARB_DIAG_RXB));
  342. /* Finish reset sequence */
  343. writel(0, &port->ip_serial_regs->sscr);
  344. /* Once RESET is done, reload cached tx_prod and rx_cons values
  345. * and set rings to empty by making prod == cons
  346. */
  347. port->ip_tx_prod = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
  348. writel(port->ip_tx_prod, &port->ip_serial_regs->stpir);
  349. port->ip_rx_cons = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
  350. writel(port->ip_rx_cons | SRCIR_ARM, &port->ip_serial_regs->srcir);
  351. /* Disable interrupts for this 16550 */
  352. uart = port->ip_uart_regs;
  353. writeb(0, &uart->iu_lcr);
  354. writeb(0, &uart->iu_ier);
  355. /* Set the default baud */
  356. set_baud(port, port->ip_baud);
  357. /* Set line control to 8 bits no parity */
  358. writeb(UART_LCR_WLEN8 | 0, &uart->iu_lcr);
  359. /* UART_LCR_STOP == 1 stop */
  360. /* Enable the FIFOs */
  361. writeb(UART_FCR_ENABLE_FIFO, &uart->iu_fcr);
  362. /* then reset 16550 FIFOs */
  363. writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
  364. &uart->iu_fcr);
  365. /* Clear modem control register */
  366. writeb(0, &uart->iu_mcr);
  367. /* Clear deltas in modem status register */
  368. writel(0, &port->ip_serial_regs->shadow);
  369. /* Only do this once per port pair */
  370. if (port->ip_hooks == &hooks_array[0]) {
  371. unsigned long ring_pci_addr;
  372. uint32_t __iomem *sbbr_l, *sbbr_h;
  373. sbbr_l = &idd->vma->sbbr_l;
  374. sbbr_h = &idd->vma->sbbr_h;
  375. ring_pci_addr = (unsigned long __iomem)port->ip_dma_ringbuf;
  376. DPRINT_CONFIG(("%s: ring_pci_addr 0x%p\n",
  377. __func__, (void *)ring_pci_addr));
  378. writel((unsigned int)((uint64_t) ring_pci_addr >> 32), sbbr_h);
  379. writel((unsigned int)ring_pci_addr | BUF_SIZE_BIT, sbbr_l);
  380. }
  381. /* Set the receive timeout value to 10 msec */
  382. writel(SRTR_HZ / 100, &port->ip_serial_regs->srtr);
  383. /* Set rx threshold, enable DMA */
  384. /* Set high water mark at 3/4 of full ring */
  385. port->ip_sscr = (ENTRIES_PER_RING * 3 / 4);
  386. /* uart experiences pauses at high baud rate reducing actual
  387. * throughput by 10% or so unless we enable high speed polling
  388. * XXX when this hardware bug is resolved we should revert to
  389. * normal polling speed
  390. */
  391. port->ip_sscr |= SSCR_HIGH_SPD;
  392. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  393. /* Disable and clear all serial related interrupt bits */
  394. port->ip_card->ic_enable &= ~hooks->intr_clear;
  395. ioc3_disable(port->ip_is, idd, hooks->intr_clear);
  396. ioc3_ack(port->ip_is, idd, hooks->intr_clear);
  397. return 0;
  398. }
  399. /**
  400. * enable_intrs - enable interrupts
  401. * @port: port to enable
  402. * @mask: mask to use
  403. */
  404. static void enable_intrs(struct ioc3_port *port, uint32_t mask)
  405. {
  406. if ((port->ip_card->ic_enable & mask) != mask) {
  407. port->ip_card->ic_enable |= mask;
  408. ioc3_enable(port->ip_is, port->ip_idd, mask);
  409. }
  410. }
  411. /**
  412. * local_open - local open a port
  413. * @port: port to open
  414. */
  415. static inline int local_open(struct ioc3_port *port)
  416. {
  417. int spiniter = 0;
  418. port->ip_flags = INPUT_ENABLE;
  419. /* Pause the DMA interface if necessary */
  420. if (port->ip_sscr & SSCR_DMA_EN) {
  421. writel(port->ip_sscr | SSCR_DMA_PAUSE,
  422. &port->ip_serial_regs->sscr);
  423. while ((readl(&port->ip_serial_regs->sscr)
  424. & SSCR_PAUSE_STATE) == 0) {
  425. spiniter++;
  426. if (spiniter > MAXITER) {
  427. NOT_PROGRESS();
  428. return -1;
  429. }
  430. }
  431. }
  432. /* Reset the input fifo. If the uart received chars while the port
  433. * was closed and DMA is not enabled, the uart may have a bunch of
  434. * chars hanging around in its rx fifo which will not be discarded
  435. * by rclr in the upper layer. We must get rid of them here.
  436. */
  437. writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR,
  438. &port->ip_uart_regs->iu_fcr);
  439. writeb(UART_LCR_WLEN8, &port->ip_uart_regs->iu_lcr);
  440. /* UART_LCR_STOP == 1 stop */
  441. /* Re-enable DMA, set default threshold to intr whenever there is
  442. * data available.
  443. */
  444. port->ip_sscr &= ~SSCR_RX_THRESHOLD;
  445. port->ip_sscr |= 1; /* default threshold */
  446. /* Plug in the new sscr. This implicitly clears the DMA_PAUSE
  447. * flag if it was set above
  448. */
  449. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  450. port->ip_tx_lowat = 1;
  451. return 0;
  452. }
  453. /**
  454. * set_rx_timeout - Set rx timeout and threshold values.
  455. * @port: port to use
  456. * @timeout: timeout value in ticks
  457. */
  458. static inline int set_rx_timeout(struct ioc3_port *port, int timeout)
  459. {
  460. int threshold;
  461. port->ip_rx_timeout = timeout;
  462. /* Timeout is in ticks. Let's figure out how many chars we
  463. * can receive at the current baud rate in that interval
  464. * and set the rx threshold to that amount. There are 4 chars
  465. * per ring entry, so we'll divide the number of chars that will
  466. * arrive in timeout by 4.
  467. * So .... timeout * baud / 10 / HZ / 4, with HZ = 100.
  468. */
  469. threshold = timeout * port->ip_baud / 4000;
  470. if (threshold == 0)
  471. threshold = 1; /* otherwise we'll intr all the time! */
  472. if ((unsigned)threshold > (unsigned)SSCR_RX_THRESHOLD)
  473. return 1;
  474. port->ip_sscr &= ~SSCR_RX_THRESHOLD;
  475. port->ip_sscr |= threshold;
  476. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  477. /* Now set the rx timeout to the given value
  478. * again timeout * SRTR_HZ / HZ
  479. */
  480. timeout = timeout * SRTR_HZ / 100;
  481. if (timeout > SRTR_CNT)
  482. timeout = SRTR_CNT;
  483. writel(timeout, &port->ip_serial_regs->srtr);
  484. return 0;
  485. }
  486. /**
  487. * config_port - config the hardware
  488. * @port: port to config
  489. * @baud: baud rate for the port
  490. * @byte_size: data size
  491. * @stop_bits: number of stop bits
  492. * @parenb: parity enable ?
  493. * @parodd: odd parity ?
  494. */
  495. static inline int
  496. config_port(struct ioc3_port *port,
  497. int baud, int byte_size, int stop_bits, int parenb, int parodd)
  498. {
  499. char lcr, sizebits;
  500. int spiniter = 0;
  501. DPRINT_CONFIG(("%s: line %d baud %d byte_size %d stop %d parenb %d "
  502. "parodd %d\n",
  503. __func__, ((struct uart_port *)port->ip_port)->line,
  504. baud, byte_size, stop_bits, parenb, parodd));
  505. if (set_baud(port, baud))
  506. return 1;
  507. switch (byte_size) {
  508. case 5:
  509. sizebits = UART_LCR_WLEN5;
  510. break;
  511. case 6:
  512. sizebits = UART_LCR_WLEN6;
  513. break;
  514. case 7:
  515. sizebits = UART_LCR_WLEN7;
  516. break;
  517. case 8:
  518. sizebits = UART_LCR_WLEN8;
  519. break;
  520. default:
  521. return 1;
  522. }
  523. /* Pause the DMA interface if necessary */
  524. if (port->ip_sscr & SSCR_DMA_EN) {
  525. writel(port->ip_sscr | SSCR_DMA_PAUSE,
  526. &port->ip_serial_regs->sscr);
  527. while ((readl(&port->ip_serial_regs->sscr)
  528. & SSCR_PAUSE_STATE) == 0) {
  529. spiniter++;
  530. if (spiniter > MAXITER)
  531. return -1;
  532. }
  533. }
  534. /* Clear relevant fields in lcr */
  535. lcr = readb(&port->ip_uart_regs->iu_lcr);
  536. lcr &= ~(LCR_MASK_BITS_CHAR | UART_LCR_EPAR |
  537. UART_LCR_PARITY | LCR_MASK_STOP_BITS);
  538. /* Set byte size in lcr */
  539. lcr |= sizebits;
  540. /* Set parity */
  541. if (parenb) {
  542. lcr |= UART_LCR_PARITY;
  543. if (!parodd)
  544. lcr |= UART_LCR_EPAR;
  545. }
  546. /* Set stop bits */
  547. if (stop_bits)
  548. lcr |= UART_LCR_STOP /* 2 stop bits */ ;
  549. writeb(lcr, &port->ip_uart_regs->iu_lcr);
  550. /* Re-enable the DMA interface if necessary */
  551. if (port->ip_sscr & SSCR_DMA_EN) {
  552. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  553. }
  554. port->ip_baud = baud;
  555. /* When we get within this number of ring entries of filling the
  556. * entire ring on tx, place an EXPLICIT intr to generate a lowat
  557. * notification when output has drained.
  558. */
  559. port->ip_tx_lowat = (TX_LOWAT_CHARS(baud) + 3) / 4;
  560. if (port->ip_tx_lowat == 0)
  561. port->ip_tx_lowat = 1;
  562. set_rx_timeout(port, 2);
  563. return 0;
  564. }
  565. /**
  566. * do_write - Write bytes to the port. Returns the number of bytes
  567. * actually written. Called from transmit_chars
  568. * @port: port to use
  569. * @buf: the stuff to write
  570. * @len: how many bytes in 'buf'
  571. */
  572. static inline int do_write(struct ioc3_port *port, char *buf, int len)
  573. {
  574. int prod_ptr, cons_ptr, total = 0;
  575. struct ring *outring;
  576. struct ring_entry *entry;
  577. struct port_hooks *hooks = port->ip_hooks;
  578. BUG_ON(!(len >= 0));
  579. prod_ptr = port->ip_tx_prod;
  580. cons_ptr = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
  581. outring = port->ip_outring;
  582. /* Maintain a 1-entry red-zone. The ring buffer is full when
  583. * (cons - prod) % ring_size is 1. Rather than do this subtraction
  584. * in the body of the loop, I'll do it now.
  585. */
  586. cons_ptr = (cons_ptr - (int)sizeof(struct ring_entry)) & PROD_CONS_MASK;
  587. /* Stuff the bytes into the output */
  588. while ((prod_ptr != cons_ptr) && (len > 0)) {
  589. int xx;
  590. /* Get 4 bytes (one ring entry) at a time */
  591. entry = (struct ring_entry *)((caddr_t) outring + prod_ptr);
  592. /* Invalidate all entries */
  593. entry->ring_allsc = 0;
  594. /* Copy in some bytes */
  595. for (xx = 0; (xx < 4) && (len > 0); xx++) {
  596. entry->ring_data[xx] = *buf++;
  597. entry->ring_sc[xx] = TXCB_VALID;
  598. len--;
  599. total++;
  600. }
  601. /* If we are within some small threshold of filling up the
  602. * entire ring buffer, we must place an EXPLICIT intr here
  603. * to generate a lowat interrupt in case we subsequently
  604. * really do fill up the ring and the caller goes to sleep.
  605. * No need to place more than one though.
  606. */
  607. if (!(port->ip_flags & LOWAT_WRITTEN) &&
  608. ((cons_ptr - prod_ptr) & PROD_CONS_MASK)
  609. <= port->ip_tx_lowat * (int)sizeof(struct ring_entry)) {
  610. port->ip_flags |= LOWAT_WRITTEN;
  611. entry->ring_sc[0] |= TXCB_INT_WHEN_DONE;
  612. }
  613. /* Go on to next entry */
  614. prod_ptr += sizeof(struct ring_entry);
  615. prod_ptr &= PROD_CONS_MASK;
  616. }
  617. /* If we sent something, start DMA if necessary */
  618. if (total > 0 && !(port->ip_sscr & SSCR_DMA_EN)) {
  619. port->ip_sscr |= SSCR_DMA_EN;
  620. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  621. }
  622. /* Store the new producer pointer. If tx is disabled, we stuff the
  623. * data into the ring buffer, but we don't actually start tx.
  624. */
  625. if (!uart_tx_stopped(port->ip_port)) {
  626. writel(prod_ptr, &port->ip_serial_regs->stpir);
  627. /* If we are now transmitting, enable tx_mt interrupt so we
  628. * can disable DMA if necessary when the tx finishes.
  629. */
  630. if (total > 0)
  631. enable_intrs(port, hooks->intr_tx_mt);
  632. }
  633. port->ip_tx_prod = prod_ptr;
  634. return total;
  635. }
  636. /**
  637. * disable_intrs - disable interrupts
  638. * @port: port to enable
  639. * @mask: mask to use
  640. */
  641. static inline void disable_intrs(struct ioc3_port *port, uint32_t mask)
  642. {
  643. if (port->ip_card->ic_enable & mask) {
  644. ioc3_disable(port->ip_is, port->ip_idd, mask);
  645. port->ip_card->ic_enable &= ~mask;
  646. }
  647. }
  648. /**
  649. * set_notification - Modify event notification
  650. * @port: port to use
  651. * @mask: events mask
  652. * @set_on: set ?
  653. */
  654. static int set_notification(struct ioc3_port *port, int mask, int set_on)
  655. {
  656. struct port_hooks *hooks = port->ip_hooks;
  657. uint32_t intrbits, sscrbits;
  658. BUG_ON(!mask);
  659. intrbits = sscrbits = 0;
  660. if (mask & N_DATA_READY)
  661. intrbits |= (hooks->intr_rx_timer | hooks->intr_rx_high);
  662. if (mask & N_OUTPUT_LOWAT)
  663. intrbits |= hooks->intr_tx_explicit;
  664. if (mask & N_DDCD) {
  665. intrbits |= hooks->intr_delta_dcd;
  666. sscrbits |= SSCR_RX_RING_DCD;
  667. }
  668. if (mask & N_DCTS)
  669. intrbits |= hooks->intr_delta_cts;
  670. if (set_on) {
  671. enable_intrs(port, intrbits);
  672. port->ip_notify |= mask;
  673. port->ip_sscr |= sscrbits;
  674. } else {
  675. disable_intrs(port, intrbits);
  676. port->ip_notify &= ~mask;
  677. port->ip_sscr &= ~sscrbits;
  678. }
  679. /* We require DMA if either DATA_READY or DDCD notification is
  680. * currently requested. If neither of these is requested and
  681. * there is currently no tx in progress, DMA may be disabled.
  682. */
  683. if (port->ip_notify & (N_DATA_READY | N_DDCD))
  684. port->ip_sscr |= SSCR_DMA_EN;
  685. else if (!(port->ip_card->ic_enable & hooks->intr_tx_mt))
  686. port->ip_sscr &= ~SSCR_DMA_EN;
  687. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  688. return 0;
  689. }
  690. /**
  691. * set_mcr - set the master control reg
  692. * @the_port: port to use
  693. * @mask1: mcr mask
  694. * @mask2: shadow mask
  695. */
  696. static inline int set_mcr(struct uart_port *the_port,
  697. int mask1, int mask2)
  698. {
  699. struct ioc3_port *port = get_ioc3_port(the_port);
  700. uint32_t shadow;
  701. int spiniter = 0;
  702. char mcr;
  703. if (!port)
  704. return -1;
  705. /* Pause the DMA interface if necessary */
  706. if (port->ip_sscr & SSCR_DMA_EN) {
  707. writel(port->ip_sscr | SSCR_DMA_PAUSE,
  708. &port->ip_serial_regs->sscr);
  709. while ((readl(&port->ip_serial_regs->sscr)
  710. & SSCR_PAUSE_STATE) == 0) {
  711. spiniter++;
  712. if (spiniter > MAXITER)
  713. return -1;
  714. }
  715. }
  716. shadow = readl(&port->ip_serial_regs->shadow);
  717. mcr = (shadow & 0xff000000) >> 24;
  718. /* Set new value */
  719. mcr |= mask1;
  720. shadow |= mask2;
  721. writeb(mcr, &port->ip_uart_regs->iu_mcr);
  722. writel(shadow, &port->ip_serial_regs->shadow);
  723. /* Re-enable the DMA interface if necessary */
  724. if (port->ip_sscr & SSCR_DMA_EN) {
  725. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  726. }
  727. return 0;
  728. }
  729. /**
  730. * ioc3_set_proto - set the protocol for the port
  731. * @port: port to use
  732. * @proto: protocol to use
  733. */
  734. static int ioc3_set_proto(struct ioc3_port *port, int proto)
  735. {
  736. struct port_hooks *hooks = port->ip_hooks;
  737. switch (proto) {
  738. default:
  739. case PROTO_RS232:
  740. /* Clear the appropriate GIO pin */
  741. DPRINT_CONFIG(("%s: rs232\n", __func__));
  742. writel(0, (&port->ip_idd->vma->gppr[0]
  743. + hooks->rs422_select_pin));
  744. break;
  745. case PROTO_RS422:
  746. /* Set the appropriate GIO pin */
  747. DPRINT_CONFIG(("%s: rs422\n", __func__));
  748. writel(1, (&port->ip_idd->vma->gppr[0]
  749. + hooks->rs422_select_pin));
  750. break;
  751. }
  752. return 0;
  753. }
  754. /**
  755. * transmit_chars - upper level write, called with the_port->lock
  756. * @the_port: port to write
  757. */
  758. static void transmit_chars(struct uart_port *the_port)
  759. {
  760. int xmit_count, tail, head;
  761. int result;
  762. char *start;
  763. struct tty_struct *tty;
  764. struct ioc3_port *port = get_ioc3_port(the_port);
  765. struct uart_state *state;
  766. if (!the_port)
  767. return;
  768. if (!port)
  769. return;
  770. state = the_port->state;
  771. tty = state->port.tty;
  772. if (uart_circ_empty(&state->xmit) || uart_tx_stopped(the_port)) {
  773. /* Nothing to do or hw stopped */
  774. set_notification(port, N_ALL_OUTPUT, 0);
  775. return;
  776. }
  777. head = state->xmit.head;
  778. tail = state->xmit.tail;
  779. start = (char *)&state->xmit.buf[tail];
  780. /* write out all the data or until the end of the buffer */
  781. xmit_count = (head < tail) ? (UART_XMIT_SIZE - tail) : (head - tail);
  782. if (xmit_count > 0) {
  783. result = do_write(port, start, xmit_count);
  784. if (result > 0) {
  785. /* booking */
  786. xmit_count -= result;
  787. the_port->icount.tx += result;
  788. /* advance the pointers */
  789. tail += result;
  790. tail &= UART_XMIT_SIZE - 1;
  791. state->xmit.tail = tail;
  792. start = (char *)&state->xmit.buf[tail];
  793. }
  794. }
  795. if (uart_circ_chars_pending(&state->xmit) < WAKEUP_CHARS)
  796. uart_write_wakeup(the_port);
  797. if (uart_circ_empty(&state->xmit)) {
  798. set_notification(port, N_OUTPUT_LOWAT, 0);
  799. } else {
  800. set_notification(port, N_OUTPUT_LOWAT, 1);
  801. }
  802. }
  803. /**
  804. * ioc3_change_speed - change the speed of the port
  805. * @the_port: port to change
  806. * @new_termios: new termios settings
  807. * @old_termios: old termios settings
  808. */
  809. static void
  810. ioc3_change_speed(struct uart_port *the_port,
  811. struct ktermios *new_termios, struct ktermios *old_termios)
  812. {
  813. struct ioc3_port *port = get_ioc3_port(the_port);
  814. unsigned int cflag, iflag;
  815. int baud;
  816. int new_parity = 0, new_parity_enable = 0, new_stop = 0, new_data = 8;
  817. struct uart_state *state = the_port->state;
  818. cflag = new_termios->c_cflag;
  819. iflag = new_termios->c_iflag;
  820. switch (cflag & CSIZE) {
  821. case CS5:
  822. new_data = 5;
  823. break;
  824. case CS6:
  825. new_data = 6;
  826. break;
  827. case CS7:
  828. new_data = 7;
  829. break;
  830. case CS8:
  831. new_data = 8;
  832. break;
  833. default:
  834. /* cuz we always need a default ... */
  835. new_data = 5;
  836. break;
  837. }
  838. if (cflag & CSTOPB) {
  839. new_stop = 1;
  840. }
  841. if (cflag & PARENB) {
  842. new_parity_enable = 1;
  843. if (cflag & PARODD)
  844. new_parity = 1;
  845. }
  846. baud = uart_get_baud_rate(the_port, new_termios, old_termios,
  847. MIN_BAUD_SUPPORTED, MAX_BAUD_SUPPORTED);
  848. DPRINT_CONFIG(("%s: returned baud %d for line %d\n", __func__, baud,
  849. the_port->line));
  850. if (!the_port->fifosize)
  851. the_port->fifosize = FIFO_SIZE;
  852. uart_update_timeout(the_port, cflag, baud);
  853. the_port->ignore_status_mask = N_ALL_INPUT;
  854. state->port.low_latency = 1;
  855. if (iflag & IGNPAR)
  856. the_port->ignore_status_mask &= ~(N_PARITY_ERROR
  857. | N_FRAMING_ERROR);
  858. if (iflag & IGNBRK) {
  859. the_port->ignore_status_mask &= ~N_BREAK;
  860. if (iflag & IGNPAR)
  861. the_port->ignore_status_mask &= ~N_OVERRUN_ERROR;
  862. }
  863. if (!(cflag & CREAD)) {
  864. /* ignore everything */
  865. the_port->ignore_status_mask &= ~N_DATA_READY;
  866. }
  867. if (cflag & CRTSCTS) {
  868. /* enable hardware flow control */
  869. port->ip_sscr |= SSCR_HFC_EN;
  870. }
  871. else {
  872. /* disable hardware flow control */
  873. port->ip_sscr &= ~SSCR_HFC_EN;
  874. }
  875. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  876. /* Set the configuration and proper notification call */
  877. DPRINT_CONFIG(("%s : port 0x%p line %d cflag 0%o "
  878. "config_port(baud %d data %d stop %d penable %d "
  879. " parity %d), notification 0x%x\n",
  880. __func__, (void *)port, the_port->line, cflag, baud,
  881. new_data, new_stop, new_parity_enable, new_parity,
  882. the_port->ignore_status_mask));
  883. if ((config_port(port, baud, /* baud */
  884. new_data, /* byte size */
  885. new_stop, /* stop bits */
  886. new_parity_enable, /* set parity */
  887. new_parity)) >= 0) { /* parity 1==odd */
  888. set_notification(port, the_port->ignore_status_mask, 1);
  889. }
  890. }
  891. /**
  892. * ic3_startup_local - Start up the serial port - returns >= 0 if no errors
  893. * @the_port: Port to operate on
  894. */
  895. static inline int ic3_startup_local(struct uart_port *the_port)
  896. {
  897. struct ioc3_port *port;
  898. if (!the_port) {
  899. NOT_PROGRESS();
  900. return -1;
  901. }
  902. port = get_ioc3_port(the_port);
  903. if (!port) {
  904. NOT_PROGRESS();
  905. return -1;
  906. }
  907. local_open(port);
  908. /* set the protocol */
  909. ioc3_set_proto(port, IS_RS232(the_port->line) ? PROTO_RS232 :
  910. PROTO_RS422);
  911. return 0;
  912. }
  913. /*
  914. * ioc3_cb_output_lowat - called when the output low water mark is hit
  915. * @port: port to output
  916. */
  917. static void ioc3_cb_output_lowat(struct ioc3_port *port)
  918. {
  919. unsigned long pflags;
  920. /* the_port->lock is set on the call here */
  921. if (port->ip_port) {
  922. spin_lock_irqsave(&port->ip_port->lock, pflags);
  923. transmit_chars(port->ip_port);
  924. spin_unlock_irqrestore(&port->ip_port->lock, pflags);
  925. }
  926. }
  927. /*
  928. * ioc3_cb_post_ncs - called for some basic errors
  929. * @port: port to use
  930. * @ncs: event
  931. */
  932. static void ioc3_cb_post_ncs(struct uart_port *the_port, int ncs)
  933. {
  934. struct uart_icount *icount;
  935. icount = &the_port->icount;
  936. if (ncs & NCS_BREAK)
  937. icount->brk++;
  938. if (ncs & NCS_FRAMING)
  939. icount->frame++;
  940. if (ncs & NCS_OVERRUN)
  941. icount->overrun++;
  942. if (ncs & NCS_PARITY)
  943. icount->parity++;
  944. }
  945. /**
  946. * do_read - Read in bytes from the port. Return the number of bytes
  947. * actually read.
  948. * @the_port: port to use
  949. * @buf: place to put the stuff we read
  950. * @len: how big 'buf' is
  951. */
  952. static inline int do_read(struct uart_port *the_port, char *buf, int len)
  953. {
  954. int prod_ptr, cons_ptr, total;
  955. struct ioc3_port *port = get_ioc3_port(the_port);
  956. struct ring *inring;
  957. struct ring_entry *entry;
  958. struct port_hooks *hooks;
  959. int byte_num;
  960. char *sc;
  961. int loop_counter;
  962. BUG_ON(!(len >= 0));
  963. BUG_ON(!port);
  964. hooks = port->ip_hooks;
  965. /* There is a nasty timing issue in the IOC3. When the rx_timer
  966. * expires or the rx_high condition arises, we take an interrupt.
  967. * At some point while servicing the interrupt, we read bytes from
  968. * the ring buffer and re-arm the rx_timer. However the rx_timer is
  969. * not started until the first byte is received *after* it is armed,
  970. * and any bytes pending in the rx construction buffers are not drained
  971. * to memory until either there are 4 bytes available or the rx_timer
  972. * expires. This leads to a potential situation where data is left
  973. * in the construction buffers forever - 1 to 3 bytes were received
  974. * after the interrupt was generated but before the rx_timer was
  975. * re-armed. At that point as long as no subsequent bytes are received
  976. * the timer will never be started and the bytes will remain in the
  977. * construction buffer forever. The solution is to execute a DRAIN
  978. * command after rearming the timer. This way any bytes received before
  979. * the DRAIN will be drained to memory, and any bytes received after
  980. * the DRAIN will start the TIMER and be drained when it expires.
  981. * Luckily, this only needs to be done when the DMA buffer is empty
  982. * since there is no requirement that this function return all
  983. * available data as long as it returns some.
  984. */
  985. /* Re-arm the timer */
  986. writel(port->ip_rx_cons | SRCIR_ARM, &port->ip_serial_regs->srcir);
  987. prod_ptr = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
  988. cons_ptr = port->ip_rx_cons;
  989. if (prod_ptr == cons_ptr) {
  990. int reset_dma = 0;
  991. /* Input buffer appears empty, do a flush. */
  992. /* DMA must be enabled for this to work. */
  993. if (!(port->ip_sscr & SSCR_DMA_EN)) {
  994. port->ip_sscr |= SSCR_DMA_EN;
  995. reset_dma = 1;
  996. }
  997. /* Potential race condition: we must reload the srpir after
  998. * issuing the drain command, otherwise we could think the rx
  999. * buffer is empty, then take a very long interrupt, and when
  1000. * we come back it's full and we wait forever for the drain to
  1001. * complete.
  1002. */
  1003. writel(port->ip_sscr | SSCR_RX_DRAIN,
  1004. &port->ip_serial_regs->sscr);
  1005. prod_ptr = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
  1006. /* We must not wait for the DRAIN to complete unless there are
  1007. * at least 8 bytes (2 ring entries) available to receive the
  1008. * data otherwise the DRAIN will never complete and we'll
  1009. * deadlock here.
  1010. * In fact, to make things easier, I'll just ignore the flush if
  1011. * there is any data at all now available.
  1012. */
  1013. if (prod_ptr == cons_ptr) {
  1014. loop_counter = 0;
  1015. while (readl(&port->ip_serial_regs->sscr) &
  1016. SSCR_RX_DRAIN) {
  1017. loop_counter++;
  1018. if (loop_counter > MAXITER)
  1019. return -1;
  1020. }
  1021. /* SIGH. We have to reload the prod_ptr *again* since
  1022. * the drain may have caused it to change
  1023. */
  1024. prod_ptr = readl(&port->ip_serial_regs->srpir)
  1025. & PROD_CONS_MASK;
  1026. }
  1027. if (reset_dma) {
  1028. port->ip_sscr &= ~SSCR_DMA_EN;
  1029. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  1030. }
  1031. }
  1032. inring = port->ip_inring;
  1033. port->ip_flags &= ~READ_ABORTED;
  1034. total = 0;
  1035. loop_counter = 0xfffff; /* to avoid hangs */
  1036. /* Grab bytes from the hardware */
  1037. while ((prod_ptr != cons_ptr) && (len > 0)) {
  1038. entry = (struct ring_entry *)((caddr_t) inring + cons_ptr);
  1039. if (loop_counter-- <= 0) {
  1040. printk(KERN_WARNING "IOC3 serial: "
  1041. "possible hang condition/"
  1042. "port stuck on read (line %d).\n",
  1043. the_port->line);
  1044. break;
  1045. }
  1046. /* According to the producer pointer, this ring entry
  1047. * must contain some data. But if the PIO happened faster
  1048. * than the DMA, the data may not be available yet, so let's
  1049. * wait until it arrives.
  1050. */
  1051. if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
  1052. /* Indicate the read is aborted so we don't disable
  1053. * the interrupt thinking that the consumer is
  1054. * congested.
  1055. */
  1056. port->ip_flags |= READ_ABORTED;
  1057. len = 0;
  1058. break;
  1059. }
  1060. /* Load the bytes/status out of the ring entry */
  1061. for (byte_num = 0; byte_num < 4 && len > 0; byte_num++) {
  1062. sc = &(entry->ring_sc[byte_num]);
  1063. /* Check for change in modem state or overrun */
  1064. if ((*sc & RXSB_MODEM_VALID)
  1065. && (port->ip_notify & N_DDCD)) {
  1066. /* Notify upper layer if DCD dropped */
  1067. if ((port->ip_flags & DCD_ON)
  1068. && !(*sc & RXSB_DCD)) {
  1069. /* If we have already copied some data,
  1070. * return it. We'll pick up the carrier
  1071. * drop on the next pass. That way we
  1072. * don't throw away the data that has
  1073. * already been copied back to
  1074. * the caller's buffer.
  1075. */
  1076. if (total > 0) {
  1077. len = 0;
  1078. break;
  1079. }
  1080. port->ip_flags &= ~DCD_ON;
  1081. /* Turn off this notification so the
  1082. * carrier drop protocol won't see it
  1083. * again when it does a read.
  1084. */
  1085. *sc &= ~RXSB_MODEM_VALID;
  1086. /* To keep things consistent, we need
  1087. * to update the consumer pointer so
  1088. * the next reader won't come in and
  1089. * try to read the same ring entries
  1090. * again. This must be done here before
  1091. * the dcd change.
  1092. */
  1093. if ((entry->ring_allsc & RING_ANY_VALID)
  1094. == 0) {
  1095. cons_ptr += (int)sizeof
  1096. (struct ring_entry);
  1097. cons_ptr &= PROD_CONS_MASK;
  1098. }
  1099. writel(cons_ptr,
  1100. &port->ip_serial_regs->srcir);
  1101. port->ip_rx_cons = cons_ptr;
  1102. /* Notify upper layer of carrier drop */
  1103. if ((port->ip_notify & N_DDCD)
  1104. && port->ip_port) {
  1105. uart_handle_dcd_change
  1106. (port->ip_port, 0);
  1107. wake_up_interruptible
  1108. (&the_port->state->
  1109. port.delta_msr_wait);
  1110. }
  1111. /* If we had any data to return, we
  1112. * would have returned it above.
  1113. */
  1114. return 0;
  1115. }
  1116. }
  1117. if (*sc & RXSB_MODEM_VALID) {
  1118. /* Notify that an input overrun occurred */
  1119. if ((*sc & RXSB_OVERRUN)
  1120. && (port->ip_notify & N_OVERRUN_ERROR)) {
  1121. ioc3_cb_post_ncs(the_port, NCS_OVERRUN);
  1122. }
  1123. /* Don't look at this byte again */
  1124. *sc &= ~RXSB_MODEM_VALID;
  1125. }
  1126. /* Check for valid data or RX errors */
  1127. if ((*sc & RXSB_DATA_VALID) &&
  1128. ((*sc & (RXSB_PAR_ERR
  1129. | RXSB_FRAME_ERR | RXSB_BREAK))
  1130. && (port->ip_notify & (N_PARITY_ERROR
  1131. | N_FRAMING_ERROR
  1132. | N_BREAK)))) {
  1133. /* There is an error condition on the next byte.
  1134. * If we have already transferred some bytes,
  1135. * we'll stop here. Otherwise if this is the
  1136. * first byte to be read, we'll just transfer
  1137. * it alone after notifying the
  1138. * upper layer of its status.
  1139. */
  1140. if (total > 0) {
  1141. len = 0;
  1142. break;
  1143. } else {
  1144. if ((*sc & RXSB_PAR_ERR) &&
  1145. (port->
  1146. ip_notify & N_PARITY_ERROR)) {
  1147. ioc3_cb_post_ncs(the_port,
  1148. NCS_PARITY);
  1149. }
  1150. if ((*sc & RXSB_FRAME_ERR) &&
  1151. (port->
  1152. ip_notify & N_FRAMING_ERROR)) {
  1153. ioc3_cb_post_ncs(the_port,
  1154. NCS_FRAMING);
  1155. }
  1156. if ((*sc & RXSB_BREAK)
  1157. && (port->ip_notify & N_BREAK)) {
  1158. ioc3_cb_post_ncs
  1159. (the_port, NCS_BREAK);
  1160. }
  1161. len = 1;
  1162. }
  1163. }
  1164. if (*sc & RXSB_DATA_VALID) {
  1165. *sc &= ~RXSB_DATA_VALID;
  1166. *buf = entry->ring_data[byte_num];
  1167. buf++;
  1168. len--;
  1169. total++;
  1170. }
  1171. }
  1172. /* If we used up this entry entirely, go on to the next one,
  1173. * otherwise we must have run out of buffer space, so
  1174. * leave the consumer pointer here for the next read in case
  1175. * there are still unread bytes in this entry.
  1176. */
  1177. if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
  1178. cons_ptr += (int)sizeof(struct ring_entry);
  1179. cons_ptr &= PROD_CONS_MASK;
  1180. }
  1181. }
  1182. /* Update consumer pointer and re-arm rx timer interrupt */
  1183. writel(cons_ptr, &port->ip_serial_regs->srcir);
  1184. port->ip_rx_cons = cons_ptr;
  1185. /* If we have now dipped below the rx high water mark and we have
  1186. * rx_high interrupt turned off, we can now turn it back on again.
  1187. */
  1188. if ((port->ip_flags & INPUT_HIGH) && (((prod_ptr - cons_ptr)
  1189. & PROD_CONS_MASK) <
  1190. ((port->
  1191. ip_sscr &
  1192. SSCR_RX_THRESHOLD)
  1193. << PROD_CONS_PTR_OFF))) {
  1194. port->ip_flags &= ~INPUT_HIGH;
  1195. enable_intrs(port, hooks->intr_rx_high);
  1196. }
  1197. return total;
  1198. }
  1199. /**
  1200. * receive_chars - upper level read.
  1201. * @the_port: port to read from
  1202. */
  1203. static int receive_chars(struct uart_port *the_port)
  1204. {
  1205. unsigned char ch[MAX_CHARS];
  1206. int read_count = 0, read_room, flip = 0;
  1207. struct uart_state *state = the_port->state;
  1208. struct ioc3_port *port = get_ioc3_port(the_port);
  1209. unsigned long pflags;
  1210. /* Make sure all the pointers are "good" ones */
  1211. if (!state)
  1212. return 0;
  1213. if (!(port->ip_flags & INPUT_ENABLE))
  1214. return 0;
  1215. spin_lock_irqsave(&the_port->lock, pflags);
  1216. read_count = do_read(the_port, ch, MAX_CHARS);
  1217. if (read_count > 0) {
  1218. flip = 1;
  1219. read_room = tty_insert_flip_string(&state->port, ch,
  1220. read_count);
  1221. the_port->icount.rx += read_count;
  1222. }
  1223. spin_unlock_irqrestore(&the_port->lock, pflags);
  1224. if (flip)
  1225. tty_flip_buffer_push(&state->port);
  1226. return read_count;
  1227. }
  1228. /**
  1229. * ioc3uart_intr_one - lowest level (per port) interrupt handler.
  1230. * @is : submodule
  1231. * @idd: driver data
  1232. * @pending: interrupts to handle
  1233. */
  1234. static inline int
  1235. ioc3uart_intr_one(struct ioc3_submodule *is,
  1236. struct ioc3_driver_data *idd,
  1237. unsigned int pending)
  1238. {
  1239. int port_num = GET_PORT_FROM_SIO_IR(pending);
  1240. struct port_hooks *hooks;
  1241. unsigned int rx_high_rd_aborted = 0;
  1242. unsigned long flags;
  1243. struct uart_port *the_port;
  1244. struct ioc3_port *port;
  1245. int loop_counter;
  1246. struct ioc3_card *card_ptr;
  1247. unsigned int sio_ir;
  1248. card_ptr = idd->data[is->id];
  1249. port = card_ptr->ic_port[port_num].icp_port;
  1250. hooks = port->ip_hooks;
  1251. /* Possible race condition here: The tx_mt interrupt bit may be
  1252. * cleared without the intervention of the interrupt handler,
  1253. * e.g. by a write. If the top level interrupt handler reads a
  1254. * tx_mt, then some other processor does a write, starting up
  1255. * output, then we come in here, see the tx_mt and stop DMA, the
  1256. * output started by the other processor will hang. Thus we can
  1257. * only rely on tx_mt being legitimate if it is read while the
  1258. * port lock is held. Therefore this bit must be ignored in the
  1259. * passed in interrupt mask which was read by the top level
  1260. * interrupt handler since the port lock was not held at the time
  1261. * it was read. We can only rely on this bit being accurate if it
  1262. * is read while the port lock is held. So we'll clear it for now,
  1263. * and reload it later once we have the port lock.
  1264. */
  1265. sio_ir = pending & ~(hooks->intr_tx_mt);
  1266. spin_lock_irqsave(&port->ip_lock, flags);
  1267. loop_counter = MAXITER; /* to avoid hangs */
  1268. do {
  1269. uint32_t shadow;
  1270. if (loop_counter-- <= 0) {
  1271. printk(KERN_WARNING "IOC3 serial: "
  1272. "possible hang condition/"
  1273. "port stuck on interrupt (line %d).\n",
  1274. ((struct uart_port *)port->ip_port)->line);
  1275. break;
  1276. }
  1277. /* Handle a DCD change */
  1278. if (sio_ir & hooks->intr_delta_dcd) {
  1279. ioc3_ack(is, idd, hooks->intr_delta_dcd);
  1280. shadow = readl(&port->ip_serial_regs->shadow);
  1281. if ((port->ip_notify & N_DDCD)
  1282. && (shadow & SHADOW_DCD)
  1283. && (port->ip_port)) {
  1284. the_port = port->ip_port;
  1285. uart_handle_dcd_change(the_port,
  1286. shadow & SHADOW_DCD);
  1287. wake_up_interruptible
  1288. (&the_port->state->port.delta_msr_wait);
  1289. } else if ((port->ip_notify & N_DDCD)
  1290. && !(shadow & SHADOW_DCD)) {
  1291. /* Flag delta DCD/no DCD */
  1292. uart_handle_dcd_change(port->ip_port,
  1293. shadow & SHADOW_DCD);
  1294. port->ip_flags |= DCD_ON;
  1295. }
  1296. }
  1297. /* Handle a CTS change */
  1298. if (sio_ir & hooks->intr_delta_cts) {
  1299. ioc3_ack(is, idd, hooks->intr_delta_cts);
  1300. shadow = readl(&port->ip_serial_regs->shadow);
  1301. if ((port->ip_notify & N_DCTS) && (port->ip_port)) {
  1302. the_port = port->ip_port;
  1303. uart_handle_cts_change(the_port, shadow
  1304. & SHADOW_CTS);
  1305. wake_up_interruptible
  1306. (&the_port->state->port.delta_msr_wait);
  1307. }
  1308. }
  1309. /* rx timeout interrupt. Must be some data available. Put this
  1310. * before the check for rx_high since servicing this condition
  1311. * may cause that condition to clear.
  1312. */
  1313. if (sio_ir & hooks->intr_rx_timer) {
  1314. ioc3_ack(is, idd, hooks->intr_rx_timer);
  1315. if ((port->ip_notify & N_DATA_READY)
  1316. && (port->ip_port)) {
  1317. receive_chars(port->ip_port);
  1318. }
  1319. }
  1320. /* rx high interrupt. Must be after rx_timer. */
  1321. else if (sio_ir & hooks->intr_rx_high) {
  1322. /* Data available, notify upper layer */
  1323. if ((port->ip_notify & N_DATA_READY) && port->ip_port) {
  1324. receive_chars(port->ip_port);
  1325. }
  1326. /* We can't ACK this interrupt. If receive_chars didn't
  1327. * cause the condition to clear, we'll have to disable
  1328. * the interrupt until the data is drained.
  1329. * If the read was aborted, don't disable the interrupt
  1330. * as this may cause us to hang indefinitely. An
  1331. * aborted read generally means that this interrupt
  1332. * hasn't been delivered to the cpu yet anyway, even
  1333. * though we see it as asserted when we read the sio_ir.
  1334. */
  1335. if ((sio_ir = PENDING(card_ptr, idd))
  1336. & hooks->intr_rx_high) {
  1337. if (port->ip_flags & READ_ABORTED) {
  1338. rx_high_rd_aborted++;
  1339. }
  1340. else {
  1341. card_ptr->ic_enable &= ~hooks->intr_rx_high;
  1342. port->ip_flags |= INPUT_HIGH;
  1343. }
  1344. }
  1345. }
  1346. /* We got a low water interrupt: notify upper layer to
  1347. * send more data. Must come before tx_mt since servicing
  1348. * this condition may cause that condition to clear.
  1349. */
  1350. if (sio_ir & hooks->intr_tx_explicit) {
  1351. port->ip_flags &= ~LOWAT_WRITTEN;
  1352. ioc3_ack(is, idd, hooks->intr_tx_explicit);
  1353. if (port->ip_notify & N_OUTPUT_LOWAT)
  1354. ioc3_cb_output_lowat(port);
  1355. }
  1356. /* Handle tx_mt. Must come after tx_explicit. */
  1357. else if (sio_ir & hooks->intr_tx_mt) {
  1358. /* If we are expecting a lowat notification
  1359. * and we get to this point it probably means that for
  1360. * some reason the tx_explicit didn't work as expected
  1361. * (that can legitimately happen if the output buffer is
  1362. * filled up in just the right way).
  1363. * So send the notification now.
  1364. */
  1365. if (port->ip_notify & N_OUTPUT_LOWAT) {
  1366. ioc3_cb_output_lowat(port);
  1367. /* We need to reload the sio_ir since the lowat
  1368. * call may have caused another write to occur,
  1369. * clearing the tx_mt condition.
  1370. */
  1371. sio_ir = PENDING(card_ptr, idd);
  1372. }
  1373. /* If the tx_mt condition still persists even after the
  1374. * lowat call, we've got some work to do.
  1375. */
  1376. if (sio_ir & hooks->intr_tx_mt) {
  1377. /* If we are not currently expecting DMA input,
  1378. * and the transmitter has just gone idle,
  1379. * there is no longer any reason for DMA, so
  1380. * disable it.
  1381. */
  1382. if (!(port->ip_notify
  1383. & (N_DATA_READY | N_DDCD))) {
  1384. BUG_ON(!(port->ip_sscr
  1385. & SSCR_DMA_EN));
  1386. port->ip_sscr &= ~SSCR_DMA_EN;
  1387. writel(port->ip_sscr,
  1388. &port->ip_serial_regs->sscr);
  1389. }
  1390. /* Prevent infinite tx_mt interrupt */
  1391. card_ptr->ic_enable &= ~hooks->intr_tx_mt;
  1392. }
  1393. }
  1394. sio_ir = PENDING(card_ptr, idd);
  1395. /* if the read was aborted and only hooks->intr_rx_high,
  1396. * clear hooks->intr_rx_high, so we do not loop forever.
  1397. */
  1398. if (rx_high_rd_aborted && (sio_ir == hooks->intr_rx_high)) {
  1399. sio_ir &= ~hooks->intr_rx_high;
  1400. }
  1401. } while (sio_ir & hooks->intr_all);
  1402. spin_unlock_irqrestore(&port->ip_lock, flags);
  1403. ioc3_enable(is, idd, card_ptr->ic_enable);
  1404. return 0;
  1405. }
  1406. /**
  1407. * ioc3uart_intr - field all serial interrupts
  1408. * @is : submodule
  1409. * @idd: driver data
  1410. * @pending: interrupts to handle
  1411. *
  1412. */
  1413. static int ioc3uart_intr(struct ioc3_submodule *is,
  1414. struct ioc3_driver_data *idd,
  1415. unsigned int pending)
  1416. {
  1417. int ret = 0;
  1418. /*
  1419. * The upper level interrupt handler sends interrupts for both ports
  1420. * here. So we need to call for each port with its interrupts.
  1421. */
  1422. if (pending & SIO_IR_SA)
  1423. ret |= ioc3uart_intr_one(is, idd, pending & SIO_IR_SA);
  1424. if (pending & SIO_IR_SB)
  1425. ret |= ioc3uart_intr_one(is, idd, pending & SIO_IR_SB);
  1426. return ret;
  1427. }
  1428. /**
  1429. * ic3_type
  1430. * @port: Port to operate with (we ignore since we only have one port)
  1431. *
  1432. */
  1433. static const char *ic3_type(struct uart_port *the_port)
  1434. {
  1435. if (IS_RS232(the_port->line))
  1436. return "SGI IOC3 Serial [rs232]";
  1437. else
  1438. return "SGI IOC3 Serial [rs422]";
  1439. }
  1440. /**
  1441. * ic3_tx_empty - Is the transmitter empty?
  1442. * @port: Port to operate on
  1443. *
  1444. */
  1445. static unsigned int ic3_tx_empty(struct uart_port *the_port)
  1446. {
  1447. unsigned int ret = 0;
  1448. struct ioc3_port *port = get_ioc3_port(the_port);
  1449. if (readl(&port->ip_serial_regs->shadow) & SHADOW_TEMT)
  1450. ret = TIOCSER_TEMT;
  1451. return ret;
  1452. }
  1453. /**
  1454. * ic3_stop_tx - stop the transmitter
  1455. * @port: Port to operate on
  1456. *
  1457. */
  1458. static void ic3_stop_tx(struct uart_port *the_port)
  1459. {
  1460. struct ioc3_port *port = get_ioc3_port(the_port);
  1461. if (port)
  1462. set_notification(port, N_OUTPUT_LOWAT, 0);
  1463. }
  1464. /**
  1465. * ic3_stop_rx - stop the receiver
  1466. * @port: Port to operate on
  1467. *
  1468. */
  1469. static void ic3_stop_rx(struct uart_port *the_port)
  1470. {
  1471. struct ioc3_port *port = get_ioc3_port(the_port);
  1472. if (port)
  1473. port->ip_flags &= ~INPUT_ENABLE;
  1474. }
  1475. /**
  1476. * null_void_function
  1477. * @port: Port to operate on
  1478. *
  1479. */
  1480. static void null_void_function(struct uart_port *the_port)
  1481. {
  1482. }
  1483. /**
  1484. * ic3_shutdown - shut down the port - free irq and disable
  1485. * @port: port to shut down
  1486. *
  1487. */
  1488. static void ic3_shutdown(struct uart_port *the_port)
  1489. {
  1490. unsigned long port_flags;
  1491. struct ioc3_port *port;
  1492. struct uart_state *state;
  1493. port = get_ioc3_port(the_port);
  1494. if (!port)
  1495. return;
  1496. state = the_port->state;
  1497. wake_up_interruptible(&state->port.delta_msr_wait);
  1498. spin_lock_irqsave(&the_port->lock, port_flags);
  1499. set_notification(port, N_ALL, 0);
  1500. spin_unlock_irqrestore(&the_port->lock, port_flags);
  1501. }
  1502. /**
  1503. * ic3_set_mctrl - set control lines (dtr, rts, etc)
  1504. * @port: Port to operate on
  1505. * @mctrl: Lines to set/unset
  1506. *
  1507. */
  1508. static void ic3_set_mctrl(struct uart_port *the_port, unsigned int mctrl)
  1509. {
  1510. unsigned char mcr = 0;
  1511. if (mctrl & TIOCM_RTS)
  1512. mcr |= UART_MCR_RTS;
  1513. if (mctrl & TIOCM_DTR)
  1514. mcr |= UART_MCR_DTR;
  1515. if (mctrl & TIOCM_OUT1)
  1516. mcr |= UART_MCR_OUT1;
  1517. if (mctrl & TIOCM_OUT2)
  1518. mcr |= UART_MCR_OUT2;
  1519. if (mctrl & TIOCM_LOOP)
  1520. mcr |= UART_MCR_LOOP;
  1521. set_mcr(the_port, mcr, SHADOW_DTR);
  1522. }
  1523. /**
  1524. * ic3_get_mctrl - get control line info
  1525. * @port: port to operate on
  1526. *
  1527. */
  1528. static unsigned int ic3_get_mctrl(struct uart_port *the_port)
  1529. {
  1530. struct ioc3_port *port = get_ioc3_port(the_port);
  1531. uint32_t shadow;
  1532. unsigned int ret = 0;
  1533. if (!port)
  1534. return 0;
  1535. shadow = readl(&port->ip_serial_regs->shadow);
  1536. if (shadow & SHADOW_DCD)
  1537. ret |= TIOCM_CD;
  1538. if (shadow & SHADOW_DR)
  1539. ret |= TIOCM_DSR;
  1540. if (shadow & SHADOW_CTS)
  1541. ret |= TIOCM_CTS;
  1542. return ret;
  1543. }
  1544. /**
  1545. * ic3_start_tx - Start transmitter. Called with the_port->lock
  1546. * @port: Port to operate on
  1547. *
  1548. */
  1549. static void ic3_start_tx(struct uart_port *the_port)
  1550. {
  1551. struct ioc3_port *port = get_ioc3_port(the_port);
  1552. if (port) {
  1553. set_notification(port, N_OUTPUT_LOWAT, 1);
  1554. enable_intrs(port, port->ip_hooks->intr_tx_mt);
  1555. }
  1556. }
  1557. /**
  1558. * ic3_break_ctl - handle breaks
  1559. * @port: Port to operate on
  1560. * @break_state: Break state
  1561. *
  1562. */
  1563. static void ic3_break_ctl(struct uart_port *the_port, int break_state)
  1564. {
  1565. }
  1566. /**
  1567. * ic3_startup - Start up the serial port - always return 0 (We're always on)
  1568. * @port: Port to operate on
  1569. *
  1570. */
  1571. static int ic3_startup(struct uart_port *the_port)
  1572. {
  1573. int retval;
  1574. struct ioc3_port *port;
  1575. struct ioc3_card *card_ptr;
  1576. unsigned long port_flags;
  1577. if (!the_port) {
  1578. NOT_PROGRESS();
  1579. return -ENODEV;
  1580. }
  1581. port = get_ioc3_port(the_port);
  1582. if (!port) {
  1583. NOT_PROGRESS();
  1584. return -ENODEV;
  1585. }
  1586. card_ptr = port->ip_card;
  1587. port->ip_port = the_port;
  1588. if (!card_ptr) {
  1589. NOT_PROGRESS();
  1590. return -ENODEV;
  1591. }
  1592. /* Start up the serial port */
  1593. spin_lock_irqsave(&the_port->lock, port_flags);
  1594. retval = ic3_startup_local(the_port);
  1595. spin_unlock_irqrestore(&the_port->lock, port_flags);
  1596. return retval;
  1597. }
  1598. /**
  1599. * ic3_set_termios - set termios stuff
  1600. * @port: port to operate on
  1601. * @termios: New settings
  1602. * @termios: Old
  1603. *
  1604. */
  1605. static void
  1606. ic3_set_termios(struct uart_port *the_port,
  1607. struct ktermios *termios, struct ktermios *old_termios)
  1608. {
  1609. unsigned long port_flags;
  1610. spin_lock_irqsave(&the_port->lock, port_flags);
  1611. ioc3_change_speed(the_port, termios, old_termios);
  1612. spin_unlock_irqrestore(&the_port->lock, port_flags);
  1613. }
  1614. /**
  1615. * ic3_request_port - allocate resources for port - no op....
  1616. * @port: port to operate on
  1617. *
  1618. */
  1619. static int ic3_request_port(struct uart_port *port)
  1620. {
  1621. return 0;
  1622. }
  1623. /* Associate the uart functions above - given to serial core */
  1624. static const struct uart_ops ioc3_ops = {
  1625. .tx_empty = ic3_tx_empty,
  1626. .set_mctrl = ic3_set_mctrl,
  1627. .get_mctrl = ic3_get_mctrl,
  1628. .stop_tx = ic3_stop_tx,
  1629. .start_tx = ic3_start_tx,
  1630. .stop_rx = ic3_stop_rx,
  1631. .break_ctl = ic3_break_ctl,
  1632. .startup = ic3_startup,
  1633. .shutdown = ic3_shutdown,
  1634. .set_termios = ic3_set_termios,
  1635. .type = ic3_type,
  1636. .release_port = null_void_function,
  1637. .request_port = ic3_request_port,
  1638. };
  1639. /*
  1640. * Boot-time initialization code
  1641. */
  1642. static struct uart_driver ioc3_uart = {
  1643. .owner = THIS_MODULE,
  1644. .driver_name = "ioc3_serial",
  1645. .dev_name = DEVICE_NAME,
  1646. .major = DEVICE_MAJOR,
  1647. .minor = DEVICE_MINOR,
  1648. .nr = MAX_LOGICAL_PORTS
  1649. };
  1650. /**
  1651. * ioc3_serial_core_attach - register with serial core
  1652. * This is done during pci probing
  1653. * @is: submodule struct for this
  1654. * @idd: handle for this card
  1655. */
  1656. static inline int ioc3_serial_core_attach( struct ioc3_submodule *is,
  1657. struct ioc3_driver_data *idd)
  1658. {
  1659. struct ioc3_port *port;
  1660. struct uart_port *the_port;
  1661. struct ioc3_card *card_ptr = idd->data[is->id];
  1662. int ii, phys_port;
  1663. struct pci_dev *pdev = idd->pdev;
  1664. DPRINT_CONFIG(("%s: attach pdev 0x%p - card_ptr 0x%p\n",
  1665. __func__, pdev, (void *)card_ptr));
  1666. if (!card_ptr)
  1667. return -ENODEV;
  1668. /* once around for each logical port on this card */
  1669. for (ii = 0; ii < LOGICAL_PORTS_PER_CARD; ii++) {
  1670. phys_port = GET_PHYSICAL_PORT(ii);
  1671. the_port = &card_ptr->ic_port[phys_port].
  1672. icp_uart_port[GET_LOGICAL_PORT(ii)];
  1673. port = card_ptr->ic_port[phys_port].icp_port;
  1674. port->ip_port = the_port;
  1675. DPRINT_CONFIG(("%s: attach the_port 0x%p / port 0x%p [%d/%d]\n",
  1676. __func__, (void *)the_port, (void *)port,
  1677. phys_port, ii));
  1678. /* membase, iobase and mapbase just need to be non-0 */
  1679. the_port->membase = (unsigned char __iomem *)1;
  1680. the_port->iobase = (pdev->bus->number << 16) | ii;
  1681. the_port->line = (Num_of_ioc3_cards << 2) | ii;
  1682. the_port->mapbase = 1;
  1683. the_port->type = PORT_16550A;
  1684. the_port->fifosize = FIFO_SIZE;
  1685. the_port->ops = &ioc3_ops;
  1686. the_port->irq = idd->irq_io;
  1687. the_port->dev = &pdev->dev;
  1688. if (uart_add_one_port(&ioc3_uart, the_port) < 0) {
  1689. printk(KERN_WARNING
  1690. "%s: unable to add port %d bus %d\n",
  1691. __func__, the_port->line, pdev->bus->number);
  1692. } else {
  1693. DPRINT_CONFIG(("IOC3 serial port %d irq %d bus %d\n",
  1694. the_port->line, the_port->irq, pdev->bus->number));
  1695. }
  1696. /* all ports are rs232 for now */
  1697. if (IS_PHYSICAL_PORT(ii))
  1698. ioc3_set_proto(port, PROTO_RS232);
  1699. }
  1700. return 0;
  1701. }
  1702. /**
  1703. * ioc3uart_remove - register detach function
  1704. * @is: submodule struct for this submodule
  1705. * @idd: ioc3 driver data for this submodule
  1706. */
  1707. static int ioc3uart_remove(struct ioc3_submodule *is,
  1708. struct ioc3_driver_data *idd)
  1709. {
  1710. struct ioc3_card *card_ptr = idd->data[is->id];
  1711. struct uart_port *the_port;
  1712. struct ioc3_port *port;
  1713. int ii;
  1714. if (card_ptr) {
  1715. for (ii = 0; ii < LOGICAL_PORTS_PER_CARD; ii++) {
  1716. the_port = &card_ptr->ic_port[GET_PHYSICAL_PORT(ii)].
  1717. icp_uart_port[GET_LOGICAL_PORT(ii)];
  1718. if (the_port)
  1719. uart_remove_one_port(&ioc3_uart, the_port);
  1720. port = card_ptr->ic_port[GET_PHYSICAL_PORT(ii)].icp_port;
  1721. if (port && IS_PHYSICAL_PORT(ii)
  1722. && (GET_PHYSICAL_PORT(ii) == 0)) {
  1723. pci_free_consistent(port->ip_idd->pdev,
  1724. TOTAL_RING_BUF_SIZE,
  1725. (void *)port->ip_cpu_ringbuf,
  1726. port->ip_dma_ringbuf);
  1727. kfree(port);
  1728. card_ptr->ic_port[GET_PHYSICAL_PORT(ii)].
  1729. icp_port = NULL;
  1730. }
  1731. }
  1732. kfree(card_ptr);
  1733. idd->data[is->id] = NULL;
  1734. }
  1735. return 0;
  1736. }
  1737. /**
  1738. * ioc3uart_probe - card probe function called from shim driver
  1739. * @is: submodule struct for this submodule
  1740. * @idd: ioc3 driver data for this card
  1741. */
  1742. static int
  1743. ioc3uart_probe(struct ioc3_submodule *is, struct ioc3_driver_data *idd)
  1744. {
  1745. struct pci_dev *pdev = idd->pdev;
  1746. struct ioc3_card *card_ptr;
  1747. int ret = 0;
  1748. struct ioc3_port *port;
  1749. struct ioc3_port *ports[PORTS_PER_CARD];
  1750. int phys_port;
  1751. int cnt;
  1752. DPRINT_CONFIG(("%s (0x%p, 0x%p)\n", __func__, is, idd));
  1753. card_ptr = kzalloc(sizeof(struct ioc3_card), GFP_KERNEL);
  1754. if (!card_ptr) {
  1755. printk(KERN_WARNING "ioc3_attach_one"
  1756. ": unable to get memory for the IOC3\n");
  1757. return -ENOMEM;
  1758. }
  1759. idd->data[is->id] = card_ptr;
  1760. Submodule_slot = is->id;
  1761. writel(((UARTA_BASE >> 3) << SIO_CR_SER_A_BASE_SHIFT) |
  1762. ((UARTB_BASE >> 3) << SIO_CR_SER_B_BASE_SHIFT) |
  1763. (0xf << SIO_CR_CMD_PULSE_SHIFT), &idd->vma->sio_cr);
  1764. pci_write_config_dword(pdev, PCI_LAT, 0xff00);
  1765. /* Enable serial port mode select generic PIO pins as outputs */
  1766. ioc3_gpcr_set(idd, GPCR_UARTA_MODESEL | GPCR_UARTB_MODESEL);
  1767. /* Create port structures for each port */
  1768. for (phys_port = 0; phys_port < PORTS_PER_CARD; phys_port++) {
  1769. port = kzalloc(sizeof(struct ioc3_port), GFP_KERNEL);
  1770. if (!port) {
  1771. printk(KERN_WARNING
  1772. "IOC3 serial memory not available for port\n");
  1773. ret = -ENOMEM;
  1774. goto out4;
  1775. }
  1776. spin_lock_init(&port->ip_lock);
  1777. /* we need to remember the previous ones, to point back to
  1778. * them farther down - setting up the ring buffers.
  1779. */
  1780. ports[phys_port] = port;
  1781. /* init to something useful */
  1782. card_ptr->ic_port[phys_port].icp_port = port;
  1783. port->ip_is = is;
  1784. port->ip_idd = idd;
  1785. port->ip_baud = 9600;
  1786. port->ip_card = card_ptr;
  1787. port->ip_hooks = &hooks_array[phys_port];
  1788. /* Setup each port */
  1789. if (phys_port == 0) {
  1790. port->ip_serial_regs = &idd->vma->port_a;
  1791. port->ip_uart_regs = &idd->vma->sregs.uarta;
  1792. DPRINT_CONFIG(("%s : Port A ip_serial_regs 0x%p "
  1793. "ip_uart_regs 0x%p\n",
  1794. __func__,
  1795. (void *)port->ip_serial_regs,
  1796. (void *)port->ip_uart_regs));
  1797. /* setup ring buffers */
  1798. port->ip_cpu_ringbuf = pci_alloc_consistent(pdev,
  1799. TOTAL_RING_BUF_SIZE, &port->ip_dma_ringbuf);
  1800. BUG_ON(!((((int64_t) port->ip_dma_ringbuf) &
  1801. (TOTAL_RING_BUF_SIZE - 1)) == 0));
  1802. port->ip_inring = RING(port, RX_A);
  1803. port->ip_outring = RING(port, TX_A);
  1804. DPRINT_CONFIG(("%s : Port A ip_cpu_ringbuf 0x%p "
  1805. "ip_dma_ringbuf 0x%p, ip_inring 0x%p "
  1806. "ip_outring 0x%p\n",
  1807. __func__,
  1808. (void *)port->ip_cpu_ringbuf,
  1809. (void *)port->ip_dma_ringbuf,
  1810. (void *)port->ip_inring,
  1811. (void *)port->ip_outring));
  1812. }
  1813. else {
  1814. port->ip_serial_regs = &idd->vma->port_b;
  1815. port->ip_uart_regs = &idd->vma->sregs.uartb;
  1816. DPRINT_CONFIG(("%s : Port B ip_serial_regs 0x%p "
  1817. "ip_uart_regs 0x%p\n",
  1818. __func__,
  1819. (void *)port->ip_serial_regs,
  1820. (void *)port->ip_uart_regs));
  1821. /* share the ring buffers */
  1822. port->ip_dma_ringbuf =
  1823. ports[phys_port - 1]->ip_dma_ringbuf;
  1824. port->ip_cpu_ringbuf =
  1825. ports[phys_port - 1]->ip_cpu_ringbuf;
  1826. port->ip_inring = RING(port, RX_B);
  1827. port->ip_outring = RING(port, TX_B);
  1828. DPRINT_CONFIG(("%s : Port B ip_cpu_ringbuf 0x%p "
  1829. "ip_dma_ringbuf 0x%p, ip_inring 0x%p "
  1830. "ip_outring 0x%p\n",
  1831. __func__,
  1832. (void *)port->ip_cpu_ringbuf,
  1833. (void *)port->ip_dma_ringbuf,
  1834. (void *)port->ip_inring,
  1835. (void *)port->ip_outring));
  1836. }
  1837. DPRINT_CONFIG(("%s : port %d [addr 0x%p] card_ptr 0x%p",
  1838. __func__,
  1839. phys_port, (void *)port, (void *)card_ptr));
  1840. DPRINT_CONFIG((" ip_serial_regs 0x%p ip_uart_regs 0x%p\n",
  1841. (void *)port->ip_serial_regs,
  1842. (void *)port->ip_uart_regs));
  1843. /* Initialize the hardware for IOC3 */
  1844. port_init(port);
  1845. DPRINT_CONFIG(("%s: phys_port %d port 0x%p inring 0x%p "
  1846. "outring 0x%p\n",
  1847. __func__,
  1848. phys_port, (void *)port,
  1849. (void *)port->ip_inring,
  1850. (void *)port->ip_outring));
  1851. }
  1852. /* register port with the serial core */
  1853. ret = ioc3_serial_core_attach(is, idd);
  1854. if (ret)
  1855. goto out4;
  1856. Num_of_ioc3_cards++;
  1857. return ret;
  1858. /* error exits that give back resources */
  1859. out4:
  1860. for (cnt = 0; cnt < phys_port; cnt++)
  1861. kfree(ports[cnt]);
  1862. kfree(card_ptr);
  1863. return ret;
  1864. }
  1865. static struct ioc3_submodule ioc3uart_ops = {
  1866. .name = "IOC3uart",
  1867. .probe = ioc3uart_probe,
  1868. .remove = ioc3uart_remove,
  1869. /* call .intr for both ports initially */
  1870. .irq_mask = SIO_IR_SA | SIO_IR_SB,
  1871. .intr = ioc3uart_intr,
  1872. .owner = THIS_MODULE,
  1873. };
  1874. /**
  1875. * ioc3_detect - module init called,
  1876. */
  1877. static int __init ioc3uart_init(void)
  1878. {
  1879. int ret;
  1880. /* register with serial core */
  1881. if ((ret = uart_register_driver(&ioc3_uart)) < 0) {
  1882. printk(KERN_WARNING
  1883. "%s: Couldn't register IOC3 uart serial driver\n",
  1884. __func__);
  1885. return ret;
  1886. }
  1887. ret = ioc3_register_submodule(&ioc3uart_ops);
  1888. if (ret)
  1889. uart_unregister_driver(&ioc3_uart);
  1890. return ret;
  1891. }
  1892. static void __exit ioc3uart_exit(void)
  1893. {
  1894. ioc3_unregister_submodule(&ioc3uart_ops);
  1895. uart_unregister_driver(&ioc3_uart);
  1896. }
  1897. module_init(ioc3uart_init);
  1898. module_exit(ioc3uart_exit);
  1899. MODULE_AUTHOR("Pat Gefre - Silicon Graphics Inc. (SGI) <pfg@sgi.com>");
  1900. MODULE_DESCRIPTION("Serial PCI driver module for SGI IOC3 card");
  1901. MODULE_LICENSE("GPL");