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