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