bpp.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /*
  2. * drivers/sbus/char/bpp.c
  3. *
  4. * Copyright (c) 1995 Picture Elements
  5. * Stephen Williams (steve@icarus.com)
  6. * Gus Baldauf (gbaldauf@ix.netcom.com)
  7. *
  8. * Linux/SPARC port by Peter Zaitcev.
  9. * Integration into SPARC tree by Tom Dyas.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/fs.h>
  14. #include <linux/errno.h>
  15. #include <linux/sched.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/timer.h>
  18. #include <linux/ioport.h>
  19. #include <linux/major.h>
  20. #include <linux/smp_lock.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/io.h>
  23. #if defined(__i386__)
  24. # include <asm/system.h>
  25. #endif
  26. #if defined(__sparc__)
  27. # include <linux/init.h>
  28. # include <linux/delay.h> /* udelay() */
  29. # include <asm/oplib.h> /* OpenProm Library */
  30. # include <asm/sbus.h>
  31. #endif
  32. #include <asm/bpp.h>
  33. #define BPP_PROBE_CODE 0x55
  34. #define BPP_DELAY 100
  35. static const unsigned BPP_MAJOR = LP_MAJOR;
  36. static const char *bpp_dev_name = "bpp";
  37. /* When switching from compatibility to a mode where I can read, try
  38. the following mode first. */
  39. /* const unsigned char DEFAULT_ECP = 0x10; */
  40. static const unsigned char DEFAULT_ECP = 0x30;
  41. static const unsigned char DEFAULT_NIBBLE = 0x00;
  42. /*
  43. * These are 1284 time constraints, in units of jiffies.
  44. */
  45. static const unsigned long TIME_PSetup = 1;
  46. static const unsigned long TIME_PResponse = 6;
  47. static const unsigned long TIME_IDLE_LIMIT = 2000;
  48. /*
  49. * One instance per supported subdevice...
  50. */
  51. # define BPP_NO 3
  52. enum IEEE_Mode { COMPATIBILITY, NIBBLE, ECP, ECP_RLE, EPP };
  53. struct inst {
  54. unsigned present : 1; /* True if the hardware exists */
  55. unsigned enhanced : 1; /* True if the hardware in "enhanced" */
  56. unsigned opened : 1; /* True if the device is opened already */
  57. unsigned run_flag : 1; /* True if waiting for a repeate byte */
  58. unsigned char direction; /* 0 --> out, 0x20 --> IN */
  59. unsigned char pp_state; /* State of host controlled pins. */
  60. enum IEEE_Mode mode;
  61. unsigned char run_length;
  62. unsigned char repeat_byte;
  63. };
  64. static struct inst instances[BPP_NO];
  65. #if defined(__i386__)
  66. static const unsigned short base_addrs[BPP_NO] = { 0x278, 0x378, 0x3bc };
  67. /*
  68. * These are for data access.
  69. * Control lines accesses are hidden in set_bits() and get_bits().
  70. * The exception is the probe procedure, which is system-dependent.
  71. */
  72. #define bpp_outb_p(data, base) outb_p((data), (base))
  73. #define bpp_inb(base) inb(base)
  74. #define bpp_inb_p(base) inb_p(base)
  75. /*
  76. * This method takes the pin values mask and sets the hardware pins to
  77. * the requested value: 1 == high voltage, 0 == low voltage. This
  78. * burries the annoying PC bit inversion and preserves the direction
  79. * flag.
  80. */
  81. static void set_pins(unsigned short pins, unsigned minor)
  82. {
  83. unsigned char bits = instances[minor].direction; /* == 0x20 */
  84. if (! (pins & BPP_PP_nStrobe)) bits |= 1;
  85. if (! (pins & BPP_PP_nAutoFd)) bits |= 2;
  86. if ( pins & BPP_PP_nInit) bits |= 4;
  87. if (! (pins & BPP_PP_nSelectIn)) bits |= 8;
  88. instances[minor].pp_state = bits;
  89. outb_p(bits, base_addrs[minor]+2);
  90. }
  91. static unsigned short get_pins(unsigned minor)
  92. {
  93. unsigned short bits = 0;
  94. unsigned value = instances[minor].pp_state;
  95. if (! (value & 0x01)) bits |= BPP_PP_nStrobe;
  96. if (! (value & 0x02)) bits |= BPP_PP_nAutoFd;
  97. if (value & 0x04) bits |= BPP_PP_nInit;
  98. if (! (value & 0x08)) bits |= BPP_PP_nSelectIn;
  99. value = inb_p(base_addrs[minor]+1);
  100. if (value & 0x08) bits |= BPP_GP_nFault;
  101. if (value & 0x10) bits |= BPP_GP_Select;
  102. if (value & 0x20) bits |= BPP_GP_PError;
  103. if (value & 0x40) bits |= BPP_GP_nAck;
  104. if (! (value & 0x80)) bits |= BPP_GP_Busy;
  105. return bits;
  106. }
  107. #endif /* __i386__ */
  108. #if defined(__sparc__)
  109. /*
  110. * Register block
  111. */
  112. /* DMA registers */
  113. #define BPP_CSR 0x00
  114. #define BPP_ADDR 0x04
  115. #define BPP_BCNT 0x08
  116. #define BPP_TST_CSR 0x0C
  117. /* Parallel Port registers */
  118. #define BPP_HCR 0x10
  119. #define BPP_OCR 0x12
  120. #define BPP_DR 0x14
  121. #define BPP_TCR 0x15
  122. #define BPP_OR 0x16
  123. #define BPP_IR 0x17
  124. #define BPP_ICR 0x18
  125. #define BPP_SIZE 0x1A
  126. /* BPP_CSR. Bits of type RW1 are cleared with writing '1'. */
  127. #define P_DEV_ID_MASK 0xf0000000 /* R */
  128. #define P_DEV_ID_ZEBRA 0x40000000
  129. #define P_DEV_ID_L64854 0xa0000000 /* == NCR 89C100+89C105. Pity. */
  130. #define P_NA_LOADED 0x08000000 /* R NA wirtten but was not used */
  131. #define P_A_LOADED 0x04000000 /* R */
  132. #define P_DMA_ON 0x02000000 /* R DMA is not disabled */
  133. #define P_EN_NEXT 0x01000000 /* RW */
  134. #define P_TCI_DIS 0x00800000 /* RW TCI forbidden from interrupts */
  135. #define P_DIAG 0x00100000 /* RW Disables draining and resetting
  136. of P-FIFO on loading of P_ADDR*/
  137. #define P_BURST_SIZE 0x000c0000 /* RW SBus burst size */
  138. #define P_BURST_8 0x00000000
  139. #define P_BURST_4 0x00040000
  140. #define P_BURST_1 0x00080000 /* "No burst" write */
  141. #define P_TC 0x00004000 /* RW1 Term Count, can be cleared when
  142. P_EN_NEXT=1 */
  143. #define P_EN_CNT 0x00002000 /* RW */
  144. #define P_EN_DMA 0x00000200 /* RW */
  145. #define P_WRITE 0x00000100 /* R DMA dir, 1=to ram, 0=to port */
  146. #define P_RESET 0x00000080 /* RW */
  147. #define P_SLAVE_ERR 0x00000040 /* RW1 Access size error */
  148. #define P_INVALIDATE 0x00000020 /* W Drop P-FIFO */
  149. #define P_INT_EN 0x00000010 /* RW OK to P_INT_PEND||P_ERR_PEND */
  150. #define P_DRAINING 0x0000000c /* R P-FIFO is draining to memory */
  151. #define P_ERR_PEND 0x00000002 /* R */
  152. #define P_INT_PEND 0x00000001 /* R */
  153. /* BPP_HCR. Time is in increments of SBus clock. */
  154. #define P_HCR_TEST 0x8000 /* Allows buried counters to be read */
  155. #define P_HCR_DSW 0x7f00 /* Data strobe width (in ticks) */
  156. #define P_HCR_DDS 0x007f /* Data setup before strobe (in ticks) */
  157. /* BPP_OCR. */
  158. #define P_OCR_MEM_CLR 0x8000
  159. #define P_OCR_DATA_SRC 0x4000 /* ) */
  160. #define P_OCR_DS_DSEL 0x2000 /* ) Bidirectional */
  161. #define P_OCR_BUSY_DSEL 0x1000 /* ) selects */
  162. #define P_OCR_ACK_DSEL 0x0800 /* ) */
  163. #define P_OCR_EN_DIAG 0x0400
  164. #define P_OCR_BUSY_OP 0x0200 /* Busy operation */
  165. #define P_OCR_ACK_OP 0x0100 /* Ack operation */
  166. #define P_OCR_SRST 0x0080 /* Reset state machines. Not selfcleaning. */
  167. #define P_OCR_IDLE 0x0008 /* PP data transfer state machine is idle */
  168. #define P_OCR_V_ILCK 0x0002 /* Versatec faded. Zebra only. */
  169. #define P_OCR_EN_VER 0x0001 /* Enable Versatec (0 - enable). Zebra only. */
  170. /* BPP_TCR */
  171. #define P_TCR_DIR 0x08
  172. #define P_TCR_BUSY 0x04
  173. #define P_TCR_ACK 0x02
  174. #define P_TCR_DS 0x01 /* Strobe */
  175. /* BPP_OR */
  176. #define P_OR_V3 0x20 /* ) */
  177. #define P_OR_V2 0x10 /* ) on Zebra only */
  178. #define P_OR_V1 0x08 /* ) */
  179. #define P_OR_INIT 0x04
  180. #define P_OR_AFXN 0x02 /* Auto Feed */
  181. #define P_OR_SLCT_IN 0x01
  182. /* BPP_IR */
  183. #define P_IR_PE 0x04
  184. #define P_IR_SLCT 0x02
  185. #define P_IR_ERR 0x01
  186. /* BPP_ICR */
  187. #define P_DS_IRQ 0x8000 /* RW1 */
  188. #define P_ACK_IRQ 0x4000 /* RW1 */
  189. #define P_BUSY_IRQ 0x2000 /* RW1 */
  190. #define P_PE_IRQ 0x1000 /* RW1 */
  191. #define P_SLCT_IRQ 0x0800 /* RW1 */
  192. #define P_ERR_IRQ 0x0400 /* RW1 */
  193. #define P_DS_IRQ_EN 0x0200 /* RW Always on rising edge */
  194. #define P_ACK_IRQ_EN 0x0100 /* RW Always on rising edge */
  195. #define P_BUSY_IRP 0x0080 /* RW 1= rising edge */
  196. #define P_BUSY_IRQ_EN 0x0040 /* RW */
  197. #define P_PE_IRP 0x0020 /* RW 1= rising edge */
  198. #define P_PE_IRQ_EN 0x0010 /* RW */
  199. #define P_SLCT_IRP 0x0008 /* RW 1= rising edge */
  200. #define P_SLCT_IRQ_EN 0x0004 /* RW */
  201. #define P_ERR_IRP 0x0002 /* RW1 1= rising edge */
  202. #define P_ERR_IRQ_EN 0x0001 /* RW */
  203. static void __iomem *base_addrs[BPP_NO];
  204. #define bpp_outb_p(data, base) sbus_writeb(data, (base) + BPP_DR)
  205. #define bpp_inb_p(base) sbus_readb((base) + BPP_DR)
  206. #define bpp_inb(base) sbus_readb((base) + BPP_DR)
  207. static void set_pins(unsigned short pins, unsigned minor)
  208. {
  209. void __iomem *base = base_addrs[minor];
  210. unsigned char bits_tcr = 0, bits_or = 0;
  211. if (instances[minor].direction & 0x20) bits_tcr |= P_TCR_DIR;
  212. if ( pins & BPP_PP_nStrobe) bits_tcr |= P_TCR_DS;
  213. if ( pins & BPP_PP_nAutoFd) bits_or |= P_OR_AFXN;
  214. if (! (pins & BPP_PP_nInit)) bits_or |= P_OR_INIT;
  215. if (! (pins & BPP_PP_nSelectIn)) bits_or |= P_OR_SLCT_IN;
  216. sbus_writeb(bits_or, base + BPP_OR);
  217. sbus_writeb(bits_tcr, base + BPP_TCR);
  218. }
  219. /*
  220. * i386 people read output pins from a software image.
  221. * We may get them back from hardware.
  222. * Again, inversion of pins must he buried here.
  223. */
  224. static unsigned short get_pins(unsigned minor)
  225. {
  226. void __iomem *base = base_addrs[minor];
  227. unsigned short bits = 0;
  228. unsigned value_tcr = sbus_readb(base + BPP_TCR);
  229. unsigned value_ir = sbus_readb(base + BPP_IR);
  230. unsigned value_or = sbus_readb(base + BPP_OR);
  231. if (value_tcr & P_TCR_DS) bits |= BPP_PP_nStrobe;
  232. if (value_or & P_OR_AFXN) bits |= BPP_PP_nAutoFd;
  233. if (! (value_or & P_OR_INIT)) bits |= BPP_PP_nInit;
  234. if (! (value_or & P_OR_SLCT_IN)) bits |= BPP_PP_nSelectIn;
  235. if (value_ir & P_IR_ERR) bits |= BPP_GP_nFault;
  236. if (! (value_ir & P_IR_SLCT)) bits |= BPP_GP_Select;
  237. if (! (value_ir & P_IR_PE)) bits |= BPP_GP_PError;
  238. if (! (value_tcr & P_TCR_ACK)) bits |= BPP_GP_nAck;
  239. if (value_tcr & P_TCR_BUSY) bits |= BPP_GP_Busy;
  240. return bits;
  241. }
  242. #endif /* __sparc__ */
  243. static void snooze(unsigned long snooze_time, unsigned minor)
  244. {
  245. schedule_timeout_uninterruptible(snooze_time + 1);
  246. }
  247. static int wait_for(unsigned short set, unsigned short clr,
  248. unsigned long delay, unsigned minor)
  249. {
  250. unsigned short pins = get_pins(minor);
  251. unsigned long extime = 0;
  252. /*
  253. * Try a real fast scan for the first jiffy, in case the device
  254. * responds real good. The first while loop guesses an expire
  255. * time accounting for possible wraparound of jiffies.
  256. */
  257. while (time_after_eq(jiffies, extime)) extime = jiffies + 1;
  258. while ( (time_before(jiffies, extime))
  259. && (((pins & set) != set) || ((pins & clr) != 0)) ) {
  260. pins = get_pins(minor);
  261. }
  262. delay -= 1;
  263. /*
  264. * If my delay expired or the pins are still not where I want
  265. * them, then resort to using the timer and greatly reduce my
  266. * sample rate. If the peripheral is going to be slow, this will
  267. * give the CPU up to some more worthy process.
  268. */
  269. while ( delay && (((pins & set) != set) || ((pins & clr) != 0)) ) {
  270. snooze(1, minor);
  271. pins = get_pins(minor);
  272. delay -= 1;
  273. }
  274. if (delay == 0) return -1;
  275. else return pins;
  276. }
  277. /*
  278. * Return ZERO(0) If the negotiation succeeds, an errno otherwise. An
  279. * errno means something broke, and I do not yet know how to fix it.
  280. */
  281. static int negotiate(unsigned char mode, unsigned minor)
  282. {
  283. int rc;
  284. unsigned short pins = get_pins(minor);
  285. if (pins & BPP_PP_nSelectIn) return -EIO;
  286. /* Event 0: Write the mode to the data lines */
  287. bpp_outb_p(mode, base_addrs[minor]);
  288. snooze(TIME_PSetup, minor);
  289. /* Event 1: Strobe the mode code into the peripheral */
  290. set_pins(BPP_PP_nSelectIn|BPP_PP_nStrobe|BPP_PP_nInit, minor);
  291. /* Wait for Event 2: Peripheral responds as a 1284 device. */
  292. rc = wait_for(BPP_GP_PError|BPP_GP_Select|BPP_GP_nFault,
  293. BPP_GP_nAck,
  294. TIME_PResponse,
  295. minor);
  296. if (rc == -1) return -ETIMEDOUT;
  297. /* Event 3: latch extensibility request */
  298. set_pins(BPP_PP_nSelectIn|BPP_PP_nInit, minor);
  299. /* ... quick nap while peripheral ponders the byte i'm sending...*/
  300. snooze(1, minor);
  301. /* Event 4: restore strobe, to ACK peripheral's response. */
  302. set_pins(BPP_PP_nSelectIn|BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, minor);
  303. /* Wait for Event 6: Peripheral latches response bits */
  304. rc = wait_for(BPP_GP_nAck, 0, TIME_PSetup+TIME_PResponse, minor);
  305. if (rc == -1) return -EIO;
  306. /* A 1284 device cannot refuse nibble mode */
  307. if (mode == DEFAULT_NIBBLE) return 0;
  308. if (pins & BPP_GP_Select) return 0;
  309. return -EPROTONOSUPPORT;
  310. }
  311. static int terminate(unsigned minor)
  312. {
  313. int rc;
  314. /* Event 22: Request termination of 1284 mode */
  315. set_pins(BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, minor);
  316. /* Wait for Events 23 and 24: ACK termination request. */
  317. rc = wait_for(BPP_GP_Busy|BPP_GP_nFault,
  318. BPP_GP_nAck,
  319. TIME_PSetup+TIME_PResponse,
  320. minor);
  321. instances[minor].direction = 0;
  322. instances[minor].mode = COMPATIBILITY;
  323. if (rc == -1) {
  324. return -EIO;
  325. }
  326. /* Event 25: Handshake by lowering nAutoFd */
  327. set_pins(BPP_PP_nStrobe|BPP_PP_nInit, minor);
  328. /* Event 26: Peripheral wiggles lines... */
  329. /* Event 27: Peripheral sets nAck HIGH to ack handshake */
  330. rc = wait_for(BPP_GP_nAck, 0, TIME_PResponse, minor);
  331. if (rc == -1) {
  332. set_pins(BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, minor);
  333. return -EIO;
  334. }
  335. /* Event 28: Finish phase by raising nAutoFd */
  336. set_pins(BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, minor);
  337. return 0;
  338. }
  339. static DEFINE_SPINLOCK(bpp_open_lock);
  340. /*
  341. * Allow only one process to open the device at a time.
  342. */
  343. static int bpp_open(struct inode *inode, struct file *f)
  344. {
  345. unsigned minor = iminor(inode);
  346. int ret;
  347. lock_kernel();
  348. spin_lock(&bpp_open_lock);
  349. ret = 0;
  350. if (minor >= BPP_NO) {
  351. ret = -ENODEV;
  352. } else {
  353. if (! instances[minor].present) {
  354. ret = -ENODEV;
  355. } else {
  356. if (instances[minor].opened)
  357. ret = -EBUSY;
  358. else
  359. instances[minor].opened = 1;
  360. }
  361. }
  362. spin_unlock(&bpp_open_lock);
  363. unlock_kernel();
  364. return ret;
  365. }
  366. /*
  367. * When the process closes the device, this method is called to clean
  368. * up and reset the hardware. Always leave the device in compatibility
  369. * mode as this is a reasonable place to clean up from messes made by
  370. * ioctls, or other mayhem.
  371. */
  372. static int bpp_release(struct inode *inode, struct file *f)
  373. {
  374. unsigned minor = iminor(inode);
  375. spin_lock(&bpp_open_lock);
  376. instances[minor].opened = 0;
  377. if (instances[minor].mode != COMPATIBILITY)
  378. terminate(minor);
  379. spin_unlock(&bpp_open_lock);
  380. return 0;
  381. }
  382. static long read_nibble(unsigned minor, char __user *c, unsigned long cnt)
  383. {
  384. unsigned long remaining = cnt;
  385. long rc;
  386. while (remaining > 0) {
  387. unsigned char byte = 0;
  388. int pins;
  389. /* Event 7: request nibble */
  390. set_pins(BPP_PP_nSelectIn|BPP_PP_nStrobe, minor);
  391. /* Wait for event 9: Peripher strobes first nibble */
  392. pins = wait_for(0, BPP_GP_nAck, TIME_IDLE_LIMIT, minor);
  393. if (pins == -1) return -ETIMEDOUT;
  394. /* Event 10: I handshake nibble */
  395. set_pins(BPP_PP_nSelectIn|BPP_PP_nStrobe|BPP_PP_nAutoFd, minor);
  396. if (pins & BPP_GP_nFault) byte |= 0x01;
  397. if (pins & BPP_GP_Select) byte |= 0x02;
  398. if (pins & BPP_GP_PError) byte |= 0x04;
  399. if (pins & BPP_GP_Busy) byte |= 0x08;
  400. /* Wait for event 11: Peripheral handshakes nibble */
  401. rc = wait_for(BPP_GP_nAck, 0, TIME_PResponse, minor);
  402. /* Event 7: request nibble */
  403. set_pins(BPP_PP_nSelectIn|BPP_PP_nStrobe, minor);
  404. /* Wait for event 9: Peripher strobes first nibble */
  405. pins = wait_for(0, BPP_GP_nAck, TIME_PResponse, minor);
  406. if (rc == -1) return -ETIMEDOUT;
  407. /* Event 10: I handshake nibble */
  408. set_pins(BPP_PP_nSelectIn|BPP_PP_nStrobe|BPP_PP_nAutoFd, minor);
  409. if (pins & BPP_GP_nFault) byte |= 0x10;
  410. if (pins & BPP_GP_Select) byte |= 0x20;
  411. if (pins & BPP_GP_PError) byte |= 0x40;
  412. if (pins & BPP_GP_Busy) byte |= 0x80;
  413. if (put_user(byte, c))
  414. return -EFAULT;
  415. c += 1;
  416. remaining -= 1;
  417. /* Wait for event 11: Peripheral handshakes nibble */
  418. rc = wait_for(BPP_GP_nAck, 0, TIME_PResponse, minor);
  419. if (rc == -1) return -EIO;
  420. }
  421. return cnt - remaining;
  422. }
  423. static long read_ecp(unsigned minor, char __user *c, unsigned long cnt)
  424. {
  425. unsigned long remaining;
  426. long rc;
  427. /* Turn ECP mode from forward to reverse if needed. */
  428. if (! instances[minor].direction) {
  429. unsigned short pins = get_pins(minor);
  430. /* Event 38: Turn the bus around */
  431. instances[minor].direction = 0x20;
  432. pins &= ~BPP_PP_nAutoFd;
  433. set_pins(pins, minor);
  434. /* Event 39: Set pins for reverse mode. */
  435. snooze(TIME_PSetup, minor);
  436. set_pins(BPP_PP_nStrobe|BPP_PP_nSelectIn, minor);
  437. /* Wait for event 40: Peripheral ready to be strobed */
  438. rc = wait_for(0, BPP_GP_PError, TIME_PResponse, minor);
  439. if (rc == -1) return -ETIMEDOUT;
  440. }
  441. remaining = cnt;
  442. while (remaining > 0) {
  443. /* If there is a run length for a repeated byte, repeat */
  444. /* that byte a few times. */
  445. if (instances[minor].run_length && !instances[minor].run_flag) {
  446. char buffer[128];
  447. unsigned idx;
  448. unsigned repeat = remaining < instances[minor].run_length
  449. ? remaining
  450. : instances[minor].run_length;
  451. for (idx = 0 ; idx < repeat ; idx += 1)
  452. buffer[idx] = instances[minor].repeat_byte;
  453. if (copy_to_user(c, buffer, repeat))
  454. return -EFAULT;
  455. remaining -= repeat;
  456. c += repeat;
  457. instances[minor].run_length -= repeat;
  458. }
  459. if (remaining == 0) break;
  460. /* Wait for Event 43: Data active on the bus. */
  461. rc = wait_for(0, BPP_GP_nAck, TIME_IDLE_LIMIT, minor);
  462. if (rc == -1) break;
  463. if (rc & BPP_GP_Busy) {
  464. /* OK, this is data. read it in. */
  465. unsigned char byte = bpp_inb(base_addrs[minor]);
  466. if (put_user(byte, c))
  467. return -EFAULT;
  468. c += 1;
  469. remaining -= 1;
  470. if (instances[minor].run_flag) {
  471. instances[minor].repeat_byte = byte;
  472. instances[minor].run_flag = 0;
  473. }
  474. } else {
  475. unsigned char byte = bpp_inb(base_addrs[minor]);
  476. if (byte & 0x80) {
  477. printk("bpp%d: "
  478. "Ignoring ECP channel %u from device.\n",
  479. minor, byte & 0x7f);
  480. } else {
  481. instances[minor].run_length = byte;
  482. instances[minor].run_flag = 1;
  483. }
  484. }
  485. /* Event 44: I got it. */
  486. set_pins(BPP_PP_nStrobe|BPP_PP_nAutoFd|BPP_PP_nSelectIn, minor);
  487. /* Wait for event 45: peripheral handshake */
  488. rc = wait_for(BPP_GP_nAck, 0, TIME_PResponse, minor);
  489. if (rc == -1) return -ETIMEDOUT;
  490. /* Event 46: Finish handshake */
  491. set_pins(BPP_PP_nStrobe|BPP_PP_nSelectIn, minor);
  492. }
  493. return cnt - remaining;
  494. }
  495. static ssize_t bpp_read(struct file *f, char __user *c, size_t cnt, loff_t * ppos)
  496. {
  497. long rc;
  498. unsigned minor = iminor(f->f_path.dentry->d_inode);
  499. if (minor >= BPP_NO) return -ENODEV;
  500. if (!instances[minor].present) return -ENODEV;
  501. switch (instances[minor].mode) {
  502. default:
  503. if (instances[minor].mode != COMPATIBILITY)
  504. terminate(minor);
  505. if (instances[minor].enhanced) {
  506. /* For now, do all reads with ECP-RLE mode */
  507. unsigned short pins;
  508. rc = negotiate(DEFAULT_ECP, minor);
  509. if (rc < 0) break;
  510. instances[minor].mode = ECP_RLE;
  511. /* Event 30: set nAutoFd low to setup for ECP mode */
  512. pins = get_pins(minor);
  513. pins &= ~BPP_PP_nAutoFd;
  514. set_pins(pins, minor);
  515. /* Wait for Event 31: peripheral ready */
  516. rc = wait_for(BPP_GP_PError, 0, TIME_PResponse, minor);
  517. if (rc == -1) return -ETIMEDOUT;
  518. rc = read_ecp(minor, c, cnt);
  519. } else {
  520. rc = negotiate(DEFAULT_NIBBLE, minor);
  521. if (rc < 0) break;
  522. instances[minor].mode = NIBBLE;
  523. rc = read_nibble(minor, c, cnt);
  524. }
  525. break;
  526. case NIBBLE:
  527. rc = read_nibble(minor, c, cnt);
  528. break;
  529. case ECP:
  530. case ECP_RLE:
  531. rc = read_ecp(minor, c, cnt);
  532. break;
  533. }
  534. return rc;
  535. }
  536. /*
  537. * Compatibility mode handshaking is a matter of writing data,
  538. * strobing it, and waiting for the printer to stop being busy.
  539. */
  540. static long write_compat(unsigned minor, const char __user *c, unsigned long cnt)
  541. {
  542. long rc;
  543. unsigned short pins = get_pins(minor);
  544. unsigned long remaining = cnt;
  545. while (remaining > 0) {
  546. unsigned char byte;
  547. if (get_user(byte, c))
  548. return -EFAULT;
  549. c += 1;
  550. rc = wait_for(BPP_GP_nAck, BPP_GP_Busy, TIME_IDLE_LIMIT, minor);
  551. if (rc == -1) return -ETIMEDOUT;
  552. bpp_outb_p(byte, base_addrs[minor]);
  553. remaining -= 1;
  554. /* snooze(1, minor); */
  555. pins &= ~BPP_PP_nStrobe;
  556. set_pins(pins, minor);
  557. rc = wait_for(BPP_GP_Busy, 0, TIME_PResponse, minor);
  558. pins |= BPP_PP_nStrobe;
  559. set_pins(pins, minor);
  560. }
  561. return cnt - remaining;
  562. }
  563. /*
  564. * Write data using ECP mode. Watch out that the port may be set up
  565. * for reading. If so, turn the port around.
  566. */
  567. static long write_ecp(unsigned minor, const char __user *c, unsigned long cnt)
  568. {
  569. unsigned short pins = get_pins(minor);
  570. unsigned long remaining = cnt;
  571. if (instances[minor].direction) {
  572. int rc;
  573. /* Event 47 Request bus be turned around */
  574. pins |= BPP_PP_nInit;
  575. set_pins(pins, minor);
  576. /* Wait for Event 49: Peripheral relinquished bus */
  577. rc = wait_for(BPP_GP_PError, 0, TIME_PResponse, minor);
  578. pins |= BPP_PP_nAutoFd;
  579. instances[minor].direction = 0;
  580. set_pins(pins, minor);
  581. }
  582. while (remaining > 0) {
  583. unsigned char byte;
  584. int rc;
  585. if (get_user(byte, c))
  586. return -EFAULT;
  587. rc = wait_for(0, BPP_GP_Busy, TIME_PResponse, minor);
  588. if (rc == -1) return -ETIMEDOUT;
  589. c += 1;
  590. bpp_outb_p(byte, base_addrs[minor]);
  591. pins &= ~BPP_PP_nStrobe;
  592. set_pins(pins, minor);
  593. pins |= BPP_PP_nStrobe;
  594. rc = wait_for(BPP_GP_Busy, 0, TIME_PResponse, minor);
  595. if (rc == -1) return -EIO;
  596. set_pins(pins, minor);
  597. }
  598. return cnt - remaining;
  599. }
  600. /*
  601. * Write to the peripheral. Be sensitive of the current mode. If I'm
  602. * in a mode that can be turned around (ECP) then just do
  603. * that. Otherwise, terminate and do my writing in compat mode. This
  604. * is the safest course as any device can handle it.
  605. */
  606. static ssize_t bpp_write(struct file *f, const char __user *c, size_t cnt, loff_t * ppos)
  607. {
  608. long errno = 0;
  609. unsigned minor = iminor(f->f_path.dentry->d_inode);
  610. if (minor >= BPP_NO) return -ENODEV;
  611. if (!instances[minor].present) return -ENODEV;
  612. switch (instances[minor].mode) {
  613. case ECP:
  614. case ECP_RLE:
  615. errno = write_ecp(minor, c, cnt);
  616. break;
  617. case COMPATIBILITY:
  618. errno = write_compat(minor, c, cnt);
  619. break;
  620. default:
  621. terminate(minor);
  622. errno = write_compat(minor, c, cnt);
  623. }
  624. return errno;
  625. }
  626. static int bpp_ioctl(struct inode *inode, struct file *f, unsigned int cmd,
  627. unsigned long arg)
  628. {
  629. int errno = 0;
  630. unsigned minor = iminor(inode);
  631. if (minor >= BPP_NO) return -ENODEV;
  632. if (!instances[minor].present) return -ENODEV;
  633. switch (cmd) {
  634. case BPP_PUT_PINS:
  635. set_pins(arg, minor);
  636. break;
  637. case BPP_GET_PINS:
  638. errno = get_pins(minor);
  639. break;
  640. case BPP_PUT_DATA:
  641. bpp_outb_p(arg, base_addrs[minor]);
  642. break;
  643. case BPP_GET_DATA:
  644. errno = bpp_inb_p(base_addrs[minor]);
  645. break;
  646. case BPP_SET_INPUT:
  647. if (arg)
  648. if (instances[minor].enhanced) {
  649. unsigned short bits = get_pins(minor);
  650. instances[minor].direction = 0x20;
  651. set_pins(bits, minor);
  652. } else {
  653. errno = -ENOTTY;
  654. }
  655. else {
  656. unsigned short bits = get_pins(minor);
  657. instances[minor].direction = 0x00;
  658. set_pins(bits, minor);
  659. }
  660. break;
  661. default:
  662. errno = -EINVAL;
  663. }
  664. return errno;
  665. }
  666. static const struct file_operations bpp_fops = {
  667. .owner = THIS_MODULE,
  668. .read = bpp_read,
  669. .write = bpp_write,
  670. .ioctl = bpp_ioctl,
  671. .open = bpp_open,
  672. .release = bpp_release,
  673. };
  674. #if defined(__i386__)
  675. #define collectLptPorts() {}
  676. static void probeLptPort(unsigned idx)
  677. {
  678. unsigned int testvalue;
  679. const unsigned short lpAddr = base_addrs[idx];
  680. instances[idx].present = 0;
  681. instances[idx].enhanced = 0;
  682. instances[idx].direction = 0;
  683. instances[idx].mode = COMPATIBILITY;
  684. instances[idx].run_length = 0;
  685. instances[idx].run_flag = 0;
  686. if (!request_region(lpAddr,3, bpp_dev_name)) return;
  687. /*
  688. * First, make sure the instance exists. Do this by writing to
  689. * the data latch and reading the value back. If the port *is*
  690. * present, test to see if it supports extended-mode
  691. * operation. This will be required for IEEE1284 reverse
  692. * transfers.
  693. */
  694. outb_p(BPP_PROBE_CODE, lpAddr);
  695. for (testvalue=0; testvalue<BPP_DELAY; testvalue++)
  696. ;
  697. testvalue = inb_p(lpAddr);
  698. if (testvalue == BPP_PROBE_CODE) {
  699. unsigned save;
  700. instances[idx].present = 1;
  701. save = inb_p(lpAddr+2);
  702. for (testvalue=0; testvalue<BPP_DELAY; testvalue++)
  703. ;
  704. outb_p(save|0x20, lpAddr+2);
  705. for (testvalue=0; testvalue<BPP_DELAY; testvalue++)
  706. ;
  707. outb_p(~BPP_PROBE_CODE, lpAddr);
  708. for (testvalue=0; testvalue<BPP_DELAY; testvalue++)
  709. ;
  710. testvalue = inb_p(lpAddr);
  711. if ((testvalue&0xff) == (0xff&~BPP_PROBE_CODE))
  712. instances[idx].enhanced = 0;
  713. else
  714. instances[idx].enhanced = 1;
  715. outb_p(save, lpAddr+2);
  716. }
  717. else {
  718. release_region(lpAddr,3);
  719. }
  720. /*
  721. * Leave the port in compat idle mode.
  722. */
  723. set_pins(BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, idx);
  724. printk("bpp%d: Port at 0x%03x: Enhanced mode %s\n", idx, base_addrs[idx],
  725. instances[idx].enhanced? "SUPPORTED" : "UNAVAILABLE");
  726. }
  727. static inline void freeLptPort(int idx)
  728. {
  729. release_region(base_addrs[idx], 3);
  730. }
  731. #endif
  732. #if defined(__sparc__)
  733. static void __iomem *map_bpp(struct sbus_dev *dev, int idx)
  734. {
  735. return sbus_ioremap(&dev->resource[0], 0, BPP_SIZE, "bpp");
  736. }
  737. static int collectLptPorts(void)
  738. {
  739. struct sbus_bus *bus;
  740. struct sbus_dev *dev;
  741. int count;
  742. count = 0;
  743. for_all_sbusdev(dev, bus) {
  744. if (strcmp(dev->prom_name, "SUNW,bpp") == 0) {
  745. if (count >= BPP_NO) {
  746. printk(KERN_NOTICE
  747. "bpp: More than %d bpp ports,"
  748. " rest is ignored\n", BPP_NO);
  749. return count;
  750. }
  751. base_addrs[count] = map_bpp(dev, count);
  752. count++;
  753. }
  754. }
  755. return count;
  756. }
  757. static void probeLptPort(unsigned idx)
  758. {
  759. void __iomem *rp = base_addrs[idx];
  760. __u32 csr;
  761. char *brand;
  762. instances[idx].present = 0;
  763. instances[idx].enhanced = 0;
  764. instances[idx].direction = 0;
  765. instances[idx].mode = COMPATIBILITY;
  766. instances[idx].run_length = 0;
  767. instances[idx].run_flag = 0;
  768. if (!rp) return;
  769. instances[idx].present = 1;
  770. instances[idx].enhanced = 1; /* Sure */
  771. csr = sbus_readl(rp + BPP_CSR);
  772. if ((csr & P_DRAINING) != 0 && (csr & P_ERR_PEND) == 0) {
  773. udelay(20);
  774. csr = sbus_readl(rp + BPP_CSR);
  775. if ((csr & P_DRAINING) != 0 && (csr & P_ERR_PEND) == 0) {
  776. printk("bpp%d: DRAINING still active (0x%08x)\n", idx, csr);
  777. }
  778. }
  779. printk("bpp%d: reset with 0x%08x ..", idx, csr);
  780. sbus_writel((csr | P_RESET) & ~P_INT_EN, rp + BPP_CSR);
  781. udelay(500);
  782. sbus_writel(sbus_readl(rp + BPP_CSR) & ~P_RESET, rp + BPP_CSR);
  783. csr = sbus_readl(rp + BPP_CSR);
  784. printk(" done with csr=0x%08x ocr=0x%04x\n",
  785. csr, sbus_readw(rp + BPP_OCR));
  786. switch (csr & P_DEV_ID_MASK) {
  787. case P_DEV_ID_ZEBRA:
  788. brand = "Zebra";
  789. break;
  790. case P_DEV_ID_L64854:
  791. brand = "DMA2";
  792. break;
  793. default:
  794. brand = "Unknown";
  795. }
  796. printk("bpp%d: %s at %p\n", idx, brand, rp);
  797. /*
  798. * Leave the port in compat idle mode.
  799. */
  800. set_pins(BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, idx);
  801. return;
  802. }
  803. static inline void freeLptPort(int idx)
  804. {
  805. sbus_iounmap(base_addrs[idx], BPP_SIZE);
  806. }
  807. #endif
  808. static int __init bpp_init(void)
  809. {
  810. int rc;
  811. unsigned idx;
  812. rc = collectLptPorts();
  813. if (rc == 0)
  814. return -ENODEV;
  815. rc = register_chrdev(BPP_MAJOR, bpp_dev_name, &bpp_fops);
  816. if (rc < 0)
  817. return rc;
  818. for (idx = 0; idx < BPP_NO; idx++) {
  819. instances[idx].opened = 0;
  820. probeLptPort(idx);
  821. }
  822. return 0;
  823. }
  824. static void __exit bpp_cleanup(void)
  825. {
  826. unsigned idx;
  827. unregister_chrdev(BPP_MAJOR, bpp_dev_name);
  828. for (idx = 0; idx < BPP_NO; idx++) {
  829. if (instances[idx].present)
  830. freeLptPort(idx);
  831. }
  832. }
  833. module_init(bpp_init);
  834. module_exit(bpp_cleanup);
  835. MODULE_LICENSE("GPL");