ioc3_serial.c 57 KB

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