spi-butterfly.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * 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. */
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/module.h>
  24. #include <linux/device.h>
  25. #include <linux/parport.h>
  26. #include <linux/sched.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/spi/spi_bitbang.h>
  29. #include <linux/spi/flash.h>
  30. #include <linux/mtd/partitions.h>
  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. /* DATA output bits (pins 2..9 == D0..D7) */
  40. #define butterfly_nreset (1 << 1) /* pin 3 */
  41. #define spi_sck_bit (1 << 0) /* pin 2 */
  42. #define spi_mosi_bit (1 << 7) /* pin 9 */
  43. #define vcc_bits ((1 << 6) | (1 << 5)) /* pins 7, 8 */
  44. /* STATUS input bits */
  45. #define spi_miso_bit PARPORT_STATUS_BUSY /* pin 11 */
  46. /* CONTROL output bits */
  47. #define spi_cs_bit PARPORT_CONTROL_SELECT /* pin 17 */
  48. static inline struct butterfly *spidev_to_pp(struct spi_device *spi)
  49. {
  50. return spi->controller_data;
  51. }
  52. struct butterfly {
  53. /* REVISIT ... for now, this must be first */
  54. struct spi_bitbang bitbang;
  55. struct parport *port;
  56. struct pardevice *pd;
  57. u8 lastbyte;
  58. struct spi_device *dataflash;
  59. struct spi_device *butterfly;
  60. struct spi_board_info info[2];
  61. };
  62. /*----------------------------------------------------------------------*/
  63. static inline void
  64. setsck(struct spi_device *spi, int is_on)
  65. {
  66. struct butterfly *pp = spidev_to_pp(spi);
  67. u8 bit, byte = pp->lastbyte;
  68. bit = spi_sck_bit;
  69. if (is_on)
  70. byte |= bit;
  71. else
  72. byte &= ~bit;
  73. parport_write_data(pp->port, byte);
  74. pp->lastbyte = byte;
  75. }
  76. static inline void
  77. setmosi(struct spi_device *spi, int is_on)
  78. {
  79. struct butterfly *pp = spidev_to_pp(spi);
  80. u8 bit, byte = pp->lastbyte;
  81. bit = spi_mosi_bit;
  82. if (is_on)
  83. byte |= bit;
  84. else
  85. byte &= ~bit;
  86. parport_write_data(pp->port, byte);
  87. pp->lastbyte = byte;
  88. }
  89. static inline int getmiso(struct spi_device *spi)
  90. {
  91. struct butterfly *pp = spidev_to_pp(spi);
  92. int value;
  93. u8 bit;
  94. bit = spi_miso_bit;
  95. /* only STATUS_BUSY is NOT negated */
  96. value = !(parport_read_status(pp->port) & bit);
  97. return (bit == PARPORT_STATUS_BUSY) ? value : !value;
  98. }
  99. static void butterfly_chipselect(struct spi_device *spi, int value)
  100. {
  101. struct butterfly *pp = spidev_to_pp(spi);
  102. /* set default clock polarity */
  103. if (value != BITBANG_CS_INACTIVE)
  104. setsck(spi, spi->mode & SPI_CPOL);
  105. /* here, value == "activate or not";
  106. * most PARPORT_CONTROL_* bits are negated, so we must
  107. * morph it to value == "bit value to write in control register"
  108. */
  109. if (spi_cs_bit == PARPORT_CONTROL_INIT)
  110. value = !value;
  111. parport_frob_control(pp->port, spi_cs_bit, value ? spi_cs_bit : 0);
  112. }
  113. /* we only needed to implement one mode here, and choose SPI_MODE_0 */
  114. #define spidelay(X) do{}while(0)
  115. //#define spidelay ndelay
  116. #include "spi-bitbang-txrx.h"
  117. static u32
  118. butterfly_txrx_word_mode0(struct spi_device *spi,
  119. unsigned nsecs,
  120. u32 word, u8 bits)
  121. {
  122. return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits);
  123. }
  124. /*----------------------------------------------------------------------*/
  125. /* override default partitioning with cmdlinepart */
  126. static struct mtd_partition partitions[] = { {
  127. /* JFFS2 wants partitions of 4*N blocks for this device,
  128. * so sectors 0 and 1 can't be partitions by themselves.
  129. */
  130. /* sector 0 = 8 pages * 264 bytes/page (1 block)
  131. * sector 1 = 248 pages * 264 bytes/page
  132. */
  133. .name = "bookkeeping", // 66 KB
  134. .offset = 0,
  135. .size = (8 + 248) * 264,
  136. // .mask_flags = MTD_WRITEABLE,
  137. }, {
  138. /* sector 2 = 256 pages * 264 bytes/page
  139. * sectors 3-5 = 512 pages * 264 bytes/page
  140. */
  141. .name = "filesystem", // 462 KB
  142. .offset = MTDPART_OFS_APPEND,
  143. .size = MTDPART_SIZ_FULL,
  144. } };
  145. static struct flash_platform_data flash = {
  146. .name = "butterflash",
  147. .parts = partitions,
  148. .nr_parts = ARRAY_SIZE(partitions),
  149. };
  150. /* REVISIT remove this ugly global and its "only one" limitation */
  151. static struct butterfly *butterfly;
  152. static void butterfly_attach(struct parport *p)
  153. {
  154. struct pardevice *pd;
  155. int status;
  156. struct butterfly *pp;
  157. struct spi_master *master;
  158. struct device *dev = p->physport->dev;
  159. if (butterfly || !dev)
  160. return;
  161. /* REVISIT: this just _assumes_ a butterfly is there ... no probe,
  162. * and no way to be selective about what it binds to.
  163. */
  164. master = spi_alloc_master(dev, sizeof *pp);
  165. if (!master) {
  166. status = -ENOMEM;
  167. goto done;
  168. }
  169. pp = spi_master_get_devdata(master);
  170. /*
  171. * SPI and bitbang hookup
  172. *
  173. * use default setup(), cleanup(), and transfer() methods; and
  174. * only bother implementing mode 0. Start it later.
  175. */
  176. master->bus_num = 42;
  177. master->num_chipselect = 2;
  178. pp->bitbang.master = spi_master_get(master);
  179. pp->bitbang.chipselect = butterfly_chipselect;
  180. pp->bitbang.txrx_word[SPI_MODE_0] = butterfly_txrx_word_mode0;
  181. /*
  182. * parport hookup
  183. */
  184. pp->port = p;
  185. pd = parport_register_device(p, "spi_butterfly",
  186. NULL, NULL, NULL,
  187. 0 /* FLAGS */, pp);
  188. if (!pd) {
  189. status = -ENOMEM;
  190. goto clean0;
  191. }
  192. pp->pd = pd;
  193. status = parport_claim(pd);
  194. if (status < 0)
  195. goto clean1;
  196. /*
  197. * Butterfly reset, powerup, run firmware
  198. */
  199. pr_debug("%s: powerup/reset Butterfly\n", p->name);
  200. /* nCS for dataflash (this bit is inverted on output) */
  201. parport_frob_control(pp->port, spi_cs_bit, 0);
  202. /* stabilize power with chip in reset (nRESET), and
  203. * spi_sck_bit clear (CPOL=0)
  204. */
  205. pp->lastbyte |= vcc_bits;
  206. parport_write_data(pp->port, pp->lastbyte);
  207. msleep(5);
  208. /* take it out of reset; assume long reset delay */
  209. pp->lastbyte |= butterfly_nreset;
  210. parport_write_data(pp->port, pp->lastbyte);
  211. msleep(100);
  212. /*
  213. * Start SPI ... for now, hide that we're two physical busses.
  214. */
  215. status = spi_bitbang_start(&pp->bitbang);
  216. if (status < 0)
  217. goto clean2;
  218. /* Bus 1 lets us talk to at45db041b (firmware disables AVR SPI), AVR
  219. * (firmware resets at45, acts as spi slave) or neither (we ignore
  220. * both, AVR uses AT45). Here we expect firmware for the first option.
  221. */
  222. pp->info[0].max_speed_hz = 15 * 1000 * 1000;
  223. strcpy(pp->info[0].modalias, "mtd_dataflash");
  224. pp->info[0].platform_data = &flash;
  225. pp->info[0].chip_select = 1;
  226. pp->info[0].controller_data = pp;
  227. pp->dataflash = spi_new_device(pp->bitbang.master, &pp->info[0]);
  228. if (pp->dataflash)
  229. pr_debug("%s: dataflash at %s\n", p->name,
  230. dev_name(&pp->dataflash->dev));
  231. // dev_info(_what?_, ...)
  232. pr_info("%s: AVR Butterfly\n", p->name);
  233. butterfly = pp;
  234. return;
  235. clean2:
  236. /* turn off VCC */
  237. parport_write_data(pp->port, 0);
  238. parport_release(pp->pd);
  239. clean1:
  240. parport_unregister_device(pd);
  241. clean0:
  242. (void) spi_master_put(pp->bitbang.master);
  243. done:
  244. pr_debug("%s: butterfly probe, fail %d\n", p->name, status);
  245. }
  246. static void butterfly_detach(struct parport *p)
  247. {
  248. struct butterfly *pp;
  249. int status;
  250. /* FIXME this global is ugly ... but, how to quickly get from
  251. * the parport to the "struct butterfly" associated with it?
  252. * "old school" driver-internal device lists?
  253. */
  254. if (!butterfly || butterfly->port != p)
  255. return;
  256. pp = butterfly;
  257. butterfly = NULL;
  258. /* stop() unregisters child devices too */
  259. status = spi_bitbang_stop(&pp->bitbang);
  260. /* turn off VCC */
  261. parport_write_data(pp->port, 0);
  262. msleep(10);
  263. parport_release(pp->pd);
  264. parport_unregister_device(pp->pd);
  265. (void) spi_master_put(pp->bitbang.master);
  266. }
  267. static struct parport_driver butterfly_driver = {
  268. .name = "spi_butterfly",
  269. .attach = butterfly_attach,
  270. .detach = butterfly_detach,
  271. };
  272. static int __init butterfly_init(void)
  273. {
  274. return parport_register_driver(&butterfly_driver);
  275. }
  276. device_initcall(butterfly_init);
  277. static void __exit butterfly_exit(void)
  278. {
  279. parport_unregister_driver(&butterfly_driver);
  280. }
  281. module_exit(butterfly_exit);
  282. MODULE_DESCRIPTION("Parport Adapter driver for AVR Butterfly");
  283. MODULE_LICENSE("GPL");