2 * parport-to-butterfly adapter
4 * Copyright (C) 2005 David Brownell
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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>
27 #include <linux/sched.h>
28 #include <linux/spi/spi.h>
29 #include <linux/spi/spi_bitbang.h>
30 #include <linux/spi/flash.h>
32 #include <linux/mtd/partitions.h>
36 * This uses SPI to talk with an "AVR Butterfly", which is a $US20 card
37 * with a battery powered AVR microcontroller and lots of goodies. You
38 * can use GCC to develop firmware for this.
40 * See Documentation/spi/butterfly for information about how to build
41 * and use this custom parallel port cable.
45 /* DATA output bits (pins 2..9 == D0..D7) */
46 #define butterfly_nreset (1 << 1) /* pin 3 */
48 #define spi_sck_bit (1 << 0) /* pin 2 */
49 #define spi_mosi_bit (1 << 7) /* pin 9 */
51 #define vcc_bits ((1 << 6) | (1 << 5)) /* pins 7, 8 */
53 /* STATUS input bits */
54 #define spi_miso_bit PARPORT_STATUS_BUSY /* pin 11 */
56 /* CONTROL output bits */
57 #define spi_cs_bit PARPORT_CONTROL_SELECT /* pin 17 */
61 static inline struct butterfly
*spidev_to_pp(struct spi_device
*spi
)
63 return spi
->controller_data
;
68 /* REVISIT ... for now, this must be first */
69 struct spi_bitbang bitbang
;
76 struct spi_device
*dataflash
;
77 struct spi_device
*butterfly
;
78 struct spi_board_info info
[2];
82 /*----------------------------------------------------------------------*/
85 setsck(struct spi_device
*spi
, int is_on
)
87 struct butterfly
*pp
= spidev_to_pp(spi
);
88 u8 bit
, byte
= pp
->lastbyte
;
96 parport_write_data(pp
->port
, byte
);
101 setmosi(struct spi_device
*spi
, int is_on
)
103 struct butterfly
*pp
= spidev_to_pp(spi
);
104 u8 bit
, byte
= pp
->lastbyte
;
112 parport_write_data(pp
->port
, byte
);
116 static inline int getmiso(struct spi_device
*spi
)
118 struct butterfly
*pp
= spidev_to_pp(spi
);
124 /* only STATUS_BUSY is NOT negated */
125 value
= !(parport_read_status(pp
->port
) & bit
);
126 return (bit
== PARPORT_STATUS_BUSY
) ? value
: !value
;
129 static void butterfly_chipselect(struct spi_device
*spi
, int value
)
131 struct butterfly
*pp
= spidev_to_pp(spi
);
133 /* set default clock polarity */
134 if (value
!= BITBANG_CS_INACTIVE
)
135 setsck(spi
, spi
->mode
& SPI_CPOL
);
137 /* here, value == "activate or not";
138 * most PARPORT_CONTROL_* bits are negated, so we must
139 * morph it to value == "bit value to write in control register"
141 if (spi_cs_bit
== PARPORT_CONTROL_INIT
)
144 parport_frob_control(pp
->port
, spi_cs_bit
, value
? spi_cs_bit
: 0);
148 /* we only needed to implement one mode here, and choose SPI_MODE_0 */
150 #define spidelay(X) do { } while (0)
151 /* #define spidelay ndelay */
153 #include "spi-bitbang-txrx.h"
156 butterfly_txrx_word_mode0(struct spi_device
*spi
,
160 return bitbang_txrx_be_cpha0(spi
, nsecs
, 0, 0, word
, bits
);
163 /*----------------------------------------------------------------------*/
165 /* override default partitioning with cmdlinepart */
166 static struct mtd_partition partitions
[] = { {
167 /* JFFS2 wants partitions of 4*N blocks for this device,
168 * so sectors 0 and 1 can't be partitions by themselves.
171 /* sector 0 = 8 pages * 264 bytes/page (1 block)
172 * sector 1 = 248 pages * 264 bytes/page
174 .name
= "bookkeeping", /* 66 KB */
176 .size
= (8 + 248) * 264,
177 /* .mask_flags = MTD_WRITEABLE, */
179 /* sector 2 = 256 pages * 264 bytes/page
180 * sectors 3-5 = 512 pages * 264 bytes/page
182 .name
= "filesystem", /* 462 KB */
183 .offset
= MTDPART_OFS_APPEND
,
184 .size
= MTDPART_SIZ_FULL
,
187 static struct flash_platform_data flash
= {
188 .name
= "butterflash",
190 .nr_parts
= ARRAY_SIZE(partitions
),
194 /* REVISIT remove this ugly global and its "only one" limitation */
195 static struct butterfly
*butterfly
;
197 static void butterfly_attach(struct parport
*p
)
199 struct pardevice
*pd
;
201 struct butterfly
*pp
;
202 struct spi_master
*master
;
203 struct device
*dev
= p
->physport
->dev
;
205 if (butterfly
|| !dev
)
208 /* REVISIT: this just _assumes_ a butterfly is there ... no probe,
209 * and no way to be selective about what it binds to.
212 master
= spi_alloc_master(dev
, sizeof(*pp
));
217 pp
= spi_master_get_devdata(master
);
220 * SPI and bitbang hookup
222 * use default setup(), cleanup(), and transfer() methods; and
223 * only bother implementing mode 0. Start it later.
225 master
->bus_num
= 42;
226 master
->num_chipselect
= 2;
228 pp
->bitbang
.master
= master
;
229 pp
->bitbang
.chipselect
= butterfly_chipselect
;
230 pp
->bitbang
.txrx_word
[SPI_MODE_0
] = butterfly_txrx_word_mode0
;
236 pd
= parport_register_device(p
, "spi_butterfly",
245 status
= parport_claim(pd
);
250 * Butterfly reset, powerup, run firmware
252 pr_debug("%s: powerup/reset Butterfly\n", p
->name
);
254 /* nCS for dataflash (this bit is inverted on output) */
255 parport_frob_control(pp
->port
, spi_cs_bit
, 0);
257 /* stabilize power with chip in reset (nRESET), and
258 * spi_sck_bit clear (CPOL=0)
260 pp
->lastbyte
|= vcc_bits
;
261 parport_write_data(pp
->port
, pp
->lastbyte
);
264 /* take it out of reset; assume long reset delay */
265 pp
->lastbyte
|= butterfly_nreset
;
266 parport_write_data(pp
->port
, pp
->lastbyte
);
271 * Start SPI ... for now, hide that we're two physical busses.
273 status
= spi_bitbang_start(&pp
->bitbang
);
277 /* Bus 1 lets us talk to at45db041b (firmware disables AVR SPI), AVR
278 * (firmware resets at45, acts as spi slave) or neither (we ignore
279 * both, AVR uses AT45). Here we expect firmware for the first option.
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]);
289 pr_debug("%s: dataflash at %s\n", p
->name
,
290 dev_name(&pp
->dataflash
->dev
));
292 pr_info("%s: AVR Butterfly\n", p
->name
);
298 parport_write_data(pp
->port
, 0);
300 parport_release(pp
->pd
);
302 parport_unregister_device(pd
);
304 (void) spi_master_put(pp
->bitbang
.master
);
306 pr_debug("%s: butterfly probe, fail %d\n", p
->name
, status
);
309 static void butterfly_detach(struct parport
*p
)
311 struct butterfly
*pp
;
313 /* FIXME this global is ugly ... but, how to quickly get from
314 * the parport to the "struct butterfly" associated with it?
315 * "old school" driver-internal device lists?
317 if (!butterfly
|| butterfly
->port
!= p
)
322 /* stop() unregisters child devices too */
323 spi_bitbang_stop(&pp
->bitbang
);
326 parport_write_data(pp
->port
, 0);
329 parport_release(pp
->pd
);
330 parport_unregister_device(pp
->pd
);
332 (void) spi_master_put(pp
->bitbang
.master
);
335 static struct parport_driver butterfly_driver
= {
336 .name
= "spi_butterfly",
337 .attach
= butterfly_attach
,
338 .detach
= butterfly_detach
,
342 static int __init
butterfly_init(void)
344 return parport_register_driver(&butterfly_driver
);
346 device_initcall(butterfly_init
);
348 static void __exit
butterfly_exit(void)
350 parport_unregister_driver(&butterfly_driver
);
352 module_exit(butterfly_exit
);
354 MODULE_DESCRIPTION("Parport Adapter driver for AVR Butterfly");
355 MODULE_LICENSE("GPL");