Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 * parport-to-butterfly adapter
  3 *
  4 * Copyright (C) 2005 David Brownell
  5 *
  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.
 10 *
 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.
 15 *
 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.
 19 */
 20#include <linux/kernel.h>
 21#include <linux/init.h>
 22#include <linux/delay.h>
 
 23#include <linux/device.h>
 24#include <linux/parport.h>
 25
 26#include <linux/sched.h>
 27#include <linux/spi/spi.h>
 28#include <linux/spi/spi_bitbang.h>
 29#include <linux/spi/flash.h>
 30
 31#include <linux/mtd/partitions.h>
 32
 33
 34/*
 35 * This uses SPI to talk with an "AVR Butterfly", which is a $US20 card
 36 * with a battery powered AVR microcontroller and lots of goodies.  You
 37 * can use GCC to develop firmware for this.
 38 *
 39 * See Documentation/spi/butterfly for information about how to build
 40 * and use this custom parallel port cable.
 41 */
 42
 43
 44/* DATA output bits (pins 2..9 == D0..D7) */
 45#define	butterfly_nreset (1 << 1)		/* pin 3 */
 46
 47#define	spi_sck_bit	(1 << 0)		/* pin 2 */
 48#define	spi_mosi_bit	(1 << 7)		/* pin 9 */
 49
 50#define	vcc_bits	((1 << 6) | (1 << 5))	/* pins 7, 8 */
 51
 52/* STATUS input bits */
 53#define	spi_miso_bit	PARPORT_STATUS_BUSY	/* pin 11 */
 54
 55/* CONTROL output bits */
 56#define	spi_cs_bit	PARPORT_CONTROL_SELECT	/* pin 17 */
 57
 58
 59
 60static inline struct butterfly *spidev_to_pp(struct spi_device *spi)
 61{
 62	return spi->controller_data;
 63}
 64
 65
 66struct butterfly {
 67	/* REVISIT ... for now, this must be first */
 68	struct spi_bitbang	bitbang;
 69
 70	struct parport		*port;
 71	struct pardevice	*pd;
 72
 73	u8			lastbyte;
 74
 75	struct spi_device	*dataflash;
 76	struct spi_device	*butterfly;
 77	struct spi_board_info	info[2];
 78
 79};
 80
 81/*----------------------------------------------------------------------*/
 82
 83static inline void
 84setsck(struct spi_device *spi, int is_on)
 85{
 86	struct butterfly	*pp = spidev_to_pp(spi);
 87	u8			bit, byte = pp->lastbyte;
 88
 89	bit = spi_sck_bit;
 90
 91	if (is_on)
 92		byte |= bit;
 93	else
 94		byte &= ~bit;
 95	parport_write_data(pp->port, byte);
 96	pp->lastbyte = byte;
 97}
 98
 99static inline void
