1/*
2        ktti.c        (c) 1998  Grant R. Guenther <grant@torque.net>
3                          Under the terms of the GNU General Public License.
4
5	ktti.c is a low-level protocol driver for the KT Technology
6	parallel port adapter.  This adapter is used in the "PHd"
7        portable hard-drives.  As far as I can tell, this device
8	supports 4-bit mode _only_.
9
10*/
11
12#define KTTI_VERSION      "1.0"
13
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/delay.h>
17#include <linux/kernel.h>
18#include <linux/types.h>
19#include <linux/wait.h>
20#include <asm/io.h>
21
22#include "paride.h"
23
24#define j44(a,b)                (((a>>4)&0x0f)|(b&0xf0))
25
26/* cont = 0 - access the IDE register file
27   cont = 1 - access the IDE command set
28*/
29
30static int  cont_map[2] = { 0x10, 0x08 };
31
32static void  ktti_write_regr( PIA *pi, int cont, int regr, int val)
33
34{	int r;
35
36	r = regr + cont_map[cont];
37
38	w0(r); w2(0xb); w2(0xa); w2(3); w2(6);
39	w0(val); w2(3); w0(0); w2(6); w2(0xb);
40}
41
42static int ktti_read_regr( PIA *pi, int cont, int regr )
43
44{	int  a, b, r;
45
46        r = regr + cont_map[cont];
47
48        w0(r); w2(0xb); w2(0xa); w2(9); w2(0xc); w2(9);
49	a = r1(); w2(0xc);  b = r1(); w2(9); w2(0xc); w2(9);
50	return j44(a,b);
51
52}
53
54static void ktti_read_block( PIA *pi, char * buf, int count )
55
56{	int  k, a, b;
57
58	for (k=0;k<count/2;k++) {
59		w0(0x10); w2(0xb); w2(0xa); w2(9); w2(0xc); w2(9);
60		a = r1(); w2(0xc); b = r1(); w2(9);
61		buf[2*k] = j44(a,b);
62		a = r1(); w2(0xc); b = r1(); w2(9);
63		buf[2*k+1] = j44(a,b);
64	}
65}
66
67static void ktti_write_block( PIA *pi, char * buf, int count )
68
69{	int k;
70
71	for (k=0;k<count/2;k++) {
72		w0(0x10); w2(0xb); w2(0xa); w2(3); w2(6);
73		w0(buf[2*k]); w2(3);
74		w0(buf[2*k+1]); w2(6);
75		w2(0xb);
76	}
77}
78
79static void ktti_connect ( PIA *pi  )
80
81{       pi->saved_r0 = r0();
82        pi->saved_r2 = r2();
83	w2(0xb); w2(0xa); w0(0); w2(3); w2(6);
84}
85
86static void ktti_disconnect ( PIA *pi )
87
88{       w2(0xb); w2(0xa); w0(0xa0); w2(3); w2(4);
89	w0(pi->saved_r0);
90        w2(pi->saved_r2);
91}
92
93static void ktti_log_adapter( PIA *pi, char * scratch, int verbose )
94
95{       printk("%s: ktti %s, KT adapter at 0x%x, delay %d\n",
96                pi->device,KTTI_VERSION,pi->port,pi->delay);
97
98}
99
100static struct pi_protocol ktti = {
101	.owner		= THIS_MODULE,
102	.name		= "ktti",
103	.max_mode	= 1,
104	.epp_first	= 2,
105	.default_delay	= 1,
106	.max_units	= 1,
107	.write_regr	= ktti_write_regr,
108	.read_regr	= ktti_read_regr,
109	.write_block	= ktti_write_block,
110	.read_block	= ktti_read_block,
111	.connect	= ktti_connect,
112	.disconnect	= ktti_disconnect,
113	.log_adapter	= ktti_log_adapter,
114};
115
116static int __init ktti_init(void)
117{
118	return paride_register(&ktti);
119}
120
121static void __exit ktti_exit(void)
122{
123	paride_unregister(&ktti);
124}
125
126MODULE_LICENSE("GPL");
127module_init(ktti_init)
128module_exit(ktti_exit)
129