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