100setmosi(struct spi_device *spi, int is_on)
101{
102	struct butterfly	*pp = spidev_to_pp(spi);
103	u8			bit, byte = pp->lastbyte;
104
105	bit = spi_mosi_bit;
106
107	if (is_on)
108		byte |= bit;
109	else
110		byte &= ~bit;
111	parport_write_data(pp->port, byte);
112	pp->lastbyte = byte;
113}
114
115static inline int getmiso(struct spi_device *spi)
116{
117	struct butterfly	*pp = spidev_to_pp(spi);
118	int			value;
119	u8			bit;
120
121	bit = spi_miso_bit;
122
123	/* only STATUS_BUSY is NOT negated */
124	value = !(parport_read_status(pp->port) & bit);
125	return (bit == PARPORT_STATUS_BUSY) ? value : !value;
126}
127
128static void butterfly_chipselect(struct spi_device *spi, int value)
129{
130	struct butterfly	*pp = spidev_to_pp(spi);
131
132	/* set default clock polarity */
133	if (value != BITBANG_CS_INACTIVE)
134		setsck(spi, spi->mode & SPI_CPOL);
135
136	/* here, value == "activate or not";
137	 * most PARPORT_CONTROL_* bits are negated, so we must
138	 * morph it to value == "bit value to write in control register"
139	 */
140	if (spi_cs_bit == PARPORT_CONTROL_INIT)
141		value = !value;
142
143	parport_frob_control(pp->port, spi_cs_bit, value ? spi_cs_bit : 0);
144}
145
146
147/* we only needed to implement one mode here, and choose SPI_MODE_0 */
148
149#define	spidelay(X)	do{}while(0)
150//#define	spidelay	ndelay
151
152#include "spi-bitbang-txrx.h"
153
154static u32
155butterfly_txrx_word_mode0(struct spi_device *spi,
156		unsigned nsecs,
157		u32 word, u8 bits)
158{
159	return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits);
160}
161
162/*----------------------------------------------------------------------*/
163
164/* override default partitioning with cmdlinepart */
165static struct mtd_partition partitions[] = { {
166	/* JFFS2 wants partitions of 4*N blocks for this device,
167	 * so sectors 0 and 1 can't be partitions by themselves.
168	 */
169
170	/* sector 0 = 8 pages * 264 bytes/page (1 block)
171	 * sector 1 = 248 pages * 264 bytes/page
172	 */
173	.name		= "bookkeeping",	// 66 KB
174	.offset		= 0,
175	.size		= (8 + 248) * 264,
176//	.mask_flags	= MTD_WRITEABLE,
177}, {
178	/* sector 2 = 256 pages * 264 bytes/page
179	 * sectors 3-5 = 512 pages * 264 bytes/page
180	 */
181	.name		= "filesystem",		// 462 KB
182	.offset		= MTDPART_OFS_APPEND,
183	.size		= MTDPART_SIZ_FULL,
184} };
185
186static struct flash_platform_data flash = {
187	.name		= "butterflash",
188	.parts		= partitions,
189	.nr_parts	= ARRAY_SIZE(partitions),
190};
191
192
193/* REVISIT remove this ugly global and its "only one" limitation */
194static struct butterfly *butterfly;
195
196static void butterfly_attach(struct parport *p)
197{
198	struct pardevice	*pd;
199	int			status;
200	struct butterfly	*pp;
201	struct spi_master	*master;
202	struct device		*dev = p->physport->dev;
 
203
204	if (butterfly || !dev)
205		return;
206
207	/* REVISIT:  this just _assumes_ a butterfly is there ... no probe,
208	 * and no way to be selective about what it binds to.
209	 */
210
211	master = spi_alloc_master(dev, sizeof *pp);
212	if (!master) {
213		status = -ENOMEM;
214		goto done;
215	}
216	pp = spi_master_get_devdata(master);
217
218	/*
219	 * SPI and bitbang hookup
220	 *
221	 * use default setup(), cleanup(), and transfer() methods; and
222	 * only bother implementing mode 0.  Start it later.
223	 */
224	master->bus_num = 42;
225	master->num_chipselect = 2;
226
227	pp->bitbang.master = spi_master_get(master);
228	pp->bitbang.chipselect = butterfly_chipselect;
229	pp->bitbang.txrx_word[SPI_MODE_0] = butterfly_txrx_word_mode0;
230
231	/*
232	 * parport hookup
233	 */
234	pp->port = p;
235	pd = parport_register_device(p, "spi_butterfly",
236			NULL, NULL, NULL,
237			0 /* FLAGS */, pp);
238	if (!pd) {
239		status = -ENOMEM;
240		goto clean0;
241	}
242	pp->pd = pd;
243
244	status = parport_claim(pd);
245	if (status < 0)
246		goto clean1;
247
248	/*
249	 * Butterfly reset, powerup, run firmware
250	 */
251	pr_debug("%s: powerup/reset Butterfly\n", p->name);
252
253	/* nCS for dataflash (this bit is inverted on output) */
254	parport_frob_control(pp->port, spi_cs_bit, 0);
255
256	/* stabilize power with chip in reset (nRESET), and
257	 * spi_sck_bit clear (CPOL=0)
258	 */
259	pp->lastbyte |= vcc_bits;
260	parport_write_data(pp->port, pp->lastbyte);
261	msleep(5);
262
263	/* take it out of reset; assume long reset delay */
264	pp->lastbyte |= butterfly_nreset;
265	parport_write_data(pp->port, pp->lastbyte);
266	msleep(100);
267
268
269	/*
270	 * Start SPI ... for now, hide that we're two physical busses.
271	 */
272	status = spi_bitbang_start(&pp->bitbang);
273	if (status < 0)
274		goto clean2;
275
276	/* Bus 1 lets us talk to at45db041b (firmware disables AVR SPI), AVR
277	 * (firmware resets at45, acts as spi slave) or neither (we ignore
278	 * both, AVR uses AT45).  Here we expect firmware for the first option.
279	 */
280
281	pp->info[0].max_speed_hz = 15 * 1000 * 1000;
282	strcpy(pp->info[0].modalias, "mtd_dataflash");
283	pp->info[0].platform_data = &flash;
284	pp->info[0].chip_select = 1;
285	pp->info[0].controller_data = pp;
286	pp->dataflash = spi_new_device(pp->bitbang.master, &pp->info[0]);
287	if (pp->dataflash)
288		pr_debug("%s: dataflash at %s\n", p->name,
289				dev_name(&pp->dataflash->dev));
290
291	// dev_info(_what?_, ...)
292	pr_info("%s: AVR Butterfly\n", p->name);
293	butterfly = pp;
294	return;
295
296clean2:
297	/* turn off VCC */
298	parport_write_data(pp->port, 0);
299
300	parport_release(pp->pd);
301clean1:
302	parport_unregister_device(pd);
303clean0:
304	(void) spi_master_put(pp->bitbang.master);
305done:
306	pr_debug("%s: butterfly probe, fail %d\n", p->name, status);
307}
308
309static void butterfly_detach(struct parport *p)
310{
311	struct butterfly	*pp;
312	int			status;
313
314	/* FIXME this global is ugly ... but, how to quickly get from
315	 * the parport to the "struct butterfly" associated with it?
316	 * "old school" driver-internal device lists?
317	 */
318	if (!butterfly || butterfly->port != p)
319		return;
320	pp = butterfly;
321	butterfly = NULL;
322
323	/* stop() unregisters child devices too */
324	status = spi_bitbang_stop(&pp->bitbang);
325
326	/* turn off VCC */
327	parport_write_data(pp->port, 0);
328	msleep(10);
329
330	parport_release(pp->pd);
331	parport_unregister_device(pp->pd);
332
333	(void) spi_master_put(pp->bitbang.master);
334}
335
336static struct parport_driver butterfly_driver = {
337	.name =		"spi_butterfly",
338	.attach =	butterfly_attach,
339	.detach =	butterfly_detach,
 
340};
341
342
343static int __init butterfly_init(void)
344{
345	return parport_register_driver(&butterfly_driver);
346}
347device_initcall(butterfly_init);
348
349static void __exit butterfly_exit(void)
350{
351	parport_unregister_driver(&butterfly_driver);
352}
353module_exit(butterfly_exit);
354
355MODULE_DESCRIPTION("Parport Adapter driver for AVR Butterfly");
356MODULE_LICENSE("GPL");
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * parport-to-butterfly adapter
  4 *
  5 * Copyright (C) 2005 David Brownell
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  6 */
  7#include <linux/kernel.h>
  8#include <linux/init.h>
  9#include <linux/delay.h>
 10#include <linux/module.h>
 11#include <linux/device.h>
 12#include <linux/parport.h>
 13
 14#include <linux/sched.h>
 15#include <linux/spi/spi.h>
 16#include <linux/spi/spi_bitbang.h>
 17#include <linux/spi/flash.h>
 18
 19#include <linux/mtd/partitions.h>
 20
 
 21/*
 22 * This uses SPI to talk with an "AVR Butterfly", which is a $US20 card
 23 * with a battery powered AVR microcontroller and lots of goodies.  You
 24 * can use GCC to develop firmware for this.
 25 *
 26 * See Documentation/spi/butterfly.rst for information about how to build
 27 * and use this custom parallel port cable.
 28 */
 29
 
 30/* DATA output bits (pins 2..9 == D0..D7) */
 31#define	butterfly_nreset (1 << 1)		/* pin 3 */
 32
 33#define	spi_sck_bit	(1 << 0)		/* pin 2 */
 34#define	spi_mosi_bit	(1 << 7)		/* pin 9 */
 35
 36#define	vcc_bits	((1 << 6) | (1 << 5))	/* pins 7, 8 */
 37
 38/* STATUS input bits */
 39#define	spi_miso_bit	PARPORT_STATUS_BUSY	/* pin 11 */
 40
 41/* CONTROL output bits */
 42#define	spi_cs_bit	PARPORT_CONTROL_SELECT	/* pin 17 */
 43
 
 
 44static inline struct butterfly *spidev_to_pp(struct spi_device *spi)
 45{
 46	return spi->controller_data;
 47}
 48
 
 49struct butterfly {
 50	/* REVISIT ... for now, this must be first */
 51	struct spi_bitbang	bitbang;
 52
 53	struct parport		*port;
 54	struct pardevice	*pd;
 55
 56	u8			lastbyte;
 57
 58	struct spi_device	*dataflash;
 59	struct spi_device	*butterfly;
 60	struct spi_board_info	info[2];
 61
 62};
 63
 64/*----------------------------------------------------------------------*/
 65
 66static inline void
 67setsck(struct spi_device *spi, int is_on)
 68{
 69	struct butterfly	*pp = spidev_to_pp(spi);
 70	u8			bit, byte = pp->lastbyte;
 71
 72	bit = spi_sck_bit;
 73
 74	if (is_on)
 75		byte |= bit;
 76	else
 77		byte &= ~bit;
 78	parport_write_data(pp->port, byte);
 79	pp->lastbyte = byte;
 80}
 81
 82static inline void
 83setmosi(struct spi_device *spi, int is_on)
 84{
 85	struct butterfly	*pp = spidev_to_pp(spi);
 86	u8			bit, byte = pp->lastbyte;
 87
 88	bit = spi_mosi_bit;
 89
 90	if (is_on)
 91		byte |= bit;
 92	else
 93		byte &= ~bit;
 94	parport_write_data(pp->port, byte);
 95	pp->lastbyte = byte;
 96}
 97
 98static inline int getmiso(struct spi_device *spi)
 99{
100	struct butterfly	*pp = spidev_to_pp(spi);
101	int			value;
102	u8			bit;
103
104	bit = spi_miso_bit;
105
106	/* only STATUS_BUSY is NOT negated */
107	value = !(parport_read_status(pp->port) & bit);
108	return (bit == PARPORT_STATUS_BUSY) ? value : !value;
109}
110
111static void butterfly_chipselect(struct spi_device *spi, int value)
112{
113	struct butterfly	*pp = spidev_to_pp(spi);
114
115	/* set default clock polarity */
116	if (value != BITBANG_CS_INACTIVE)
117		setsck(spi, spi->mode & SPI_CPOL);
118
119	/* here, value == "activate or not";
120	 * most PARPORT_CONTROL_* bits are negated, so we must
121	 * morph it to value == "bit value to write in control register"
122	 */
123	if (spi_cs_bit == PARPORT_CONTROL_INIT)
124		value = !value;
125
126	parport_frob_control(pp->port, spi_cs_bit, value ? spi_cs_bit : 0);
127}
128
 
