vlsi_ir.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*********************************************************************
  2. *
  3. * vlsi_ir.h: VLSI82C147 PCI IrDA controller driver for Linux
  4. *
  5. * Version: 0.5
  6. *
  7. * Copyright (c) 2001-2003 Martin Diehl
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. *
  24. ********************************************************************/
  25. #ifndef IRDA_VLSI_FIR_H
  26. #define IRDA_VLSI_FIR_H
  27. /* ================================================================
  28. * compatibility stuff
  29. */
  30. /* definitions not present in pci_ids.h */
  31. #ifndef PCI_CLASS_WIRELESS_IRDA
  32. #define PCI_CLASS_WIRELESS_IRDA 0x0d00
  33. #endif
  34. #ifndef PCI_CLASS_SUBCLASS_MASK
  35. #define PCI_CLASS_SUBCLASS_MASK 0xffff
  36. #endif
  37. /* in recent 2.5 interrupt handlers have non-void return value */
  38. #ifndef IRQ_RETVAL
  39. typedef void irqreturn_t;
  40. #define IRQ_NONE
  41. #define IRQ_HANDLED
  42. #define IRQ_RETVAL(x)
  43. #endif
  44. /* some stuff need to check kernelversion. Not all 2.5 stuff was present
  45. * in early 2.5.x - the test is merely to separate 2.4 from 2.5
  46. */
  47. #include <linux/version.h>
  48. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
  49. /* PDE() introduced in 2.5.4 */
  50. #ifdef CONFIG_PROC_FS
  51. #define PDE(inode) ((inode)->u.generic_ip)
  52. #endif
  53. /* irda crc16 calculation exported in 2.5.42 */
  54. #define irda_calc_crc16(fcs,buf,len) (GOOD_FCS)
  55. /* we use this for unified pci device name access */
  56. #define PCIDEV_NAME(pdev) ((pdev)->name)
  57. #else /* 2.5 or later */
  58. /* whatever we get from the associated struct device - bus:slot:dev.fn id */
  59. #define PCIDEV_NAME(pdev) (pci_name(pdev))
  60. #endif
  61. /* ================================================================ */
  62. /* non-standard PCI registers */
  63. enum vlsi_pci_regs {
  64. VLSI_PCI_CLKCTL = 0x40, /* chip clock input control */
  65. VLSI_PCI_MSTRPAGE = 0x41, /* addr [31:24] for all busmaster cycles */
  66. VLSI_PCI_IRMISC = 0x42 /* mainly legacy UART related */
  67. };
  68. /* ------------------------------------------ */
  69. /* VLSI_PCI_CLKCTL: Clock Control Register (u8, rw) */
  70. /* Three possible clock sources: either on-chip 48MHz PLL or
  71. * external clock applied to EXTCLK pin. External clock may
  72. * be either 48MHz or 40MHz, which is indicated by XCKSEL.
  73. * CLKSTP controls whether the selected clock source gets
  74. * connected to the IrDA block.
  75. *
  76. * On my HP OB-800 the BIOS sets external 40MHz clock as source
  77. * when IrDA enabled and I've never detected any PLL lock success.
  78. * Apparently the 14.3...MHz OSC input required for the PLL to work
  79. * is not connected and the 40MHz EXTCLK is provided externally.
  80. * At least this is what makes the driver working for me.
  81. */
  82. enum vlsi_pci_clkctl {
  83. /* PLL control */
  84. CLKCTL_PD_INV = 0x04, /* PD#: inverted power down signal,
  85. * i.e. PLL is powered, if PD_INV set */
  86. CLKCTL_LOCK = 0x40, /* (ro) set, if PLL is locked */
  87. /* clock source selection */
  88. CLKCTL_EXTCLK = 0x20, /* set to select external clock input, not PLL */
  89. CLKCTL_XCKSEL = 0x10, /* set to indicate EXTCLK is 40MHz, not 48MHz */
  90. /* IrDA block control */
  91. CLKCTL_CLKSTP = 0x80, /* set to disconnect from selected clock source */
  92. CLKCTL_WAKE = 0x08 /* set to enable wakeup feature: whenever IR activity
  93. * is detected, PD_INV gets set(?) and CLKSTP cleared */
  94. };
  95. /* ------------------------------------------ */
  96. /* VLSI_PCI_MSTRPAGE: Master Page Register (u8, rw) and busmastering stuff */
  97. #define DMA_MASK_USED_BY_HW 0xffffffff
  98. #define DMA_MASK_MSTRPAGE 0x00ffffff
  99. #define MSTRPAGE_VALUE (DMA_MASK_MSTRPAGE >> 24)
  100. /* PCI busmastering is somewhat special for this guy - in short:
  101. *
  102. * We select to operate using fixed MSTRPAGE=0, use ISA DMA
  103. * address restrictions to make the PCI BM api aware of this,
  104. * but ensure the hardware is dealing with real 32bit access.
  105. *
  106. * In detail:
  107. * The chip executes normal 32bit busmaster cycles, i.e.
  108. * drives all 32 address lines. These addresses however are
  109. * composed of [0:23] taken from various busaddr-pointers
  110. * and [24:31] taken from the MSTRPAGE register in the VLSI82C147
  111. * config space. Therefore _all_ busmastering must be
  112. * targeted to/from one single 16MB (busaddr-) superpage!
  113. * The point is to make sure all the allocations for memory
  114. * locations with busmaster access (ring descriptors, buffers)
  115. * are indeed bus-mappable to the same 16MB range (for x86 this
  116. * means they must reside in the same 16MB physical memory address
  117. * range). The only constraint we have which supports "several objects
  118. * mappable to common 16MB range" paradigma, is the old ISA DMA
  119. * restriction to the first 16MB of physical address range.
  120. * Hence the approach here is to enable PCI busmaster support using
  121. * the correct 32bit dma-mask used by the chip. Afterwards the device's
  122. * dma-mask gets restricted to 24bit, which must be honoured somehow by
  123. * all allocations for memory areas to be exposed to the chip ...
  124. *
  125. * Note:
  126. * Don't be surprised to get "Setting latency timer..." messages every
  127. * time when PCI busmastering is enabled for the chip.
  128. * The chip has its PCI latency timer RO fixed at 0 - which is not a
  129. * problem here, because it is never requesting _burst_ transactions.
  130. */
  131. /* ------------------------------------------ */
  132. /* VLSI_PCIIRMISC: IR Miscellaneous Register (u8, rw) */
  133. /* legacy UART emulation - not used by this driver - would require:
  134. * (see below for some register-value definitions)
  135. *
  136. * - IRMISC_UARTEN must be set to enable UART address decoding
  137. * - IRMISC_UARTSEL configured
  138. * - IRCFG_MASTER must be cleared
  139. * - IRCFG_SIR must be set
  140. * - IRENABLE_PHYANDCLOCK must be asserted 0->1 (and hence IRENABLE_SIR_ON)
  141. */
  142. enum vlsi_pci_irmisc {
  143. /* IR transceiver control */
  144. IRMISC_IRRAIL = 0x40, /* (ro?) IR rail power indication (and control?)
  145. * 0=3.3V / 1=5V. Probably set during power-on?
  146. * unclear - not touched by driver */
  147. IRMISC_IRPD = 0x08, /* transceiver power down, if set */
  148. /* legacy UART control */
  149. IRMISC_UARTTST = 0x80, /* UART test mode - "always write 0" */
  150. IRMISC_UARTEN = 0x04, /* enable UART address decoding */
  151. /* bits [1:0] IRMISC_UARTSEL to select legacy UART address */
  152. IRMISC_UARTSEL_3f8 = 0x00,
  153. IRMISC_UARTSEL_2f8 = 0x01,
  154. IRMISC_UARTSEL_3e8 = 0x02,
  155. IRMISC_UARTSEL_2e8 = 0x03
  156. };
  157. /* ================================================================ */
  158. /* registers mapped to 32 byte PCI IO space */
  159. /* note: better access all registers at the indicated u8/u16 size
  160. * although some of them contain only 1 byte of information.
  161. * some of them (particaluarly PROMPT and IRCFG) ignore
  162. * access when using the wrong addressing mode!
  163. */
  164. enum vlsi_pio_regs {
  165. VLSI_PIO_IRINTR = 0x00, /* interrupt enable/request (u8, rw) */
  166. VLSI_PIO_RINGPTR = 0x02, /* rx/tx ring pointer (u16, ro) */
  167. VLSI_PIO_RINGBASE = 0x04, /* [23:10] of ring address (u16, rw) */
  168. VLSI_PIO_RINGSIZE = 0x06, /* rx/tx ring size (u16, rw) */
  169. VLSI_PIO_PROMPT = 0x08, /* triggers ring processing (u16, wo) */
  170. /* 0x0a-0x0f: reserved / duplicated UART regs */
  171. VLSI_PIO_IRCFG = 0x10, /* configuration select (u16, rw) */
  172. VLSI_PIO_SIRFLAG = 0x12, /* BOF/EOF for filtered SIR (u16, ro) */
  173. VLSI_PIO_IRENABLE = 0x14, /* enable and status register (u16, rw/ro) */
  174. VLSI_PIO_PHYCTL = 0x16, /* physical layer current status (u16, ro) */
  175. VLSI_PIO_NPHYCTL = 0x18, /* next physical layer select (u16, rw) */
  176. VLSI_PIO_MAXPKT = 0x1a, /* [11:0] max len for packet receive (u16, rw) */
  177. VLSI_PIO_RCVBCNT = 0x1c /* current receive-FIFO byte count (u16, ro) */
  178. /* 0x1e-0x1f: reserved / duplicated UART regs */
  179. };
  180. /* ------------------------------------------ */
  181. /* VLSI_PIO_IRINTR: Interrupt Register (u8, rw) */
  182. /* enable-bits:
  183. * 1 = enable / 0 = disable
  184. * interrupt condition bits:
  185. * set according to corresponding interrupt source
  186. * (regardless of the state of the enable bits)
  187. * enable bit status indicates whether interrupt gets raised
  188. * write-to-clear
  189. * note: RPKTINT and TPKTINT behave different in legacy UART mode (which we don't use :-)
  190. */
  191. enum vlsi_pio_irintr {
  192. IRINTR_ACTEN = 0x80, /* activity interrupt enable */
  193. IRINTR_ACTIVITY = 0x40, /* activity monitor (traffic detected) */
  194. IRINTR_RPKTEN = 0x20, /* receive packet interrupt enable*/
  195. IRINTR_RPKTINT = 0x10, /* rx-packet transfered from fifo to memory finished */
  196. IRINTR_TPKTEN = 0x08, /* transmit packet interrupt enable */
  197. IRINTR_TPKTINT = 0x04, /* last bit of tx-packet+crc shifted to ir-pulser */
  198. IRINTR_OE_EN = 0x02, /* UART rx fifo overrun error interrupt enable */
  199. IRINTR_OE_INT = 0x01 /* UART rx fifo overrun error (read LSR to clear) */
  200. };
  201. /* we use this mask to check whether the (shared PCI) interrupt is ours */
  202. #define IRINTR_INT_MASK (IRINTR_ACTIVITY|IRINTR_RPKTINT|IRINTR_TPKTINT)
  203. /* ------------------------------------------ */
  204. /* VLSI_PIO_RINGPTR: Ring Pointer Read-Back Register (u16, ro) */
  205. /* _both_ ring pointers are indices relative to the _entire_ rx,tx-ring!
  206. * i.e. the referenced descriptor is located
  207. * at RINGBASE + PTR * sizeof(descr) for rx and tx
  208. * therefore, the tx-pointer has offset MAX_RING_DESCR
  209. */
  210. #define MAX_RING_DESCR 64 /* tx, rx rings may contain up to 64 descr each */
  211. #define RINGPTR_RX_MASK (MAX_RING_DESCR-1)
  212. #define RINGPTR_TX_MASK ((MAX_RING_DESCR-1)<<8)
  213. #define RINGPTR_GET_RX(p) ((p)&RINGPTR_RX_MASK)
  214. #define RINGPTR_GET_TX(p) (((p)&RINGPTR_TX_MASK)>>8)
  215. /* ------------------------------------------ */
  216. /* VLSI_PIO_RINGBASE: Ring Pointer Base Address Register (u16, ro) */
  217. /* Contains [23:10] part of the ring base (bus-) address
  218. * which must be 1k-alinged. [31:24] is taken from
  219. * VLSI_PCI_MSTRPAGE above.
  220. * The controller initiates non-burst PCI BM cycles to
  221. * fetch and update the descriptors in the ring.
  222. * Once fetched, the descriptor remains cached onchip
  223. * until it gets closed and updated due to the ring
  224. * processing state machine.
  225. * The entire ring area is split in rx and tx areas with each
  226. * area consisting of 64 descriptors of 8 bytes each.
  227. * The rx(tx) ring is located at ringbase+0 (ringbase+64*8).
  228. */
  229. #define BUS_TO_RINGBASE(p) (((p)>>10)&0x3fff)
  230. /* ------------------------------------------ */
  231. /* VLSI_PIO_RINGSIZE: Ring Size Register (u16, rw) */
  232. /* bit mask to indicate the ring size to be used for rx and tx.
  233. * possible values encoded bits
  234. * 4 0000
  235. * 8 0001
  236. * 16 0011
  237. * 32 0111
  238. * 64 1111
  239. * located at [15:12] for tx and [11:8] for rx ([7:0] unused)
  240. *
  241. * note: probably a good idea to have IRCFG_MSTR cleared when writing
  242. * this so the state machines are stopped and the RINGPTR is reset!
  243. */
  244. #define SIZE_TO_BITS(num) ((((num)-1)>>2)&0x0f)
  245. #define TX_RX_TO_RINGSIZE(tx,rx) ((SIZE_TO_BITS(tx)<<12)|(SIZE_TO_BITS(rx)<<8))
  246. #define RINGSIZE_TO_RXSIZE(rs) ((((rs)&0x0f00)>>6)+4)
  247. #define RINGSIZE_TO_TXSIZE(rs) ((((rs)&0xf000)>>10)+4)
  248. /* ------------------------------------------ */
  249. /* VLSI_PIO_PROMPT: Ring Prompting Register (u16, write-to-start) */
  250. /* writing any value kicks the ring processing state machines
  251. * for both tx, rx rings as follows:
  252. * - active rings (currently owning an active descriptor)
  253. * ignore the prompt and continue
  254. * - idle rings fetch the next descr from the ring and start
  255. * their processing
  256. */
  257. /* ------------------------------------------ */
  258. /* VLSI_PIO_IRCFG: IR Config Register (u16, rw) */
  259. /* notes:
  260. * - not more than one SIR/MIR/FIR bit must be set at any time
  261. * - SIR, MIR, FIR and CRC16 select the configuration which will
  262. * be applied on next 0->1 transition of IRENABLE_PHYANDCLOCK (see below).
  263. * - besides allowing the PCI interface to execute busmaster cycles
  264. * and therefore the ring SM to operate, the MSTR bit has side-effects:
  265. * when MSTR is cleared, the RINGPTR's get reset and the legacy UART mode
  266. * (in contrast to busmaster access mode) gets enabled.
  267. * - clearing ENRX or setting ENTX while data is received may stall the
  268. * receive fifo until ENRX reenabled _and_ another packet arrives
  269. * - SIRFILT means the chip performs the required unwrapping of hardware
  270. * headers (XBOF's, BOF/EOF) and un-escaping in the _receive_ direction.
  271. * Only the resulting IrLAP payload is copied to the receive buffers -
  272. * but with the 16bit FCS still encluded. Question remains, whether it
  273. * was already checked or we should do it before passing the packet to IrLAP?
  274. */
  275. enum vlsi_pio_ircfg {
  276. IRCFG_LOOP = 0x4000, /* enable loopback test mode */
  277. IRCFG_ENTX = 0x1000, /* transmit enable */
  278. IRCFG_ENRX = 0x0800, /* receive enable */
  279. IRCFG_MSTR = 0x0400, /* master enable */
  280. IRCFG_RXANY = 0x0200, /* receive any packet */
  281. IRCFG_CRC16 = 0x0080, /* 16bit (not 32bit) CRC select for MIR/FIR */
  282. IRCFG_FIR = 0x0040, /* FIR 4PPM encoding mode enable */
  283. IRCFG_MIR = 0x0020, /* MIR HDLC encoding mode enable */
  284. IRCFG_SIR = 0x0010, /* SIR encoding mode enable */
  285. IRCFG_SIRFILT = 0x0008, /* enable SIR decode filter (receiver unwrapping) */
  286. IRCFG_SIRTEST = 0x0004, /* allow SIR decode filter when not in SIR mode */
  287. IRCFG_TXPOL = 0x0002, /* invert tx polarity when set */
  288. IRCFG_RXPOL = 0x0001 /* invert rx polarity when set */
  289. };
  290. /* ------------------------------------------ */
  291. /* VLSI_PIO_SIRFLAG: SIR Flag Register (u16, ro) */
  292. /* register contains hardcoded BOF=0xc0 at [7:0] and EOF=0xc1 at [15:8]
  293. * which is used for unwrapping received frames in SIR decode-filter mode
  294. */
  295. /* ------------------------------------------ */
  296. /* VLSI_PIO_IRENABLE: IR Enable Register (u16, rw/ro) */
  297. /* notes:
  298. * - IREN acts as gate for latching the configured IR mode information
  299. * from IRCFG and IRPHYCTL when IREN=reset and applying them when
  300. * IREN gets set afterwards.
  301. * - ENTXST reflects IRCFG_ENTX
  302. * - ENRXST = IRCFG_ENRX && (!IRCFG_ENTX || IRCFG_LOOP)
  303. */
  304. enum vlsi_pio_irenable {
  305. IRENABLE_PHYANDCLOCK = 0x8000, /* enable IR phy and gate the mode config (rw) */
  306. IRENABLE_CFGER = 0x4000, /* mode configuration error (ro) */
  307. IRENABLE_FIR_ON = 0x2000, /* FIR on status (ro) */
  308. IRENABLE_MIR_ON = 0x1000, /* MIR on status (ro) */
  309. IRENABLE_SIR_ON = 0x0800, /* SIR on status (ro) */
  310. IRENABLE_ENTXST = 0x0400, /* transmit enable status (ro) */
  311. IRENABLE_ENRXST = 0x0200, /* Receive enable status (ro) */
  312. IRENABLE_CRC16_ON = 0x0100 /* 16bit (not 32bit) CRC enabled status (ro) */
  313. };
  314. #define IRENABLE_MASK 0xff00 /* Read mask */
  315. /* ------------------------------------------ */
  316. /* VLSI_PIO_PHYCTL: IR Physical Layer Current Control Register (u16, ro) */
  317. /* read-back of the currently applied physical layer status.
  318. * applied from VLSI_PIO_NPHYCTL at rising edge of IRENABLE_PHYANDCLOCK
  319. * contents identical to VLSI_PIO_NPHYCTL (see below)
  320. */
  321. /* ------------------------------------------ */
  322. /* VLSI_PIO_NPHYCTL: IR Physical Layer Next Control Register (u16, rw) */
  323. /* latched during IRENABLE_PHYANDCLOCK=0 and applied at 0-1 transition
  324. *
  325. * consists of BAUD[15:10], PLSWID[9:5] and PREAMB[4:0] bits defined as follows:
  326. *
  327. * SIR-mode: BAUD = (115.2kHz / baudrate) - 1
  328. * PLSWID = (pulsetime * freq / (BAUD+1)) - 1
  329. * where pulsetime is the requested IrPHY pulse width
  330. * and freq is 8(16)MHz for 40(48)MHz primary input clock
  331. * PREAMB: don't care for SIR
  332. *
  333. * The nominal SIR pulse width is 3/16 bit time so we have PLSWID=12
  334. * fixed for all SIR speeds at 40MHz input clock (PLSWID=24 at 48MHz).
  335. * IrPHY also allows shorter pulses down to the nominal pulse duration
  336. * at 115.2kbaud (minus some tolerance) which is 1.41 usec.
  337. * Using the expression PLSWID = 12/(BAUD+1)-1 (multiplied by two for 48MHz)
  338. * we get the minimum acceptable PLSWID values according to the VLSI
  339. * specification, which provides 1.5 usec pulse width for all speeds (except
  340. * for 2.4kbaud getting 6usec). This is fine with IrPHY v1.3 specs and
  341. * reduces the transceiver power which drains the battery. At 9.6kbaud for
  342. * example this amounts to more than 90% battery power saving!
  343. *
  344. * MIR-mode: BAUD = 0
  345. * PLSWID = 9(10) for 40(48) MHz input clock
  346. * to get nominal MIR pulse width
  347. * PREAMB = 1
  348. *
  349. * FIR-mode: BAUD = 0
  350. * PLSWID: don't care
  351. * PREAMB = 15
  352. */
  353. #define PHYCTL_BAUD_SHIFT 10
  354. #define PHYCTL_BAUD_MASK 0xfc00
  355. #define PHYCTL_PLSWID_SHIFT 5
  356. #define PHYCTL_PLSWID_MASK 0x03e0
  357. #define PHYCTL_PREAMB_SHIFT 0
  358. #define PHYCTL_PREAMB_MASK 0x001f
  359. #define PHYCTL_TO_BAUD(bwp) (((bwp)&PHYCTL_BAUD_MASK)>>PHYCTL_BAUD_SHIFT)
  360. #define PHYCTL_TO_PLSWID(bwp) (((bwp)&PHYCTL_PLSWID_MASK)>>PHYCTL_PLSWID_SHIFT)
  361. #define PHYCTL_TO_PREAMB(bwp) (((bwp)&PHYCTL_PREAMB_MASK)>>PHYCTL_PREAMB_SHIFT)
  362. #define BWP_TO_PHYCTL(b,w,p) ((((b)<<PHYCTL_BAUD_SHIFT)&PHYCTL_BAUD_MASK) \
  363. | (((w)<<PHYCTL_PLSWID_SHIFT)&PHYCTL_PLSWID_MASK) \
  364. | (((p)<<PHYCTL_PREAMB_SHIFT)&PHYCTL_PREAMB_MASK))
  365. #define BAUD_BITS(br) ((115200/(br))-1)
  366. static inline unsigned
  367. calc_width_bits(unsigned baudrate, unsigned widthselect, unsigned clockselect)
  368. {
  369. unsigned tmp;
  370. if (widthselect) /* nominal 3/16 puls width */
  371. return (clockselect) ? 12 : 24;
  372. tmp = ((clockselect) ? 12 : 24) / (BAUD_BITS(baudrate)+1);
  373. /* intermediate result of integer division needed here */
  374. return (tmp>0) ? (tmp-1) : 0;
  375. }
  376. #define PHYCTL_SIR(br,ws,cs) BWP_TO_PHYCTL(BAUD_BITS(br),calc_width_bits((br),(ws),(cs)),0)
  377. #define PHYCTL_MIR(cs) BWP_TO_PHYCTL(0,((cs)?9:10),1)
  378. #define PHYCTL_FIR BWP_TO_PHYCTL(0,0,15)
  379. /* quite ugly, I know. But implementing these calculations here avoids
  380. * having magic numbers in the code and allows some playing with pulsewidths
  381. * without risk to violate the standards.
  382. * FWIW, here is the table for reference:
  383. *
  384. * baudrate BAUD min-PLSWID nom-PLSWID PREAMB
  385. * 2400 47 0(0) 12(24) 0
  386. * 9600 11 0(0) 12(24) 0
  387. * 19200 5 1(2) 12(24) 0
  388. * 38400 2 3(6) 12(24) 0
  389. * 57600 1 5(10) 12(24) 0
  390. * 115200 0 11(22) 12(24) 0
  391. * MIR 0 - 9(10) 1
  392. * FIR 0 - 0 15
  393. *
  394. * note: x(y) means x-value for 40MHz / y-value for 48MHz primary input clock
  395. */
  396. /* ------------------------------------------ */
  397. /* VLSI_PIO_MAXPKT: Maximum Packet Length register (u16, rw) */
  398. /* maximum acceptable length for received packets */
  399. /* hw imposed limitation - register uses only [11:0] */
  400. #define MAX_PACKET_LENGTH 0x0fff
  401. /* IrLAP I-field (apparently not defined elsewhere) */
  402. #define IRDA_MTU 2048
  403. /* complete packet consists of A(1)+C(1)+I(<=IRDA_MTU) */
  404. #define IRLAP_SKB_ALLOCSIZE (1+1+IRDA_MTU)
  405. /* the buffers we use to exchange frames with the hardware need to be
  406. * larger than IRLAP_SKB_ALLOCSIZE because we may have up to 4 bytes FCS
  407. * appended and, in SIR mode, a lot of frame wrapping bytes. The worst
  408. * case appears to be a SIR packet with I-size==IRDA_MTU and all bytes
  409. * requiring to be escaped to provide transparency. Furthermore, the peer
  410. * might ask for quite a number of additional XBOFs:
  411. * up to 115+48 XBOFS 163
  412. * regular BOF 1
  413. * A-field 1
  414. * C-field 1
  415. * I-field, IRDA_MTU, all escaped 4096
  416. * FCS (16 bit at SIR, escaped) 4
  417. * EOF 1
  418. * AFAICS nothing in IrLAP guarantees A/C field not to need escaping
  419. * (f.e. 0xc0/0xc1 - i.e. BOF/EOF - are legal values there) so in the
  420. * worst case we have 4269 bytes total frame size.
  421. * However, the VLSI uses 12 bits only for all buffer length values,
  422. * which limits the maximum useable buffer size <= 4095.
  423. * Note this is not a limitation in the receive case because we use
  424. * the SIR filtering mode where the hw unwraps the frame and only the
  425. * bare packet+fcs is stored into the buffer - in contrast to the SIR
  426. * tx case where we have to pass frame-wrapped packets to the hw.
  427. * If this would ever become an issue in real life, the only workaround
  428. * I see would be using the legacy UART emulation in SIR mode.
  429. */
  430. #define XFER_BUF_SIZE MAX_PACKET_LENGTH
  431. /* ------------------------------------------ */
  432. /* VLSI_PIO_RCVBCNT: Receive Byte Count Register (u16, ro) */
  433. /* receive packet counter gets incremented on every non-filtered
  434. * byte which was put in the receive fifo and reset for each
  435. * new packet. Used to decide whether we are just in the middle
  436. * of receiving
  437. */
  438. /* better apply the [11:0] mask when reading, as some docs say the
  439. * reserved [15:12] would return 1 when reading - which is wrong AFAICS
  440. */
  441. #define RCVBCNT_MASK 0x0fff
  442. /******************************************************************/
  443. /* descriptors for rx/tx ring
  444. *
  445. * accessed by hardware - don't change!
  446. *
  447. * the descriptor is owned by hardware, when the ACTIVE status bit
  448. * is set and nothing (besides reading status to test the bit)
  449. * shall be done. The bit gets cleared by hw, when the descriptor
  450. * gets closed. Premature reaping of descriptors owned be the chip
  451. * can be achieved by disabling IRCFG_MSTR
  452. *
  453. * Attention: Writing addr overwrites status!
  454. *
  455. * ### FIXME: depends on endianess (but there ain't no non-i586 ob800 ;-)
  456. */
  457. struct ring_descr_hw {
  458. volatile u16 rd_count; /* tx/rx count [11:0] */
  459. u16 reserved;
  460. union {
  461. u32 addr; /* [23:0] of the buffer's busaddress */
  462. struct {
  463. u8 addr_res[3];
  464. volatile u8 status; /* descriptor status */
  465. } rd_s __attribute__((packed));
  466. } rd_u __attribute((packed));
  467. } __attribute__ ((packed));
  468. #define rd_addr rd_u.addr
  469. #define rd_status rd_u.rd_s.status
  470. /* ring descriptor status bits */
  471. #define RD_ACTIVE 0x80 /* descriptor owned by hw (both TX,RX) */
  472. /* TX ring descriptor status */
  473. #define RD_TX_DISCRC 0x40 /* do not send CRC (for SIR) */
  474. #define RD_TX_BADCRC 0x20 /* force a bad CRC */
  475. #define RD_TX_PULSE 0x10 /* send indication pulse after this frame (MIR/FIR) */
  476. #define RD_TX_FRCEUND 0x08 /* force underrun */
  477. #define RD_TX_CLRENTX 0x04 /* clear ENTX after this frame */
  478. #define RD_TX_UNDRN 0x01 /* TX fifo underrun (probably PCI problem) */
  479. /* RX ring descriptor status */
  480. #define RD_RX_PHYERR 0x40 /* physical encoding error */
  481. #define RD_RX_CRCERR 0x20 /* CRC error (MIR/FIR) */
  482. #define RD_RX_LENGTH 0x10 /* frame exceeds buffer length */
  483. #define RD_RX_OVER 0x08 /* RX fifo overrun (probably PCI problem) */
  484. #define RD_RX_SIRBAD 0x04 /* EOF missing: BOF follows BOF (SIR, filtered) */
  485. #define RD_RX_ERROR 0x7c /* any error in received frame */
  486. /* the memory required to hold the 2 descriptor rings */
  487. #define HW_RING_AREA_SIZE (2 * MAX_RING_DESCR * sizeof(struct ring_descr_hw))
  488. /******************************************************************/
  489. /* sw-ring descriptors consists of a bus-mapped transfer buffer with
  490. * associated skb and a pointer to the hw entry descriptor
  491. */
  492. struct ring_descr {
  493. struct ring_descr_hw *hw;
  494. struct sk_buff *skb;
  495. void *buf;
  496. };
  497. /* wrappers for operations on hw-exposed ring descriptors
  498. * access to the hw-part of the descriptors must use these.
  499. */
  500. static inline int rd_is_active(struct ring_descr *rd)
  501. {
  502. return ((rd->hw->rd_status & RD_ACTIVE) != 0);
  503. }
  504. static inline void rd_activate(struct ring_descr *rd)
  505. {
  506. rd->hw->rd_status |= RD_ACTIVE;
  507. }
  508. static inline void rd_set_status(struct ring_descr *rd, u8 s)
  509. {
  510. rd->hw->rd_status = s; /* may pass ownership to the hardware */
  511. }
  512. static inline void rd_set_addr_status(struct ring_descr *rd, dma_addr_t a, u8 s)
  513. {
  514. /* order is important for two reasons:
  515. * - overlayed: writing addr overwrites status
  516. * - we want to write status last so we have valid address in
  517. * case status has RD_ACTIVE set
  518. */
  519. if ((a & ~DMA_MASK_MSTRPAGE)>>24 != MSTRPAGE_VALUE) {
  520. IRDA_ERROR("%s: pci busaddr inconsistency!\n", __FUNCTION__);
  521. dump_stack();
  522. return;
  523. }
  524. a &= DMA_MASK_MSTRPAGE; /* clear highbyte to make sure we won't write
  525. * to status - just in case MSTRPAGE_VALUE!=0
  526. */
  527. rd->hw->rd_addr = cpu_to_le32(a);
  528. wmb();
  529. rd_set_status(rd, s); /* may pass ownership to the hardware */
  530. }
  531. static inline void rd_set_count(struct ring_descr *rd, u16 c)
  532. {
  533. rd->hw->rd_count = cpu_to_le16(c);
  534. }
  535. static inline u8 rd_get_status(struct ring_descr *rd)
  536. {
  537. return rd->hw->rd_status;
  538. }
  539. static inline dma_addr_t rd_get_addr(struct ring_descr *rd)
  540. {
  541. dma_addr_t a;
  542. a = le32_to_cpu(rd->hw->rd_addr);
  543. return (a & DMA_MASK_MSTRPAGE) | (MSTRPAGE_VALUE << 24);
  544. }
  545. static inline u16 rd_get_count(struct ring_descr *rd)
  546. {
  547. return le16_to_cpu(rd->hw->rd_count);
  548. }
  549. /******************************************************************/
  550. /* sw descriptor rings for rx, tx:
  551. *
  552. * operations follow producer-consumer paradigm, with the hw
  553. * in the middle doing the processing.
  554. * ring size must be power of two.
  555. *
  556. * producer advances r->tail after inserting for processing
  557. * consumer advances r->head after removing processed rd
  558. * ring is empty if head==tail / full if (tail+1)==head
  559. */
  560. struct vlsi_ring {
  561. struct pci_dev *pdev;
  562. int dir;
  563. unsigned len;
  564. unsigned size;
  565. unsigned mask;
  566. atomic_t head, tail;
  567. struct ring_descr *rd;
  568. };
  569. /* ring processing helpers */
  570. static inline struct ring_descr *ring_last(struct vlsi_ring *r)
  571. {
  572. int t;
  573. t = atomic_read(&r->tail) & r->mask;
  574. return (((t+1) & r->mask) == (atomic_read(&r->head) & r->mask)) ? NULL : &r->rd[t];
  575. }
  576. static inline struct ring_descr *ring_put(struct vlsi_ring *r)
  577. {
  578. atomic_inc(&r->tail);
  579. return ring_last(r);
  580. }
  581. static inline struct ring_descr *ring_first(struct vlsi_ring *r)
  582. {
  583. int h;
  584. h = atomic_read(&r->head) & r->mask;
  585. return (h == (atomic_read(&r->tail) & r->mask)) ? NULL : &r->rd[h];
  586. }
  587. static inline struct ring_descr *ring_get(struct vlsi_ring *r)
  588. {
  589. atomic_inc(&r->head);
  590. return ring_first(r);
  591. }
  592. /******************************************************************/
  593. /* our private compound VLSI-PCI-IRDA device information */
  594. typedef struct vlsi_irda_dev {
  595. struct pci_dev *pdev;
  596. struct net_device_stats stats;
  597. struct irlap_cb *irlap;
  598. struct qos_info qos;
  599. unsigned mode;
  600. int baud, new_baud;
  601. dma_addr_t busaddr;
  602. void *virtaddr;
  603. struct vlsi_ring *tx_ring, *rx_ring;
  604. struct timeval last_rx;
  605. spinlock_t lock;
  606. struct semaphore sem;
  607. u8 resume_ok;
  608. struct proc_dir_entry *proc_entry;
  609. } vlsi_irda_dev_t;
  610. /********************************************************/
  611. /* the remapped error flags we use for returning from frame
  612. * post-processing in vlsi_process_tx/rx() after it was completed
  613. * by the hardware. These functions either return the >=0 number
  614. * of transfered bytes in case of success or the negative (-)
  615. * of the or'ed error flags.
  616. */
  617. #define VLSI_TX_DROP 0x0001
  618. #define VLSI_TX_FIFO 0x0002
  619. #define VLSI_RX_DROP 0x0100
  620. #define VLSI_RX_OVER 0x0200
  621. #define VLSI_RX_LENGTH 0x0400
  622. #define VLSI_RX_FRAME 0x0800
  623. #define VLSI_RX_CRC 0x1000
  624. /********************************************************/
  625. #endif /* IRDA_VLSI_FIR_H */