blob: 8081f96bd1d5add797eb482446f55850cbc248f1 [file] [log] [blame]
David Brownell2e10c842006-01-11 11:23:49 -08001/*
Grant Likelyca632f52011-06-06 01:16:30 -06002 * parport-to-butterfly adapter
David Brownell2e10c842006-01-11 11:23:49 -08003 *
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 */
David Brownell2e10c842006-01-11 11:23:49 -080020#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/delay.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040023#include <linux/module.h>
David Brownellda675292007-05-08 00:27:42 -070024#include <linux/device.h>
David Brownell2e10c842006-01-11 11:23:49 -080025#include <linux/parport.h>
26
Al Viro914e2632006-10-18 13:55:46 -040027#include <linux/sched.h>
David Brownell2e10c842006-01-11 11:23:49 -080028#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 * 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.
39 *
40 * See Documentation/spi/butterfly for information about how to build
41 * and use this custom parallel port cable.
42 */
43
David Brownell2e10c842006-01-11 11:23:49 -080044
45/* DATA output bits (pins 2..9 == D0..D7) */
46#define butterfly_nreset (1 << 1) /* pin 3 */
47
48#define spi_sck_bit (1 << 0) /* pin 2 */
49#define spi_mosi_bit (1 << 7) /* pin 9 */
50
David Brownell2e10c842006-01-11 11:23:49 -080051#define vcc_bits ((1 << 6) | (1 << 5)) /* pins 7, 8 */
52
53/* STATUS input bits */
54#define spi_miso_bit PARPORT_STATUS_BUSY /* pin 11 */
55
David Brownell2e10c842006-01-11 11:23:49 -080056/* CONTROL output bits */
57#define spi_cs_bit PARPORT_CONTROL_SELECT /* pin 17 */
David Brownell2e10c842006-01-11 11:23:49 -080058
59
60
61static inline struct butterfly *spidev_to_pp(struct spi_device *spi)
62{
63 return spi->controller_data;
64}
65
David Brownell2e10c842006-01-11 11:23:49 -080066
67struct butterfly {
68 /* REVISIT ... for now, this must be first */
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
David Brownell2e10c842006-01-11 11:23:49 -080084static 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
David Brownell735ce952007-05-08 00:32:13 -070090 bit = spi_sck_bit;
David Brownell2e10c842006-01-11 11:23:49 -080091
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
David Brownell735ce952007-05-08 00:32:13 -0700106 bit = spi_mosi_bit;
David Brownell2e10c842006-01-11 11:23:49 -0800107
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
David Brownell735ce952007-05-08 00:32:13 -0700122 bit = spi_miso_bit;
David Brownell2e10c842006-01-11 11:23:49 -0800123
124 /* only STATUS_BUSY is NOT negated */
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 /* set default clock polarity */
David Brownell9c1da3c2006-01-21 13:21:43 -0800134 if (value != BITBANG_CS_INACTIVE)
David Brownell2e10c842006-01-11 11:23:49 -0800135 setsck(spi, spi->mode & SPI_CPOL);
136
David Brownell9c1da3c2006-01-21 13:21:43 -0800137 /* 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"
140 */
David Brownell2e10c842006-01-11 11:23:49 -0800141 if (spi_cs_bit == PARPORT_CONTROL_INIT)
142 value = !value;
143
David Brownell2e10c842006-01-11 11:23:49 -0800144 parport_frob_control(pp->port, spi_cs_bit, value ? spi_cs_bit : 0);
145}
146
147
148/* we only needed to implement one mode here, and choose SPI_MODE_0 */
149
Jingoo Hanac8ed232013-10-14 10:34:24 +0900150#define spidelay(X) do { } while (0)
151/* #define spidelay ndelay */
David Brownell2e10c842006-01-11 11:23:49 -0800152
Grant Likelyca632f52011-06-06 01:16:30 -0600153#include "spi-bitbang-txrx.h"
David Brownell2e10c842006-01-11 11:23:49 -0800154
155static u32
156butterfly_txrx_word_mode0(struct spi_device *spi,
157 unsigned nsecs,
158 u32 word, u8 bits)
159{
Marek Szyprowski04bb2a02010-06-30 14:27:32 -0600160 return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits);
David Brownell2e10c842006-01-11 11:23:49 -0800161}
162
163/*----------------------------------------------------------------------*/
164
165/* override default partitioning with cmdlinepart */
166static struct mtd_partition partitions[] = { {
David Brownell9c1da3c2006-01-21 13:21:43 -0800167 /* JFFS2 wants partitions of 4*N blocks for this device,
168 * so sectors 0 and 1 can't be partitions by themselves.
169 */
David Brownell2e10c842006-01-11 11:23:49 -0800170
171 /* sector 0 = 8 pages * 264 bytes/page (1 block)
172 * sector 1 = 248 pages * 264 bytes/page
173 */
Jingoo Hanac8ed232013-10-14 10:34:24 +0900174 .name = "bookkeeping", /* 66 KB */
David Brownell2e10c842006-01-11 11:23:49 -0800175 .offset = 0,
176 .size = (8 + 248) * 264,
Jingoo Hanac8ed232013-10-14 10:34:24 +0900177 /* .mask_flags = MTD_WRITEABLE, */
David Brownell2e10c842006-01-11 11:23:49 -0800178}, {
179 /* sector 2 = 256 pages * 264 bytes/page
180 * sectors 3-5 = 512 pages * 264 bytes/page
181 */
Jingoo Hanac8ed232013-10-14 10:34:24 +0900182 .name = "filesystem", /* 462 KB */
David Brownell2e10c842006-01-11 11:23:49 -0800183 .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/* REVISIT remove this ugly global and its "only one" limitation */
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;
David Brownellda675292007-05-08 00:27:42 -0700203 struct device *dev = p->physport->dev;
David Brownell2e10c842006-01-11 11:23:49 -0800204
David Brownellda675292007-05-08 00:27:42 -0700205 if (butterfly || !dev)
David Brownell2e10c842006-01-11 11:23:49 -0800206 return;
207
208 /* REVISIT: this just _assumes_ a butterfly is there ... no probe,
209 * and no way to be selective about what it binds to.
210 */
211
Jingoo Hanac8ed232013-10-14 10:34:24 +0900212 master = spi_alloc_master(dev, sizeof(*pp));
David Brownell2e10c842006-01-11 11:23:49 -0800213 if (!master) {
214 status = -ENOMEM;
215 goto done;
216 }
217 pp = spi_master_get_devdata(master);
218
219 /*
220 * SPI and bitbang hookup
221 *
222 * use default setup(), cleanup(), and transfer() methods; and
223 * only bother implementing mode 0. Start it later.
224 */
225 master->bus_num = 42;
226 master->num_chipselect = 2;
227
Axel Lin94c69f72013-09-10 15:43:41 +0800228 pp->bitbang.master = master;
David Brownell2e10c842006-01-11 11:23:49 -0800229 pp->bitbang.chipselect = butterfly_chipselect;
230 pp->bitbang.txrx_word[SPI_MODE_0] = butterfly_txrx_word_mode0;
231
232 /*
233 * parport hookup
234 */
235 pp->port = p;
236 pd = parport_register_device(p, "spi_butterfly",
237 NULL, NULL, NULL,
238 0 /* FLAGS */, 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 * Butterfly reset, powerup, run firmware
251 */
252 pr_debug("%s: powerup/reset Butterfly\n", p->name);
253
254 /* nCS for dataflash (this bit is inverted on output) */
255 parport_frob_control(pp->port, spi_cs_bit, 0);
256
257 /* stabilize power with chip in reset (nRESET), and
David Brownell735ce952007-05-08 00:32:13 -0700258 * spi_sck_bit clear (CPOL=0)
David Brownell2e10c842006-01-11 11:23:49 -0800259 */
260 pp->lastbyte |= vcc_bits;
261 parport_write_data(pp->port, pp->lastbyte);
262 msleep(5);
263
264 /* take it out of reset; assume long reset delay */
265 pp->lastbyte |= butterfly_nreset;
266 parport_write_data(pp->port, pp->lastbyte);
267 msleep(100);
268
269
270 /*
271 * Start SPI ... for now, hide that we're two physical busses.
272 */
273 status = spi_bitbang_start(&pp->bitbang);
274 if (status < 0)
275 goto clean2;
276
David Brownell9c1da3c2006-01-21 13:21:43 -0800277 /* 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.
David Brownell2e10c842006-01-11 11:23:49 -0800280 */
Ben Dooks1fc75472006-05-20 15:00:17 -0700281
David Brownell2e10c842006-01-11 11:23:49 -0800282 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,
Kay Sievers35f74fc2009-01-06 10:44:37 -0800290 dev_name(&pp->dataflash->dev));
David Brownell2e10c842006-01-11 11:23:49 -0800291
David Brownell2e10c842006-01-11 11:23:49 -0800292 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:
David Brownell2e10c842006-01-11 11:23:49 -0800306 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;
David Brownell2e10c842006-01-11 11:23:49 -0800312 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
David Brownell9c1da3c2006-01-21 13:21:43 -0800323 /* stop() unregisters child devices too */
David Brownell2e10c842006-01-11 11:23:49 -0800324 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
David Brownell2e10c842006-01-11 11:23:49 -0800333 (void) spi_master_put(pp->bitbang.master);
David Brownell2e10c842006-01-11 11:23:49 -0800334}
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
David Brownell9c1da3c2006-01-21 13:21:43 -0800355MODULE_DESCRIPTION("Parport Adapter driver for AVR Butterfly");
David Brownell2e10c842006-01-11 11:23:49 -0800356MODULE_LICENSE("GPL");