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