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