1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/delay.h>
23#include <linux/module.h>
24#include <linux/device.h>
25#include <linux/parport.h>
26
27#include <linux/sched.h>
28#include <linux/spi/spi.h>
29#include <linux/spi/spi_bitbang.h>
30#include <linux/spi/flash.h>
31
32#include <linux/mtd/partitions.h>
33
34
35
36
37
38
39
40
41
42
43
44
45
46#define butterfly_nreset (1 << 1)
47
48#define spi_sck_bit (1 << 0)
49#define spi_mosi_bit (1 << 7)
50
51#define vcc_bits ((1 << 6) | (1 << 5))
52
53
54#define spi_miso_bit PARPORT_STATUS_BUSY
55
56
57#define spi_cs_bit PARPORT_CONTROL_SELECT
58
59
60
61static inline struct butterfly *spidev_to_pp(struct spi_device *spi)
62{
63 return spi->controller_data;
64}
65
66
67struct butterfly {
68
69 struct spi_bitbang bitbang;
70
71 struct parport *port;
72 struct pardevice *pd;
73
74 u8 lastbyte;
75
76 struct spi_device *dataflash;
77 struct spi_device *butterfly;
78 struct spi_board_info info[2];
79
80};
81
82
83
84static inline void
85setsck(struct spi_device *spi, int is_on)
86{
87 struct butterfly *pp = spidev_to_pp(spi);
88 u8 bit, byte = pp->lastbyte;
89
90 bit = spi_sck_bit;
91
92 if (is_on)
93 byte |= bit;
94 else
95 byte &= ~bit;
96 parport_write_data(pp->port, byte);
97 pp->lastbyte = byte;
98}
99
100static inline void
101setmosi(struct spi_device *spi, int is_on)
102{
103 struct butterfly *pp = spidev_to_pp(spi);
104 u8 bit, byte = pp->lastbyte;
105
106 bit = spi_mosi_bit;
107
108 if (is_on)
109 byte |= bit;
110 else
111 byte &= ~bit;
112 parport_write_data(pp->port, byte);
113 pp->lastbyte = byte;
114}
115
116static inline int getmiso(struct spi_device *spi)
117{
118 struct butterfly *pp = spidev_to_pp(spi);
119 int value;
120 u8 bit;
121
122 bit = spi_miso_bit;
123
124
125 value = !(parport_read_status(pp->port) & bit);
126 return (bit == PARPORT_STATUS_BUSY) ? value : !value;
127}
128
129static void butterfly_chipselect(struct spi_device *spi, int value)
130{
131 struct butterfly *pp = spidev_to_pp(spi);
132
133
134 if (value != BITBANG_CS_INACTIVE)
135 setsck(spi, spi->mode & SPI_CPOL);
136
137
138
139
140
141 if (spi_cs_bit == PARPORT_CONTROL_INIT)
142 value = !value;
143
144 parport_frob_control(pp->port, spi_cs_bit, value ? spi_cs_bit : 0);
145}
146
147
148
149
150#define spidelay(X) do{}while(0)
151
152
153#include "spi-bitbang-txrx.h"
154
155static u32
156butterfly_txrx_word_mode0(struct spi_device *spi,
157 unsigned nsecs,
158 u32 word, u8 bits)
159{
160 return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits);
161}
162
163
164
165
166static struct mtd_partition partitions[] = { {
167
168
169
170
171
172
173
174 .name = "bookkeeping",
175 .offset = 0,
176 .size = (8 + 248) * 264,
177
178}, {
179
180
181
182 .name = "filesystem",
183 .offset = MTDPART_OFS_APPEND,
184 .size = MTDPART_SIZ_FULL,
185} };
186
187static struct flash_platform_data flash = {
188 .name = "butterflash",
189 .parts = partitions,
190 .nr_parts = ARRAY_SIZE(partitions),
191};
192
193
194
195static struct butterfly *butterfly;
196
197static void butterfly_attach(struct parport *p)
198{
199 struct pardevice *pd;
200 int status;
201 struct butterfly *pp;
202 struct spi_master *master;
203 struct device *dev = p->physport->dev;
204
205 if (butterfly || !dev)
206 return;
207
208
209
210
211
212 master = spi_alloc_master(dev, sizeof *pp);
213 if (!master) {
214 status = -ENOMEM;
215 goto done;
216 }
217 pp = spi_master_get_devdata(master);
218
219
220
221
222
223
224
225 master->bus_num = 42;
226 master->num_chipselect = 2;
227
228 pp->bitbang.master = spi_master_get(master);
229 pp->bitbang.chipselect = butterfly_chipselect;
230 pp->bitbang.txrx_word[SPI_MODE_0] = butterfly_txrx_word_mode0;
231
232
233
234
235 pp->port = p;
236 pd = parport_register_device(p, "spi_butterfly",
237 NULL, NULL, NULL,
238 0 , pp);
239 if (!pd) {
240 status = -ENOMEM;
241 goto clean0;
242 }
243 pp->pd = pd;
244
245 status = parport_claim(pd);
246 if (status < 0)
247 goto clean1;
248
249
250
251
252 pr_debug("%s: powerup/reset Butterfly\n", p->name);
253
254
255 parport_frob_control(pp->port, spi_cs_bit, 0);
256
257
258
259
260 pp->lastbyte |= vcc_bits;
261 parport_write_data(pp->port, pp->lastbyte);
262 msleep(5);
263
264
265 pp->lastbyte |= butterfly_nreset;
266 parport_write_data(pp->port, pp->lastbyte);
267 msleep(100);
268
269
270
271
272
273 status = spi_bitbang_start(&pp->bitbang);
274 if (status < 0)
275 goto clean2;
276
277
278
279
280
281
282 pp->info[0].max_speed_hz = 15 * 1000 * 1000;
283 strcpy(pp->info[0].modalias, "mtd_dataflash");
284 pp->info[0].platform_data = &flash;
285 pp->info[0].chip_select = 1;
286 pp->info[0].controller_data = pp;
287 pp->dataflash = spi_new_device(pp->bitbang.master, &pp->info[0]);
288 if (pp->dataflash)
289 pr_debug("%s: dataflash at %s\n", p->name,
290 dev_name(&pp->dataflash->dev));
291
292
293 pr_info("%s: AVR Butterfly\n", p->name);
294 butterfly = pp;
295 return;
296
297clean2:
298
299 parport_write_data(pp->port, 0);
300
301 parport_release(pp->pd);
302clean1:
303 parport_unregister_device(pd);
304clean0:
305 (void) spi_master_put(pp->bitbang.master);
306done:
307 pr_debug("%s: butterfly probe, fail %d\n", p->name, status);
308}
309
310static void butterfly_detach(struct parport *p)
311{
312 struct butterfly *pp;
313 int status;
314
315
316
317
318
319 if (!butterfly || butterfly->port != p)
320 return;
321 pp = butterfly;
322 butterfly = NULL;
323
324
325 status = spi_bitbang_stop(&pp->bitbang);
326
327
328 parport_write_data(pp->port, 0);
329 msleep(10);
330
331 parport_release(pp->pd);
332 parport_unregister_device(pp->pd);
333
334 (void) spi_master_put(pp->bitbang.master);
335}
336
337static struct parport_driver butterfly_driver = {
338 .name = "spi_butterfly",
339 .attach = butterfly_attach,
340 .detach = butterfly_detach,
341};
342
343
344static int __init butterfly_init(void)
345{
346 return parport_register_driver(&butterfly_driver);
347}
348device_initcall(butterfly_init);
349
350static void __exit butterfly_exit(void)
351{
352 parport_unregister_driver(&butterfly_driver);
353}
354module_exit(butterfly_exit);
355
356MODULE_DESCRIPTION("Parport Adapter driver for AVR Butterfly");
357MODULE_LICENSE("GPL");
358