bpp.c 30 KB

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