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