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