David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * spi_butterfly.c - parport-to-butterfly adapter |
| 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. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 20 | #include <linux/kernel.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/delay.h> |
David Brownell | da67529 | 2007-05-08 00:27:42 -0700 | [diff] [blame^] | 23 | #include <linux/device.h> |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 24 | #include <linux/parport.h> |
| 25 | |
Al Viro | 914e263 | 2006-10-18 13:55:46 -0400 | [diff] [blame] | 26 | #include <linux/sched.h> |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 27 | #include <linux/spi/spi.h> |
| 28 | #include <linux/spi/spi_bitbang.h> |
| 29 | #include <linux/spi/flash.h> |
| 30 | |
| 31 | #include <linux/mtd/partitions.h> |
| 32 | |
| 33 | |
| 34 | /* |
| 35 | * This uses SPI to talk with an "AVR Butterfly", which is a $US20 card |
| 36 | * with a battery powered AVR microcontroller and lots of goodies. You |
| 37 | * can use GCC to develop firmware for this. |
| 38 | * |
| 39 | * See Documentation/spi/butterfly for information about how to build |
| 40 | * and use this custom parallel port cable. |
| 41 | */ |
| 42 | |
| 43 | #undef HAVE_USI /* nyet */ |
| 44 | |
| 45 | |
| 46 | /* DATA output bits (pins 2..9 == D0..D7) */ |
| 47 | #define butterfly_nreset (1 << 1) /* pin 3 */ |
| 48 | |
| 49 | #define spi_sck_bit (1 << 0) /* pin 2 */ |
| 50 | #define spi_mosi_bit (1 << 7) /* pin 9 */ |
| 51 | |
| 52 | #define usi_sck_bit (1 << 3) /* pin 5 */ |
| 53 | #define usi_mosi_bit (1 << 4) /* pin 6 */ |
| 54 | |
| 55 | #define vcc_bits ((1 << 6) | (1 << 5)) /* pins 7, 8 */ |
| 56 | |
| 57 | /* STATUS input bits */ |
| 58 | #define spi_miso_bit PARPORT_STATUS_BUSY /* pin 11 */ |
| 59 | |
| 60 | #define usi_miso_bit PARPORT_STATUS_PAPEROUT /* pin 12 */ |
| 61 | |
| 62 | /* CONTROL output bits */ |
| 63 | #define spi_cs_bit PARPORT_CONTROL_SELECT /* pin 17 */ |
| 64 | /* USI uses no chipselect */ |
| 65 | |
| 66 | |
| 67 | |
| 68 | static inline struct butterfly *spidev_to_pp(struct spi_device *spi) |
| 69 | { |
| 70 | return spi->controller_data; |
| 71 | } |
| 72 | |
| 73 | static inline int is_usidev(struct spi_device *spi) |
| 74 | { |
| 75 | #ifdef HAVE_USI |
| 76 | return spi->chip_select != 1; |
| 77 | #else |
| 78 | return 0; |
| 79 | #endif |
| 80 | } |
| 81 | |
| 82 | |
| 83 | struct butterfly { |
| 84 | /* REVISIT ... for now, this must be first */ |
| 85 | struct spi_bitbang bitbang; |
| 86 | |
| 87 | struct parport *port; |
| 88 | struct pardevice *pd; |
| 89 | |
| 90 | u8 lastbyte; |
| 91 | |
| 92 | struct spi_device *dataflash; |
| 93 | struct spi_device *butterfly; |
| 94 | struct spi_board_info info[2]; |
| 95 | |
| 96 | }; |
| 97 | |
| 98 | /*----------------------------------------------------------------------*/ |
| 99 | |
| 100 | /* |
| 101 | * these routines may be slower than necessary because they're hiding |
| 102 | * the fact that there are two different SPI busses on this cable: one |
| 103 | * to the DataFlash chip (or AVR SPI controller), the other to the |
| 104 | * AVR USI controller. |
| 105 | */ |
| 106 | |
| 107 | static inline void |
| 108 | setsck(struct spi_device *spi, int is_on) |
| 109 | { |
| 110 | struct butterfly *pp = spidev_to_pp(spi); |
| 111 | u8 bit, byte = pp->lastbyte; |
| 112 | |
| 113 | if (is_usidev(spi)) |
| 114 | bit = usi_sck_bit; |
| 115 | else |
| 116 | bit = spi_sck_bit; |
| 117 | |
| 118 | if (is_on) |
| 119 | byte |= bit; |
| 120 | else |
| 121 | byte &= ~bit; |
| 122 | parport_write_data(pp->port, byte); |
| 123 | pp->lastbyte = byte; |
| 124 | } |
| 125 | |
| 126 | static inline void |
| 127 | setmosi(struct spi_device *spi, int is_on) |
| 128 | { |
| 129 | struct butterfly *pp = spidev_to_pp(spi); |
| 130 | u8 bit, byte = pp->lastbyte; |
| 131 | |
| 132 | if (is_usidev(spi)) |
| 133 | bit = usi_mosi_bit; |
| 134 | else |
| 135 | bit = spi_mosi_bit; |
| 136 | |
| 137 | if (is_on) |
| 138 | byte |= bit; |
| 139 | else |
| 140 | byte &= ~bit; |
| 141 | parport_write_data(pp->port, byte); |
| 142 | pp->lastbyte = byte; |
| 143 | } |
| 144 | |
| 145 | static inline int getmiso(struct spi_device *spi) |
| 146 | { |
| 147 | struct butterfly *pp = spidev_to_pp(spi); |
| 148 | int value; |
| 149 | u8 bit; |
| 150 | |
| 151 | if (is_usidev(spi)) |
| 152 | bit = usi_miso_bit; |
| 153 | else |
| 154 | bit = spi_miso_bit; |
| 155 | |
| 156 | /* only STATUS_BUSY is NOT negated */ |
| 157 | value = !(parport_read_status(pp->port) & bit); |
| 158 | return (bit == PARPORT_STATUS_BUSY) ? value : !value; |
| 159 | } |
| 160 | |
| 161 | static void butterfly_chipselect(struct spi_device *spi, int value) |
| 162 | { |
| 163 | struct butterfly *pp = spidev_to_pp(spi); |
| 164 | |
| 165 | /* set default clock polarity */ |
David Brownell | 9c1da3c | 2006-01-21 13:21:43 -0800 | [diff] [blame] | 166 | if (value != BITBANG_CS_INACTIVE) |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 167 | setsck(spi, spi->mode & SPI_CPOL); |
| 168 | |
| 169 | /* no chipselect on this USI link config */ |
| 170 | if (is_usidev(spi)) |
| 171 | return; |
| 172 | |
David Brownell | 9c1da3c | 2006-01-21 13:21:43 -0800 | [diff] [blame] | 173 | /* here, value == "activate or not"; |
| 174 | * most PARPORT_CONTROL_* bits are negated, so we must |
| 175 | * morph it to value == "bit value to write in control register" |
| 176 | */ |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 177 | if (spi_cs_bit == PARPORT_CONTROL_INIT) |
| 178 | value = !value; |
| 179 | |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 180 | parport_frob_control(pp->port, spi_cs_bit, value ? spi_cs_bit : 0); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /* we only needed to implement one mode here, and choose SPI_MODE_0 */ |
| 185 | |
| 186 | #define spidelay(X) do{}while(0) |
| 187 | //#define spidelay ndelay |
| 188 | |
| 189 | #define EXPAND_BITBANG_TXRX |
| 190 | #include <linux/spi/spi_bitbang.h> |
| 191 | |
| 192 | static u32 |
| 193 | butterfly_txrx_word_mode0(struct spi_device *spi, |
| 194 | unsigned nsecs, |
| 195 | u32 word, u8 bits) |
| 196 | { |
| 197 | return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits); |
| 198 | } |
| 199 | |
| 200 | /*----------------------------------------------------------------------*/ |
| 201 | |
| 202 | /* override default partitioning with cmdlinepart */ |
| 203 | static struct mtd_partition partitions[] = { { |
David Brownell | 9c1da3c | 2006-01-21 13:21:43 -0800 | [diff] [blame] | 204 | /* JFFS2 wants partitions of 4*N blocks for this device, |
| 205 | * so sectors 0 and 1 can't be partitions by themselves. |
| 206 | */ |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 207 | |
| 208 | /* sector 0 = 8 pages * 264 bytes/page (1 block) |
| 209 | * sector 1 = 248 pages * 264 bytes/page |
| 210 | */ |
| 211 | .name = "bookkeeping", // 66 KB |
| 212 | .offset = 0, |
| 213 | .size = (8 + 248) * 264, |
| 214 | // .mask_flags = MTD_WRITEABLE, |
| 215 | }, { |
| 216 | /* sector 2 = 256 pages * 264 bytes/page |
| 217 | * sectors 3-5 = 512 pages * 264 bytes/page |
| 218 | */ |
| 219 | .name = "filesystem", // 462 KB |
| 220 | .offset = MTDPART_OFS_APPEND, |
| 221 | .size = MTDPART_SIZ_FULL, |
| 222 | } }; |
| 223 | |
| 224 | static struct flash_platform_data flash = { |
| 225 | .name = "butterflash", |
| 226 | .parts = partitions, |
| 227 | .nr_parts = ARRAY_SIZE(partitions), |
| 228 | }; |
| 229 | |
| 230 | |
| 231 | /* REVISIT remove this ugly global and its "only one" limitation */ |
| 232 | static struct butterfly *butterfly; |
| 233 | |
| 234 | static void butterfly_attach(struct parport *p) |
| 235 | { |
| 236 | struct pardevice *pd; |
| 237 | int status; |
| 238 | struct butterfly *pp; |
| 239 | struct spi_master *master; |
David Brownell | da67529 | 2007-05-08 00:27:42 -0700 | [diff] [blame^] | 240 | struct device *dev = p->physport->dev; |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 241 | |
David Brownell | da67529 | 2007-05-08 00:27:42 -0700 | [diff] [blame^] | 242 | if (butterfly || !dev) |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 243 | return; |
| 244 | |
| 245 | /* REVISIT: this just _assumes_ a butterfly is there ... no probe, |
| 246 | * and no way to be selective about what it binds to. |
| 247 | */ |
| 248 | |
David Brownell | da67529 | 2007-05-08 00:27:42 -0700 | [diff] [blame^] | 249 | master = spi_alloc_master(dev, sizeof *pp); |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 250 | if (!master) { |
| 251 | status = -ENOMEM; |
| 252 | goto done; |
| 253 | } |
| 254 | pp = spi_master_get_devdata(master); |
| 255 | |
| 256 | /* |
| 257 | * SPI and bitbang hookup |
| 258 | * |
| 259 | * use default setup(), cleanup(), and transfer() methods; and |
| 260 | * only bother implementing mode 0. Start it later. |
| 261 | */ |
| 262 | master->bus_num = 42; |
| 263 | master->num_chipselect = 2; |
| 264 | |
| 265 | pp->bitbang.master = spi_master_get(master); |
| 266 | pp->bitbang.chipselect = butterfly_chipselect; |
| 267 | pp->bitbang.txrx_word[SPI_MODE_0] = butterfly_txrx_word_mode0; |
| 268 | |
| 269 | /* |
| 270 | * parport hookup |
| 271 | */ |
| 272 | pp->port = p; |
| 273 | pd = parport_register_device(p, "spi_butterfly", |
| 274 | NULL, NULL, NULL, |
| 275 | 0 /* FLAGS */, pp); |
| 276 | if (!pd) { |
| 277 | status = -ENOMEM; |
| 278 | goto clean0; |
| 279 | } |
| 280 | pp->pd = pd; |
| 281 | |
| 282 | status = parport_claim(pd); |
| 283 | if (status < 0) |
| 284 | goto clean1; |
| 285 | |
| 286 | /* |
| 287 | * Butterfly reset, powerup, run firmware |
| 288 | */ |
| 289 | pr_debug("%s: powerup/reset Butterfly\n", p->name); |
| 290 | |
| 291 | /* nCS for dataflash (this bit is inverted on output) */ |
| 292 | parport_frob_control(pp->port, spi_cs_bit, 0); |
| 293 | |
| 294 | /* stabilize power with chip in reset (nRESET), and |
| 295 | * both spi_sck_bit and usi_sck_bit clear (CPOL=0) |
| 296 | */ |
| 297 | pp->lastbyte |= vcc_bits; |
| 298 | parport_write_data(pp->port, pp->lastbyte); |
| 299 | msleep(5); |
| 300 | |
| 301 | /* take it out of reset; assume long reset delay */ |
| 302 | pp->lastbyte |= butterfly_nreset; |
| 303 | parport_write_data(pp->port, pp->lastbyte); |
| 304 | msleep(100); |
| 305 | |
| 306 | |
| 307 | /* |
| 308 | * Start SPI ... for now, hide that we're two physical busses. |
| 309 | */ |
| 310 | status = spi_bitbang_start(&pp->bitbang); |
| 311 | if (status < 0) |
| 312 | goto clean2; |
| 313 | |
David Brownell | 9c1da3c | 2006-01-21 13:21:43 -0800 | [diff] [blame] | 314 | /* Bus 1 lets us talk to at45db041b (firmware disables AVR SPI), AVR |
| 315 | * (firmware resets at45, acts as spi slave) or neither (we ignore |
| 316 | * both, AVR uses AT45). Here we expect firmware for the first option. |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 317 | */ |
Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 318 | |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 319 | pp->info[0].max_speed_hz = 15 * 1000 * 1000; |
| 320 | strcpy(pp->info[0].modalias, "mtd_dataflash"); |
| 321 | pp->info[0].platform_data = &flash; |
| 322 | pp->info[0].chip_select = 1; |
| 323 | pp->info[0].controller_data = pp; |
| 324 | pp->dataflash = spi_new_device(pp->bitbang.master, &pp->info[0]); |
| 325 | if (pp->dataflash) |
| 326 | pr_debug("%s: dataflash at %s\n", p->name, |
| 327 | pp->dataflash->dev.bus_id); |
| 328 | |
| 329 | #ifdef HAVE_USI |
David Brownell | 9c1da3c | 2006-01-21 13:21:43 -0800 | [diff] [blame] | 330 | /* Bus 2 is only for talking to the AVR, and it can work no |
| 331 | * matter who masters bus 1; needs appropriate AVR firmware. |
| 332 | */ |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 333 | pp->info[1].max_speed_hz = 10 /* ?? */ * 1000 * 1000; |
| 334 | strcpy(pp->info[1].modalias, "butterfly"); |
| 335 | // pp->info[1].platform_data = ... TBD ... ; |
| 336 | pp->info[1].chip_select = 2, |
| 337 | pp->info[1].controller_data = pp; |
| 338 | pp->butterfly = spi_new_device(pp->bitbang.master, &pp->info[1]); |
| 339 | if (pp->butterfly) |
| 340 | pr_debug("%s: butterfly at %s\n", p->name, |
| 341 | pp->butterfly->dev.bus_id); |
| 342 | |
| 343 | /* FIXME setup ACK for the IRQ line ... */ |
| 344 | #endif |
| 345 | |
| 346 | // dev_info(_what?_, ...) |
| 347 | pr_info("%s: AVR Butterfly\n", p->name); |
| 348 | butterfly = pp; |
| 349 | return; |
| 350 | |
| 351 | clean2: |
| 352 | /* turn off VCC */ |
| 353 | parport_write_data(pp->port, 0); |
| 354 | |
| 355 | parport_release(pp->pd); |
| 356 | clean1: |
| 357 | parport_unregister_device(pd); |
| 358 | clean0: |
| 359 | (void) spi_master_put(pp->bitbang.master); |
| 360 | done: |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 361 | pr_debug("%s: butterfly probe, fail %d\n", p->name, status); |
| 362 | } |
| 363 | |
| 364 | static void butterfly_detach(struct parport *p) |
| 365 | { |
| 366 | struct butterfly *pp; |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 367 | int status; |
| 368 | |
| 369 | /* FIXME this global is ugly ... but, how to quickly get from |
| 370 | * the parport to the "struct butterfly" associated with it? |
| 371 | * "old school" driver-internal device lists? |
| 372 | */ |
| 373 | if (!butterfly || butterfly->port != p) |
| 374 | return; |
| 375 | pp = butterfly; |
| 376 | butterfly = NULL; |
| 377 | |
David Brownell | 9c1da3c | 2006-01-21 13:21:43 -0800 | [diff] [blame] | 378 | /* stop() unregisters child devices too */ |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 379 | status = spi_bitbang_stop(&pp->bitbang); |
| 380 | |
| 381 | /* turn off VCC */ |
| 382 | parport_write_data(pp->port, 0); |
| 383 | msleep(10); |
| 384 | |
| 385 | parport_release(pp->pd); |
| 386 | parport_unregister_device(pp->pd); |
| 387 | |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 388 | (void) spi_master_put(pp->bitbang.master); |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static struct parport_driver butterfly_driver = { |
| 392 | .name = "spi_butterfly", |
| 393 | .attach = butterfly_attach, |
| 394 | .detach = butterfly_detach, |
| 395 | }; |
| 396 | |
| 397 | |
| 398 | static int __init butterfly_init(void) |
| 399 | { |
| 400 | return parport_register_driver(&butterfly_driver); |
| 401 | } |
| 402 | device_initcall(butterfly_init); |
| 403 | |
| 404 | static void __exit butterfly_exit(void) |
| 405 | { |
| 406 | parport_unregister_driver(&butterfly_driver); |
| 407 | } |
| 408 | module_exit(butterfly_exit); |
| 409 | |
David Brownell | 9c1da3c | 2006-01-21 13:21:43 -0800 | [diff] [blame] | 410 | MODULE_DESCRIPTION("Parport Adapter driver for AVR Butterfly"); |
David Brownell | 2e10c84 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 411 | MODULE_LICENSE("GPL"); |