1 /*- 2 * Copyright (c) 1997 Nicolas Souchu 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $Id: ppb_1284.c,v 1.3 1998/01/31 07:23:06 eivind Exp $ 27 * 28 */ 29 30 #include "opt_debug_1284.h" 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 35 #include <machine/clock.h> 36 37 #include <dev/ppbus/ppbconf.h> 38 #include <dev/ppbus/ppb_1284.h> 39 40 /* 41 * do_1284_wait() 42 * 43 * Wait for the peripherial up to 40ms 44 */ 45 int 46 do_1284_wait(struct ppb_device *dev, char mask, char status) 47 { 48 int i; 49 char r; 50 51 /* try up to 5ms */ 52 for (i = 0; i < 20; i++) { 53 r = ppb_rstr(dev); 54 DELAY(25); 55 if ((r & mask) == status) 56 return (0); 57 } 58 59 return (ppb_poll_device(dev, 4, mask, status, PPB_NOINTR)); 60 } 61 62 #define nibble2char(s) (((s & ~nACK) >> 3) | (~s & nBUSY) >> 4) 63 64 /* 65 * byte_1284_inbyte() 66 * 67 * Read 1 byte in BYTE mode 68 */ 69 int 70 byte_1284_inbyte(struct ppb_device *dev, char *buffer) 71 { 72 int error; 73 74 /* notify the peripherial to put data on the lines */ 75 ppb_wctr(dev, PCD | AUTOFEED | nSTROBE | nINIT | nSELECTIN); 76 77 /* wait for valid byte signal */ 78 if ((error = do_1284_wait(dev, nACK, 0))) 79 return (error); 80 81 /* fetch data */ 82 *buffer = ppb_rdtr(dev); 83 84 /* indicate that data has been received, not ready for another */ 85 ppb_wctr(dev, PCD | nAUTOFEED | nSTROBE | nINIT | nSELECTIN); 86 87 /* wait peripherial's acknowledgement */ 88 if ((error = do_1284_wait(dev, nACK, nACK))) 89 return (error); 90 91 /* acknowledge the peripherial */ 92 ppb_wctr(dev, PCD | nAUTOFEED | STROBE | nINIT | nSELECTIN); 93 94 return (0); 95 } 96 97 /* 98 * nibble_1284_inbyte() 99 * 100 * Read 1 byte in NIBBLE mode 101 */ 102 int 103 nibble_1284_inbyte(struct ppb_device *dev, char *buffer) 104 { 105 char nibble[2]; 106 int i, error; 107 108 for (i = 0; i < 2; i++) { 109 /* ready to take data (nAUTO low) */ 110 ppb_wctr(dev, AUTOFEED | nSTROBE | nINIT | nSELECTIN); 111 112 if ((error = do_1284_wait(dev, nACK, 0))) 113 return (error); 114 115 /* read nibble */ 116 nibble[i] = ppb_rstr(dev); 117 118 #ifdef DEBUG_1284 119 printf("nibble_1284_inbyte: nibble[%d]=0x%x\n", i, nibble[i]); 120 #endif 121 122 /* ack, not ready for another nibble */ 123 ppb_wctr(dev, nAUTOFEED | nSTROBE | nINIT | nSELECTIN); 124 125 /* wait ack from peripherial */ 126 if ((error = do_1284_wait(dev, nACK, nACK))) 127 return (error); 128 } 129 130 *buffer = ((nibble2char(nibble[1]) << 4) & 0xf0) | 131 (nibble2char(nibble[0]) & 0x0f); 132 133 #ifdef DEBUG_1284 134 printf("nibble_1284_inbyte: byte=0x%x\n", *buffer); 135 #endif 136 137 return (0); 138 } 139 140 /* 141 * nibble_1284_sync() 142 */ 143 void 144 nibble_1284_sync(struct ppb_device *dev) 145 { 146 char ctr; 147 148 ctr = ppb_rctr(dev); 149 150 ppb_wctr(dev, (ctr & ~AUTOFEED) | SELECTIN); 151 if (do_1284_wait(dev, nACK, 0)) 152 return; 153 154 ppb_wctr(dev, ctr | AUTOFEED); 155 do_1284_wait(dev, nACK, nACK); 156 157 ppb_wctr(dev, (ctr & ~AUTOFEED) | SELECTIN); 158 159 return; 160 } 161 162 /* 163 * nibble_1284_mode() 164 * 165 * Normal nibble mode or request device id mode (see ppb_1284.h) 166 */ 167 int 168 nibble_1284_mode(struct ppb_device *dev, int mode) 169 { 170 char ctrl; 171 int error; 172 173 ctrl = ppb_rctr(dev); 174 175 ppb_wdtr(dev, mode); 176 DELAY(5); 177 178 ppb_wctr(dev, (ctrl & ~SELECTIN) | AUTOFEED); 179 if ((error = do_1284_wait(dev, nACK | ERROR | SELECT | nFAULT, 180 ERROR | SELECT | nFAULT))) { 181 ppb_wctr(dev, ctrl); 182 return (error); 183 } 184 185 ppb_wctr(dev, ppb_rctr(dev) | STROBE); 186 DELAY(5); 187 188 ppb_wctr(dev, ppb_rctr(dev) & ~STROBE); 189 DELAY(5); 190 191 ppb_wctr(dev, ppb_rctr(dev) & ~AUTOFEED); 192 193 return (0); 194 } 195