129/* we only needed to implement one mode here, and choose SPI_MODE_0 */
130
131#define spidelay(X)	do { } while (0)
132/* #define spidelay	ndelay */
133
134#include "spi-bitbang-txrx.h"
135
136static u32
137butterfly_txrx_word_mode0(struct spi_device *spi, unsigned nsecs, u32 word,
138			  u8 bits, unsigned flags)
 
139{
140	return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
141}
142
143/*----------------------------------------------------------------------*/
144
145/* override default partitioning with cmdlinepart */
146static struct mtd_partition partitions[] = { {
147	/* JFFS2 wants partitions of 4*N blocks for this device,
148	 * so sectors 0 and 1 can't be partitions by themselves.
149	 */
150
151	/* sector 0 = 8 pages * 264 bytes/page (1 block)
152	 * sector 1 = 248 pages * 264 bytes/page
153	 */
154	.name		= "bookkeeping",	/* 66 KB */
155	.offset		= 0,
156	.size		= (8 + 248) * 264,
157	/* .mask_flags	= MTD_WRITEABLE, */
158}, {
159	/* sector 2 = 256 pages * 264 bytes/page
160	 * sectors 3-5 = 512 pages * 264 bytes/page
161	 */
162	.name		= "filesystem",		/* 462 KB */
163	.offset		= MTDPART_OFS_APPEND,
164	.size		= MTDPART_SIZ_FULL,
165} };
166
167static struct flash_platform_data flash = {
168	.name		= "butterflash",
169	.parts		= partitions,
170	.nr_parts	= ARRAY_SIZE(partitions),
171};
172
 
