blob: 8bef6d60f88b6f10a683ffae201530123c5c49e9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Low-level parallel port routines for the Amiga built-in port
2 *
3 * Author: Joerg Dorchain <joerg@dorchain.net>
4 *
5 * This is a complete rewrite of the code, but based heaviy upon the old
6 * lp_intern. code.
7 *
8 * The built-in Amiga parallel port provides one port at a fixed address
9 * with 8 bidirectional data lines (D0 - D7) and 3 bidirectional status
10 * lines (BUSY, POUT, SEL), 1 output control line /STROBE (raised automatically
11 * in hardware when the data register is accessed), and 1 input control line
12 * /ACK, able to cause an interrupt, but both not directly settable by
13 * software.
14 */
15
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/parport.h>
19#include <linux/ioport.h>
20#include <linux/interrupt.h>
Geert Uytterhoeven6f8221c2009-04-05 13:13:20 +020021#include <linux/platform_device.h>
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/setup.h>
24#include <asm/amigahw.h>
25#include <asm/irq.h>
26#include <asm/io.h>
27#include <asm/amigaints.h>
28
29#undef DEBUG
30#ifdef DEBUG
31#define DPRINTK printk
32#else
33#define DPRINTK(x...) do { } while (0)
34#endif
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37static void amiga_write_data(struct parport *p, unsigned char data)
38{
39 DPRINTK(KERN_DEBUG "write_data %c\n",data);
40 /* Triggers also /STROBE. This behavior cannot be changed */
41 ciaa.prb = data;
42 mb();
43}
44
45static unsigned char amiga_read_data(struct parport *p)
46{
47 /* Triggers also /STROBE. This behavior cannot be changed */
48 return ciaa.prb;
49}
50
51#if 0
52static unsigned char control_pc_to_amiga(unsigned char control)
53{
54 unsigned char ret = 0;
55
56 if (control & PARPORT_CONTROL_SELECT) /* XXX: What is SELECP? */
57 ;
58 if (control & PARPORT_CONTROL_INIT) /* INITP */
59 /* reset connected to cpu reset pin */;
60 if (control & PARPORT_CONTROL_AUTOFD) /* AUTOLF */
61 /* Not connected */;
62 if (control & PARPORT_CONTROL_STROBE) /* Strobe */
63 /* Handled only directly by hardware */;
64 return ret;
65}
66#endif
67
68static unsigned char control_amiga_to_pc(unsigned char control)
69{
70 return PARPORT_CONTROL_SELECT |
71 PARPORT_CONTROL_AUTOFD | PARPORT_CONTROL_STROBE;
72 /* fake value: interrupt enable, select in, no reset,
73 no autolf, no strobe - seems to be closest the wiring diagram */
74}
75
76static void amiga_write_control(struct parport *p, unsigned char control)
77{
78 DPRINTK(KERN_DEBUG "write_control %02x\n",control);
79 /* No implementation possible */
80}
81
82static unsigned char amiga_read_control( struct parport *p)
83{
84 DPRINTK(KERN_DEBUG "read_control \n");
85 return control_amiga_to_pc(0);
86}
87
88static unsigned char amiga_frob_control( struct parport *p, unsigned char mask, unsigned char val)
89{
90 unsigned char old;
91
92 DPRINTK(KERN_DEBUG "frob_control mask %02x, value %02x\n",mask,val);
93 old = amiga_read_control(p);
94 amiga_write_control(p, (old & ~mask) ^ val);
95 return old;
96}
97
98#if 0 /* currently unused */
99static unsigned char status_pc_to_amiga(unsigned char status)
100{
101 unsigned char ret = 1;
102
103 if (status & PARPORT_STATUS_BUSY) /* Busy */
104 ret &= ~1;
105 if (status & PARPORT_STATUS_ACK) /* Ack */
106 /* handled in hardware */;
107 if (status & PARPORT_STATUS_PAPEROUT) /* PaperOut */
108 ret |= 2;
109 if (status & PARPORT_STATUS_SELECT) /* select */
110 ret |= 4;
111 if (status & PARPORT_STATUS_ERROR) /* error */
112 /* not connected */;
113 return ret;
114}
115#endif
116
117static unsigned char status_amiga_to_pc(unsigned char status)
118{
119 unsigned char ret = PARPORT_STATUS_BUSY | PARPORT_STATUS_ACK | PARPORT_STATUS_ERROR;
120
121 if (status & 1) /* Busy */
122 ret &= ~PARPORT_STATUS_BUSY;
123 if (status & 2) /* PaperOut */
124 ret |= PARPORT_STATUS_PAPEROUT;
125 if (status & 4) /* Selected */
126 ret |= PARPORT_STATUS_SELECT;
127 /* the rest is not connected or handled autonomously in hardware */
128
129 return ret;
130}
131
132static unsigned char amiga_read_status(struct parport *p)
133{
134 unsigned char status;
135
136 status = status_amiga_to_pc(ciab.pra & 7);
137 DPRINTK(KERN_DEBUG "read_status %02x\n", status);
138 return status;
139}
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static void amiga_enable_irq(struct parport *p)
142{
143 enable_irq(IRQ_AMIGA_CIAA_FLG);
144}
145
146static void amiga_disable_irq(struct parport *p)
147{
148 disable_irq(IRQ_AMIGA_CIAA_FLG);
149}
150
151static void amiga_data_forward(struct parport *p)
152{
153 DPRINTK(KERN_DEBUG "forward\n");
154 ciaa.ddrb = 0xff; /* all pins output */
155 mb();
156}
157
158static void amiga_data_reverse(struct parport *p)
159{
160 DPRINTK(KERN_DEBUG "reverse\n");
161 ciaa.ddrb = 0; /* all pins input */
162 mb();
163}
164
165static void amiga_init_state(struct pardevice *dev, struct parport_state *s)
166{
167 s->u.amiga.data = 0;
168 s->u.amiga.datadir = 255;
169 s->u.amiga.status = 0;
170 s->u.amiga.statusdir = 0;
171}
172
173static void amiga_save_state(struct parport *p, struct parport_state *s)
174{
175 mb();
176 s->u.amiga.data = ciaa.prb;
177 s->u.amiga.datadir = ciaa.ddrb;
178 s->u.amiga.status = ciab.pra & 7;
179 s->u.amiga.statusdir = ciab.ddra & 7;
180 mb();
181}
182
183static void amiga_restore_state(struct parport *p, struct parport_state *s)
184{
185 mb();
186 ciaa.prb = s->u.amiga.data;
187 ciaa.ddrb = s->u.amiga.datadir;
188 ciab.pra |= (ciab.pra & 0xf8) | s->u.amiga.status;
189 ciab.ddra |= (ciab.ddra & 0xf8) | s->u.amiga.statusdir;
190 mb();
191}
192
193static struct parport_operations pp_amiga_ops = {
194 .write_data = amiga_write_data,
195 .read_data = amiga_read_data,
196
197 .write_control = amiga_write_control,
198 .read_control = amiga_read_control,
199 .frob_control = amiga_frob_control,
200
201 .read_status = amiga_read_status,
202
203 .enable_irq = amiga_enable_irq,
204 .disable_irq = amiga_disable_irq,
205
206 .data_forward = amiga_data_forward,
207 .data_reverse = amiga_data_reverse,
208
209 .init_state = amiga_init_state,
210 .save_state = amiga_save_state,
211 .restore_state = amiga_restore_state,
212
213 .epp_write_data = parport_ieee1284_epp_write_data,
214 .epp_read_data = parport_ieee1284_epp_read_data,
215 .epp_write_addr = parport_ieee1284_epp_write_addr,
216 .epp_read_addr = parport_ieee1284_epp_read_addr,
217
218 .ecp_write_data = parport_ieee1284_ecp_write_data,
219 .ecp_read_data = parport_ieee1284_ecp_read_data,
220 .ecp_write_addr = parport_ieee1284_ecp_write_addr,
221
222 .compat_write_data = parport_ieee1284_write_compat,
223 .nibble_read_data = parport_ieee1284_read_nibble,
224 .byte_read_data = parport_ieee1284_read_byte,
225
226 .owner = THIS_MODULE,
227};
228
229/* ----------- Initialisation code --------------------------------- */
230
Geert Uytterhoeven6f8221c2009-04-05 13:13:20 +0200231static int __init amiga_parallel_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 struct parport *p;
234 int err;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 ciaa.ddrb = 0xff;
237 ciab.ddra &= 0xf8;
238 mb();
239
240 p = parport_register_port((unsigned long)&ciaa.prb, IRQ_AMIGA_CIAA_FLG,
241 PARPORT_DMA_NONE, &pp_amiga_ops);
242 if (!p)
Geert Uytterhoeven6f8221c2009-04-05 13:13:20 +0200243 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Geert Uytterhoeven6f8221c2009-04-05 13:13:20 +0200245 err = request_irq(IRQ_AMIGA_CIAA_FLG, parport_irq_handler, 0, p->name,
246 p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (err)
248 goto out_irq;
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 printk(KERN_INFO "%s: Amiga built-in port using irq\n", p->name);
251 /* XXX: set operating mode */
252 parport_announce_port(p);
253
Geert Uytterhoeven6f8221c2009-04-05 13:13:20 +0200254 platform_set_drvdata(pdev, p);
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 0;
257
258out_irq:
259 parport_put_port(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return err;
261}
262
Geert Uytterhoeven6f8221c2009-04-05 13:13:20 +0200263static int __exit amiga_parallel_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Geert Uytterhoeven6f8221c2009-04-05 13:13:20 +0200265 struct parport *port = platform_get_drvdata(pdev);
266
267 parport_remove_port(port);
268 if (port->irq != PARPORT_IRQ_NONE)
269 free_irq(IRQ_AMIGA_CIAA_FLG, port);
270 parport_put_port(port);
271 platform_set_drvdata(pdev, NULL);
272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Geert Uytterhoeven6f8221c2009-04-05 13:13:20 +0200275static struct platform_driver amiga_parallel_driver = {
276 .remove = __exit_p(amiga_parallel_remove),
277 .driver = {
278 .name = "amiga-parallel",
279 .owner = THIS_MODULE,
280 },
281};
282
283static int __init amiga_parallel_init(void)
284{
285 return platform_driver_probe(&amiga_parallel_driver,
286 amiga_parallel_probe);
287}
288
289module_init(amiga_parallel_init);
290
291static void __exit amiga_parallel_exit(void)
292{
293 platform_driver_unregister(&amiga_parallel_driver);
294}
295
296module_exit(amiga_parallel_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298MODULE_AUTHOR("Joerg Dorchain <joerg@dorchain.net>");
299MODULE_DESCRIPTION("Parport Driver for Amiga builtin Port");
300MODULE_SUPPORTED_DEVICE("Amiga builtin Parallel Port");
301MODULE_LICENSE("GPL");
Geert Uytterhoeven6f8221c2009-04-05 13:13:20 +0200302MODULE_ALIAS("platform:amiga-parallel");