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 #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>
23 #include <linux/sched.h>
24 #include <linux/spi/spi.h>
25 #include <linux/spi/spi_bitbang.h>
26 #include <linux/spi/flash.h>
28 #include <linux/mtd/partitions.h>
31 * This uses SPI to talk with an "AVR Butterfly", which is a $US20 card
32 * with a battery powered AVR microcontroller and lots of goodies. You
33 * can use GCC to develop firmware for this.
35 * See Documentation/spi/butterfly for information about how to build
36 * and use this custom parallel port cable.
39 /* DATA output bits (pins 2..9 == D0..D7) */
40 #define butterfly_nreset (1 << 1) /* pin 3 */
42 #define spi_sck_bit (1 << 0) /* pin 2 */
43 #define spi_mosi_bit (1 << 7) /* pin 9 */
45 #define vcc_bits ((1 << 6) | (1 << 5)) /* pins 7, 8 */
47 /* STATUS input bits */
48 #define spi_miso_bit PARPORT_STATUS_BUSY /* pin 11 */
50 /* CONTROL output bits */
51 #define spi_cs_bit PARPORT_CONTROL_SELECT /* pin 17 */
53 static inline struct butterfly *spidev_to_pp(struct spi_device *spi)
55 return spi->controller_data;
59 /* REVISIT ... for now, this must be first */
60 struct spi_bitbang bitbang;
67 struct spi_device *dataflash;
68 struct spi_device *butterfly;
69 struct spi_board_info info[2];
73 /*----------------------------------------------------------------------*/
76 setsck(struct spi_device *spi, int is_on)
78 struct butterfly *pp = spidev_to_pp(spi);
79 u8 bit, byte = pp->lastbyte;
87 parport_write_data(pp->port, byte);
92 setmosi(struct spi_device *spi, int is_on)
94 struct butterfly *pp = spidev_to_pp(spi);
95 u8 bit, byte = pp->lastbyte;
103 parport_write_data(pp->port, byte);
107 static inline int getmiso(struct spi_device *spi)
109 struct butterfly *pp = spidev_to_pp(spi);
115 /* only STATUS_BUSY is NOT negated */
116 value = !(parport_read_status(pp->port) & bit);
117 return (bit == PARPORT_STATUS_BUSY) ? value : !value;
120 static void butterfly_chipselect(struct spi_device *spi, int value)
122 struct butterfly *pp = spidev_to_pp(spi);
124 /* set default clock polarity */
125 if (value != BITBANG_CS_INACTIVE)
126 setsck(spi, spi->mode & SPI_CPOL);
128 /* here, value == "activate or not";
129 * most PARPORT_CONTROL_* bits are negated, so we must
130 * morph it to value == "bit value to write in control register"
132 if (spi_cs_bit == PARPORT_CONTROL_INIT)
135 parport_frob_control(pp->port, spi_cs_bit, value ? spi_cs_bit : 0);
138 /* we only needed to implement one mode here, and choose SPI_MODE_0 */
140 #define spidelay(X) do { } while (0)
141 /* #define spidelay ndelay */
143 #include "spi-bitbang-txrx.h"
146 butterfly_txrx_word_mode0(struct spi_device *spi, unsigned nsecs, u32 word,
149 return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits);
152 /*----------------------------------------------------------------------*/
154 /* override default partitioning with cmdlinepart */
155 static struct mtd_partition partitions[] = { {
156 /* JFFS2 wants partitions of 4*N blocks for this device,
157 * so sectors 0 and 1 can't be partitions by themselves.
160 /* sector 0 = 8 pages * 264 bytes/page (1 block)
161 * sector 1 = 248 pages * 264 bytes/page
163 .name = "bookkeeping", /* 66 KB */
165 .size = (8 + 248) * 264,
166 /* .mask_flags = MTD_WRITEABLE, */
168 /* sector 2 = 256 pages * 264 bytes/page
169 * sectors 3-5 = 512 pages * 264 bytes/page
171 .name = "filesystem", /* 462 KB */
172 .offset = MTDPART_OFS_APPEND,
173 .size = MTDPART_SIZ_FULL,
176 static struct flash_platform_data flash = {
177 .name = "butterflash",
179 .nr_parts = ARRAY_SIZE(partitions),
182 /* REVISIT remove this ugly global and its "only one" limitation */
183 static struct butterfly *butterfly;
185 static void butterfly_attach(struct parport *p)
187 struct pardevice *pd;
189 struct butterfly *pp;
190 struct spi_master *master;
191 struct device *dev = p->physport->dev;
193 if (butterfly || !dev)
196 /* REVISIT: this just _assumes_ a butterfly is there ... no probe,
197 * and no way to be selective about what it binds to.
200 master = spi_alloc_master(dev, sizeof(*pp));
205 pp = spi_master_get_devdata(master);
208 * SPI and bitbang hookup
210 * use default setup(), cleanup(), and transfer() methods; and
211 * only bother implementing mode 0. Start it later.
213 master->bus_num = 42;
214 master->num_chipselect = 2;
216 pp->bitbang.master = master;
217 pp->bitbang.chipselect = butterfly_chipselect;
218 pp->bitbang.txrx_word[SPI_MODE_0] = butterfly_txrx_word_mode0;
224 pd = parport_register_device(p, "spi_butterfly",
233 status = parport_claim(pd);
238 * Butterfly reset, powerup, run firmware
240 pr_debug("%s: powerup/reset Butterfly\n", p->name);
242 /* nCS for dataflash (this bit is inverted on output) */
243 parport_frob_control(pp->port, spi_cs_bit, 0);
245 /* stabilize power with chip in reset (nRESET), and
246 * spi_sck_bit clear (CPOL=0)
248 pp->lastbyte |= vcc_bits;
249 parport_write_data(pp->port, pp->lastbyte);
252 /* take it out of reset; assume long reset delay */
253 pp->lastbyte |= butterfly_nreset;
254 parport_write_data(pp->port, pp->lastbyte);
258 * Start SPI ... for now, hide that we're two physical busses.
260 status = spi_bitbang_start(&pp->bitbang);
264 /* Bus 1 lets us talk to at45db041b (firmware disables AVR SPI), AVR
265 * (firmware resets at45, acts as spi slave) or neither (we ignore
266 * both, AVR uses AT45). Here we expect firmware for the first option.
269 pp->info[0].max_speed_hz = 15 * 1000 * 1000;
270 strcpy(pp->info[0].modalias, "mtd_dataflash");
271 pp->info[0].platform_data = &flash;
272 pp->info[0].chip_select = 1;
273 pp->info[0].controller_data = pp;
274 pp->dataflash = spi_new_device(pp->bitbang.master, &pp->info[0]);
276 pr_debug("%s: dataflash at %s\n", p->name,
277 dev_name(&pp->dataflash->dev));
279 pr_info("%s: AVR Butterfly\n", p->name);
285 parport_write_data(pp->port, 0);
287 parport_release(pp->pd);
289 parport_unregister_device(pd);
291 spi_master_put(pp->bitbang.master);
293 pr_debug("%s: butterfly probe, fail %d\n", p->name, status);
296 static void butterfly_detach(struct parport *p)
298 struct butterfly *pp;
300 /* FIXME this global is ugly ... but, how to quickly get from
301 * the parport to the "struct butterfly" associated with it?
302 * "old school" driver-internal device lists?
304 if (!butterfly || butterfly->port != p)
309 /* stop() unregisters child devices too */
310 spi_bitbang_stop(&pp->bitbang);
313 parport_write_data(pp->port, 0);
316 parport_release(pp->pd);
317 parport_unregister_device(pp->pd);
319 spi_master_put(pp->bitbang.master);
322 static struct parport_driver butterfly_driver = {
323 .name = "spi_butterfly",
324 .attach = butterfly_attach,
325 .detach = butterfly_detach,
328 static int __init butterfly_init(void)
330 return parport_register_driver(&butterfly_driver);
332 device_initcall(butterfly_init);
334 static void __exit butterfly_exit(void)
336 parport_unregister_driver(&butterfly_driver);
338 module_exit(butterfly_exit);
340 MODULE_DESCRIPTION("Parport Adapter driver for AVR Butterfly");
341 MODULE_LICENSE("GPL");