173/* REVISIT remove this ugly global and its "only one" limitation */
174static struct butterfly *butterfly;
175
176static void butterfly_attach(struct parport *p)
177{
178	struct pardevice	*pd;
179	int			status;
180	struct butterfly	*pp;
181	struct spi_master	*master;
182	struct device		*dev = p->physport->dev;
183	struct pardev_cb	butterfly_cb;
184
185	if (butterfly || !dev)
186		return;
187
188	/* REVISIT:  this just _assumes_ a butterfly is there ... no probe,
189	 * and no way to be selective about what it binds to.
190	 */
191
192	master = spi_alloc_master(dev, sizeof(*pp));
193	if (!master) {
194		status = -ENOMEM;
195		goto done;
196	}
197	pp = spi_master_get_devdata(master);
198
199	/*
200	 * SPI and bitbang hookup
201	 *
202	 * use default setup(), cleanup(), and transfer() methods; and
203	 * only bother implementing mode 0.  Start it later.
204	 */
205	master->bus_num = 42;
206	master->num_chipselect = 2;
207
208	pp->bitbang.master = master;
209	pp->bitbang.chipselect = butterfly_chipselect;
210	pp->bitbang.txrx_word[SPI_MODE_0] = butterfly_txrx_word_mode0;
211
212	/*
213	 * parport hookup
214	 */
215	pp->port = p;
216	memset(&butterfly_cb, 0, sizeof(butterfly_cb));
217	butterfly_cb.private = pp;
218	pd = parport_register_dev_model(p, "spi_butterfly", &butterfly_cb, 0);
219	if (!pd) {
220		status = -ENOMEM;
221		goto clean0;
222	}
223	pp->pd = pd;
224
225	status = parport_claim(pd);
226	if (status < 0)
227		goto clean1;
228
229	/*
230	 * Butterfly reset, powerup, run firmware
231	 */
232	pr_debug("%s: powerup/reset Butterfly\n", p->name);
233
234	/* nCS for dataflash (this bit is inverted on output) */
235	parport_frob_control(pp->port, spi_cs_bit, 0);
236
237	/* stabilize power with chip in reset (nRESET), and
238	 * spi_sck_bit clear (CPOL=0)
239	 */
240	pp->lastbyte |= vcc_bits;
241	parport_write_data(pp->port, pp->lastbyte);
242	msleep(5);
243
244	/* take it out of reset; assume long reset delay */
245	pp->lastbyte |= butterfly_nreset;
246	parport_write_data(pp->port, pp->lastbyte);
247	msleep(100);
248
 
249	/*
250	 * Start SPI ... for now, hide that we're two physical busses.
251	 */
252	status = spi_bitbang_start(&pp->bitbang);
253	if (status < 0)
254		goto clean2;
255
256	/* Bus 1 lets us talk to at45db041b (firmware disables AVR SPI), AVR
257	 * (firmware resets at45, acts as spi slave) or neither (we ignore
258	 * both, AVR uses AT45).  Here we expect firmware for the first option.
259	 */
260
261	pp->info[0].max_speed_hz = 15 * 1000 * 1000;
262	strcpy(pp->info[0].modalias, "mtd_dataflash");
263	pp->info[0].platform_data = &flash;
264	pp->info[0].chip_select = 1;
265	pp->info[0].controller_data = pp;
266	pp->dataflash = spi_new_device(pp->bitbang.master, &pp->info[0]);
267	if (pp->dataflash)
268		pr_debug("%s: dataflash at %s\n", p->name,
269			 dev_name(&pp->dataflash->dev));
270
 
271	pr_info("%s: AVR Butterfly\n", p->name);
272	butterfly = pp;
273	return;
274
275clean2:
276	/* turn off VCC */
277	parport_write_data(pp->port, 0);
278
279	parport_release(pp->pd);
280clean1:
281	parport_unregister_device(pd);
282clean0:
283	spi_master_put(pp->bitbang.master);
284done:
285	pr_debug("%s: butterfly probe, fail %d\n", p->name, status);
286}
287
288static void butterfly_detach(struct parport *p)
289{
290	struct butterfly	*pp;
 
291
292	/* FIXME this global is ugly ... but, how to quickly get from
293	 * the parport to the "struct butterfly" associated with it?
294	 * "old school" driver-internal device lists?
295	 */
296	if (!butterfly || butterfly->port != p)
297		return;
298	pp = butterfly;
299	butterfly = NULL;
300
301	/* stop() unregisters child devices too */
302	spi_bitbang_stop(&pp->bitbang);
303
304	/* turn off VCC */
305	parport_write_data(pp->port, 0);
306	msleep(10);
307
308	parport_release(pp->pd);
309	parport_unregister_device(pp->pd);
310
311	spi_master_put(pp->bitbang.master);
312}
313
314static struct parport_driver butterfly_driver = {
315	.name =		"spi_butterfly",
316	.match_port =	butterfly_attach,
317	.detach =	butterfly_detach,
318	.devmodel = true,
319};
320module_parport_driver(butterfly_driver);
 
 
 
 
 
 
 
 
 
 
 
 
321
322MODULE_DESCRIPTION("Parport Adapter driver for AVR Butterfly");
323MODULE_LICENSE("GPL");