ioc3_serial.c 57 KB

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