18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * parport-to-butterfly adapter 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2005 David Brownell 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci#include <linux/kernel.h> 88c2ecf20Sopenharmony_ci#include <linux/init.h> 98c2ecf20Sopenharmony_ci#include <linux/delay.h> 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci#include <linux/device.h> 128c2ecf20Sopenharmony_ci#include <linux/parport.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/sched.h> 158c2ecf20Sopenharmony_ci#include <linux/spi/spi.h> 168c2ecf20Sopenharmony_ci#include <linux/spi/spi_bitbang.h> 178c2ecf20Sopenharmony_ci#include <linux/spi/flash.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <linux/mtd/partitions.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci/* 228c2ecf20Sopenharmony_ci * This uses SPI to talk with an "AVR Butterfly", which is a $US20 card 238c2ecf20Sopenharmony_ci * with a battery powered AVR microcontroller and lots of goodies. You 248c2ecf20Sopenharmony_ci * can use GCC to develop firmware for this. 258c2ecf20Sopenharmony_ci * 268c2ecf20Sopenharmony_ci * See Documentation/spi/butterfly.rst for information about how to build 278c2ecf20Sopenharmony_ci * and use this custom parallel port cable. 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci/* DATA output bits (pins 2..9 == D0..D7) */ 318c2ecf20Sopenharmony_ci#define butterfly_nreset (1 << 1) /* pin 3 */ 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define spi_sck_bit (1 << 0) /* pin 2 */ 348c2ecf20Sopenharmony_ci#define spi_mosi_bit (1 << 7) /* pin 9 */ 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci#define vcc_bits ((1 << 6) | (1 << 5)) /* pins 7, 8 */ 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci/* STATUS input bits */ 398c2ecf20Sopenharmony_ci#define spi_miso_bit PARPORT_STATUS_BUSY /* pin 11 */ 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci/* CONTROL output bits */ 428c2ecf20Sopenharmony_ci#define spi_cs_bit PARPORT_CONTROL_SELECT /* pin 17 */ 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistatic inline struct butterfly *spidev_to_pp(struct spi_device *spi) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci return spi->controller_data; 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistruct butterfly { 508c2ecf20Sopenharmony_ci /* REVISIT ... for now, this must be first */ 518c2ecf20Sopenharmony_ci struct spi_bitbang bitbang; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci struct parport *port; 548c2ecf20Sopenharmony_ci struct pardevice *pd; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci u8 lastbyte; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci struct spi_device *dataflash; 598c2ecf20Sopenharmony_ci struct spi_device *butterfly; 608c2ecf20Sopenharmony_ci struct spi_board_info info[2]; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci}; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci/*----------------------------------------------------------------------*/ 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_cistatic inline void 678c2ecf20Sopenharmony_cisetsck(struct spi_device *spi, int is_on) 688c2ecf20Sopenharmony_ci{ 698c2ecf20Sopenharmony_ci struct butterfly *pp = spidev_to_pp(spi); 708c2ecf20Sopenharmony_ci u8 bit, byte = pp->lastbyte; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci bit = spi_sck_bit; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci if (is_on) 758c2ecf20Sopenharmony_ci byte |= bit; 768c2ecf20Sopenharmony_ci else 778c2ecf20Sopenharmony_ci byte &= ~bit; 788c2ecf20Sopenharmony_ci parport_write_data(pp->port, byte); 798c2ecf20Sopenharmony_ci pp->lastbyte = byte; 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic inline void 838c2ecf20Sopenharmony_cisetmosi(struct spi_device *spi, int is_on) 848c2ecf20Sopenharmony_ci{ 858c2ecf20Sopenharmony_ci struct butterfly *pp = spidev_to_pp(spi); 868c2ecf20Sopenharmony_ci u8 bit, byte = pp->lastbyte; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci bit = spi_mosi_bit; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci if (is_on) 918c2ecf20Sopenharmony_ci byte |= bit; 928c2ecf20Sopenharmony_ci else 938c2ecf20Sopenharmony_ci byte &= ~bit; 948c2ecf20Sopenharmony_ci parport_write_data(pp->port, byte); 958c2ecf20Sopenharmony_ci pp->lastbyte = byte; 968c2ecf20Sopenharmony_ci} 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_cistatic inline int getmiso(struct spi_device *spi) 998c2ecf20Sopenharmony_ci{ 1008c2ecf20Sopenharmony_ci struct butterfly *pp = spidev_to_pp(spi); 1018c2ecf20Sopenharmony_ci int value; 1028c2ecf20Sopenharmony_ci u8 bit; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci bit = spi_miso_bit; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci /* only STATUS_BUSY is NOT negated */ 1078c2ecf20Sopenharmony_ci value = !(parport_read_status(pp->port) & bit); 1088c2ecf20Sopenharmony_ci return (bit == PARPORT_STATUS_BUSY) ? value : !value; 1098c2ecf20Sopenharmony_ci} 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_cistatic void butterfly_chipselect(struct spi_device *spi, int value) 1128c2ecf20Sopenharmony_ci{ 1138c2ecf20Sopenharmony_ci struct butterfly *pp = spidev_to_pp(spi); 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci /* set default clock polarity */ 1168c2ecf20Sopenharmony_ci if (value != BITBANG_CS_INACTIVE) 1178c2ecf20Sopenharmony_ci setsck(spi, spi->mode & SPI_CPOL); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci /* here, value == "activate or not"; 1208c2ecf20Sopenharmony_ci * most PARPORT_CONTROL_* bits are negated, so we must 1218c2ecf20Sopenharmony_ci * morph it to value == "bit value to write in control register" 1228c2ecf20Sopenharmony_ci */ 1238c2ecf20Sopenharmony_ci if (spi_cs_bit == PARPORT_CONTROL_INIT) 1248c2ecf20Sopenharmony_ci value = !value; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci parport_frob_control(pp->port, spi_cs_bit, value ? spi_cs_bit : 0); 1278c2ecf20Sopenharmony_ci} 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci/* we only needed to implement one mode here, and choose SPI_MODE_0 */ 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci#define spidelay(X) do { } while (0) 1328c2ecf20Sopenharmony_ci/* #define spidelay ndelay */ 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci#include "spi-bitbang-txrx.h" 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_cistatic u32 1378c2ecf20Sopenharmony_cibutterfly_txrx_word_mode0(struct spi_device *spi, unsigned nsecs, u32 word, 1388c2ecf20Sopenharmony_ci u8 bits, unsigned flags) 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits); 1418c2ecf20Sopenharmony_ci} 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci/*----------------------------------------------------------------------*/ 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci/* override default partitioning with cmdlinepart */ 1468c2ecf20Sopenharmony_cistatic struct mtd_partition partitions[] = { { 1478c2ecf20Sopenharmony_ci /* JFFS2 wants partitions of 4*N blocks for this device, 1488c2ecf20Sopenharmony_ci * so sectors 0 and 1 can't be partitions by themselves. 1498c2ecf20Sopenharmony_ci */ 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci /* sector 0 = 8 pages * 264 bytes/page (1 block) 1528c2ecf20Sopenharmony_ci * sector 1 = 248 pages * 264 bytes/page 1538c2ecf20Sopenharmony_ci */ 1548c2ecf20Sopenharmony_ci .name = "bookkeeping", /* 66 KB */ 1558c2ecf20Sopenharmony_ci .offset = 0, 1568c2ecf20Sopenharmony_ci .size = (8 + 248) * 264, 1578c2ecf20Sopenharmony_ci /* .mask_flags = MTD_WRITEABLE, */ 1588c2ecf20Sopenharmony_ci}, { 1598c2ecf20Sopenharmony_ci /* sector 2 = 256 pages * 264 bytes/page 1608c2ecf20Sopenharmony_ci * sectors 3-5 = 512 pages * 264 bytes/page 1618c2ecf20Sopenharmony_ci */ 1628c2ecf20Sopenharmony_ci .name = "filesystem", /* 462 KB */ 1638c2ecf20Sopenharmony_ci .offset = MTDPART_OFS_APPEND, 1648c2ecf20Sopenharmony_ci .size = MTDPART_SIZ_FULL, 1658c2ecf20Sopenharmony_ci} }; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_cistatic struct flash_platform_data flash = { 1688c2ecf20Sopenharmony_ci .name = "butterflash", 1698c2ecf20Sopenharmony_ci .parts = partitions, 1708c2ecf20Sopenharmony_ci .nr_parts = ARRAY_SIZE(partitions), 1718c2ecf20Sopenharmony_ci}; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci/* REVISIT remove this ugly global and its "only one" limitation */ 1748c2ecf20Sopenharmony_cistatic struct butterfly *butterfly; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_cistatic void butterfly_attach(struct parport *p) 1778c2ecf20Sopenharmony_ci{ 1788c2ecf20Sopenharmony_ci struct pardevice *pd; 1798c2ecf20Sopenharmony_ci int status; 1808c2ecf20Sopenharmony_ci struct butterfly *pp; 1818c2ecf20Sopenharmony_ci struct spi_master *master; 1828c2ecf20Sopenharmony_ci struct device *dev = p->physport->dev; 1838c2ecf20Sopenharmony_ci struct pardev_cb butterfly_cb; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci if (butterfly || !dev) 1868c2ecf20Sopenharmony_ci return; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci /* REVISIT: this just _assumes_ a butterfly is there ... no probe, 1898c2ecf20Sopenharmony_ci * and no way to be selective about what it binds to. 1908c2ecf20Sopenharmony_ci */ 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci master = spi_alloc_master(dev, sizeof(*pp)); 1938c2ecf20Sopenharmony_ci if (!master) { 1948c2ecf20Sopenharmony_ci status = -ENOMEM; 1958c2ecf20Sopenharmony_ci goto done; 1968c2ecf20Sopenharmony_ci } 1978c2ecf20Sopenharmony_ci pp = spi_master_get_devdata(master); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci /* 2008c2ecf20Sopenharmony_ci * SPI and bitbang hookup 2018c2ecf20Sopenharmony_ci * 2028c2ecf20Sopenharmony_ci * use default setup(), cleanup(), and transfer() methods; and 2038c2ecf20Sopenharmony_ci * only bother implementing mode 0. Start it later. 2048c2ecf20Sopenharmony_ci */ 2058c2ecf20Sopenharmony_ci master->bus_num = 42; 2068c2ecf20Sopenharmony_ci master->num_chipselect = 2; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci pp->bitbang.master = master; 2098c2ecf20Sopenharmony_ci pp->bitbang.chipselect = butterfly_chipselect; 2108c2ecf20Sopenharmony_ci pp->bitbang.txrx_word[SPI_MODE_0] = butterfly_txrx_word_mode0; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci /* 2138c2ecf20Sopenharmony_ci * parport hookup 2148c2ecf20Sopenharmony_ci */ 2158c2ecf20Sopenharmony_ci pp->port = p; 2168c2ecf20Sopenharmony_ci memset(&butterfly_cb, 0, sizeof(butterfly_cb)); 2178c2ecf20Sopenharmony_ci butterfly_cb.private = pp; 2188c2ecf20Sopenharmony_ci pd = parport_register_dev_model(p, "spi_butterfly", &butterfly_cb, 0); 2198c2ecf20Sopenharmony_ci if (!pd) { 2208c2ecf20Sopenharmony_ci status = -ENOMEM; 2218c2ecf20Sopenharmony_ci goto clean0; 2228c2ecf20Sopenharmony_ci } 2238c2ecf20Sopenharmony_ci pp->pd = pd; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci status = parport_claim(pd); 2268c2ecf20Sopenharmony_ci if (status < 0) 2278c2ecf20Sopenharmony_ci goto clean1; 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci /* 2308c2ecf20Sopenharmony_ci * Butterfly reset, powerup, run firmware 2318c2ecf20Sopenharmony_ci */ 2328c2ecf20Sopenharmony_ci pr_debug("%s: powerup/reset Butterfly\n", p->name); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci /* nCS for dataflash (this bit is inverted on output) */ 2358c2ecf20Sopenharmony_ci parport_frob_control(pp->port, spi_cs_bit, 0); 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci /* stabilize power with chip in reset (nRESET), and 2388c2ecf20Sopenharmony_ci * spi_sck_bit clear (CPOL=0) 2398c2ecf20Sopenharmony_ci */ 2408c2ecf20Sopenharmony_ci pp->lastbyte |= vcc_bits; 2418c2ecf20Sopenharmony_ci parport_write_data(pp->port, pp->lastbyte); 2428c2ecf20Sopenharmony_ci msleep(5); 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci /* take it out of reset; assume long reset delay */ 2458c2ecf20Sopenharmony_ci pp->lastbyte |= butterfly_nreset; 2468c2ecf20Sopenharmony_ci parport_write_data(pp->port, pp->lastbyte); 2478c2ecf20Sopenharmony_ci msleep(100); 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci /* 2508c2ecf20Sopenharmony_ci * Start SPI ... for now, hide that we're two physical busses. 2518c2ecf20Sopenharmony_ci */ 2528c2ecf20Sopenharmony_ci status = spi_bitbang_start(&pp->bitbang); 2538c2ecf20Sopenharmony_ci if (status < 0) 2548c2ecf20Sopenharmony_ci goto clean2; 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci /* Bus 1 lets us talk to at45db041b (firmware disables AVR SPI), AVR 2578c2ecf20Sopenharmony_ci * (firmware resets at45, acts as spi slave) or neither (we ignore 2588c2ecf20Sopenharmony_ci * both, AVR uses AT45). Here we expect firmware for the first option. 2598c2ecf20Sopenharmony_ci */ 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci pp->info[0].max_speed_hz = 15 * 1000 * 1000; 2628c2ecf20Sopenharmony_ci strcpy(pp->info[0].modalias, "mtd_dataflash"); 2638c2ecf20Sopenharmony_ci pp->info[0].platform_data = &flash; 2648c2ecf20Sopenharmony_ci pp->info[0].chip_select = 1; 2658c2ecf20Sopenharmony_ci pp->info[0].controller_data = pp; 2668c2ecf20Sopenharmony_ci pp->dataflash = spi_new_device(pp->bitbang.master, &pp->info[0]); 2678c2ecf20Sopenharmony_ci if (pp->dataflash) 2688c2ecf20Sopenharmony_ci pr_debug("%s: dataflash at %s\n", p->name, 2698c2ecf20Sopenharmony_ci dev_name(&pp->dataflash->dev)); 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci pr_info("%s: AVR Butterfly\n", p->name); 2728c2ecf20Sopenharmony_ci butterfly = pp; 2738c2ecf20Sopenharmony_ci return; 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ciclean2: 2768c2ecf20Sopenharmony_ci /* turn off VCC */ 2778c2ecf20Sopenharmony_ci parport_write_data(pp->port, 0); 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci parport_release(pp->pd); 2808c2ecf20Sopenharmony_ciclean1: 2818c2ecf20Sopenharmony_ci parport_unregister_device(pd); 2828c2ecf20Sopenharmony_ciclean0: 2838c2ecf20Sopenharmony_ci spi_master_put(pp->bitbang.master); 2848c2ecf20Sopenharmony_cidone: 2858c2ecf20Sopenharmony_ci pr_debug("%s: butterfly probe, fail %d\n", p->name, status); 2868c2ecf20Sopenharmony_ci} 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_cistatic void butterfly_detach(struct parport *p) 2898c2ecf20Sopenharmony_ci{ 2908c2ecf20Sopenharmony_ci struct butterfly *pp; 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci /* FIXME this global is ugly ... but, how to quickly get from 2938c2ecf20Sopenharmony_ci * the parport to the "struct butterfly" associated with it? 2948c2ecf20Sopenharmony_ci * "old school" driver-internal device lists? 2958c2ecf20Sopenharmony_ci */ 2968c2ecf20Sopenharmony_ci if (!butterfly || butterfly->port != p) 2978c2ecf20Sopenharmony_ci return; 2988c2ecf20Sopenharmony_ci pp = butterfly; 2998c2ecf20Sopenharmony_ci butterfly = NULL; 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci /* stop() unregisters child devices too */ 3028c2ecf20Sopenharmony_ci spi_bitbang_stop(&pp->bitbang); 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ci /* turn off VCC */ 3058c2ecf20Sopenharmony_ci parport_write_data(pp->port, 0); 3068c2ecf20Sopenharmony_ci msleep(10); 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci parport_release(pp->pd); 3098c2ecf20Sopenharmony_ci parport_unregister_device(pp->pd); 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci spi_master_put(pp->bitbang.master); 3128c2ecf20Sopenharmony_ci} 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_cistatic struct parport_driver butterfly_driver = { 3158c2ecf20Sopenharmony_ci .name = "spi_butterfly", 3168c2ecf20Sopenharmony_ci .match_port = butterfly_attach, 3178c2ecf20Sopenharmony_ci .detach = butterfly_detach, 3188c2ecf20Sopenharmony_ci .devmodel = true, 3198c2ecf20Sopenharmony_ci}; 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_cistatic int __init butterfly_init(void) 3228c2ecf20Sopenharmony_ci{ 3238c2ecf20Sopenharmony_ci return parport_register_driver(&butterfly_driver); 3248c2ecf20Sopenharmony_ci} 3258c2ecf20Sopenharmony_cidevice_initcall(butterfly_init); 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_cistatic void __exit butterfly_exit(void) 3288c2ecf20Sopenharmony_ci{ 3298c2ecf20Sopenharmony_ci parport_unregister_driver(&butterfly_driver); 3308c2ecf20Sopenharmony_ci} 3318c2ecf20Sopenharmony_cimodule_exit(butterfly_exit); 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Parport Adapter driver for AVR Butterfly"); 3348c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 335