spi_butterfly.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. */
  20. #include <linux/config.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/parport.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/spi/spi_bitbang.h>
  28. #include <linux/spi/flash.h>
  29. #include <linux/mtd/partitions.h>
  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. #undef HAVE_USI /* nyet */
  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 usi_sck_bit (1 << 3) /* pin 5 */
  44. #define usi_mosi_bit (1 << 4) /* pin 6 */
  45. #define vcc_bits ((1 << 6) | (1 << 5)) /* pins 7, 8 */
  46. /* STATUS input bits */
  47. #define spi_miso_bit PARPORT_STATUS_BUSY /* pin 11 */
  48. #define usi_miso_bit PARPORT_STATUS_PAPEROUT /* pin 12 */
  49. /* CONTROL output bits */
  50. #define spi_cs_bit PARPORT_CONTROL_SELECT /* pin 17 */
  51. /* USI uses no chipselect */
  52. static inline struct butterfly *spidev_to_pp(struct spi_device *spi)
  53. {
  54. return spi->controller_data;
  55. }
  56. static inline int is_usidev(struct spi_device *spi)
  57. {
  58. #ifdef HAVE_USI
  59. return spi->chip_select != 1;
  60. #else
  61. return 0;
  62. #endif
  63. }
  64. struct butterfly {
  65. /* REVISIT ... for now, this must be first */
  66. struct spi_bitbang bitbang;
  67. struct parport *port;
  68. struct pardevice *pd;
  69. u8 lastbyte;
  70. struct spi_device *dataflash;
  71. struct spi_device *butterfly;
  72. struct spi_board_info info[2];
  73. };
  74. /*----------------------------------------------------------------------*/
  75. /*
  76. * these routines may be slower than necessary because they're hiding
  77. * the fact that there are two different SPI busses on this cable: one
  78. * to the DataFlash chip (or AVR SPI controller), the other to the
  79. * AVR USI controller.
  80. */
  81. static inline void
  82. setsck(struct spi_device *spi, int is_on)
  83. {
  84. struct butterfly *pp = spidev_to_pp(spi);
  85. u8 bit, byte = pp->lastbyte;
  86. if (is_usidev(spi))
  87. bit = usi_sck_bit;
  88. else
  89. bit = spi_sck_bit;
  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. static inline void
  98. setmosi(struct spi_device *spi, int is_on)
  99. {
  100. struct butterfly *pp = spidev_to_pp(spi);
  101. u8 bit, byte = pp->lastbyte;
  102. if (is_usidev(spi))
  103. bit = usi_mosi_bit;
  104. else
  105. bit = spi_mosi_bit;
  106. if (is_on)
  107. byte |= bit;
  108. else
  109. byte &= ~bit;
  110. parport_write_data(pp->port, byte);
  111. pp->lastbyte = byte;
  112. }
  113. static inline int getmiso(struct spi_device *spi)
  114. {
  115. struct butterfly *pp = spidev_to_pp(spi);
  116. int value;
  117. u8 bit;
  118. if (is_usidev(spi))
  119. bit = usi_miso_bit;
  120. else
  121. bit = spi_miso_bit;
  122. /* only STATUS_BUSY is NOT negated */
  123. value = !(parport_read_status(pp->port) & bit);
  124. return (bit == PARPORT_STATUS_BUSY) ? value : !value;
  125. }
  126. static void butterfly_chipselect(struct spi_device *spi, int value)
  127. {
  128. struct butterfly *pp = spidev_to_pp(spi);
  129. /* set default clock polarity */
  130. if (value)
  131. setsck(spi, spi->mode & SPI_CPOL);
  132. /* no chipselect on this USI link config */
  133. if (is_usidev(spi))
  134. return;
  135. /* here, value == "activate or not" */
  136. /* most PARPORT_CONTROL_* bits are negated */
  137. if (spi_cs_bit == PARPORT_CONTROL_INIT)
  138. value = !value;
  139. /* here, value == "bit value to write in control register" */
  140. parport_frob_control(pp->port, spi_cs_bit, value ? spi_cs_bit : 0);
  141. }
  142. /* we only needed to implement one mode here, and choose SPI_MODE_0 */
  143. #define spidelay(X) do{}while(0)
  144. //#define spidelay ndelay
  145. #define EXPAND_BITBANG_TXRX
  146. #include <linux/spi/spi_bitbang.h>
  147. static u32
  148. butterfly_txrx_word_mode0(struct spi_device *spi,
  149. unsigned nsecs,
  150. u32 word, u8 bits)
  151. {
  152. return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
  153. }
  154. /*----------------------------------------------------------------------*/
  155. /* override default partitioning with cmdlinepart */
  156. static struct mtd_partition partitions[] = { {
  157. /* JFFS2 wants partitions of 4*N blocks for this device ... */
  158. /* sector 0 = 8 pages * 264 bytes/page (1 block)
  159. * sector 1 = 248 pages * 264 bytes/page
  160. */
  161. .name = "bookkeeping", // 66 KB
  162. .offset = 0,
  163. .size = (8 + 248) * 264,
  164. // .mask_flags = MTD_WRITEABLE,
  165. }, {
  166. /* sector 2 = 256 pages * 264 bytes/page
  167. * sectors 3-5 = 512 pages * 264 bytes/page
  168. */
  169. .name = "filesystem", // 462 KB
  170. .offset = MTDPART_OFS_APPEND,
  171. .size = MTDPART_SIZ_FULL,
  172. } };
  173. static struct flash_platform_data flash = {
  174. .name = "butterflash",
  175. .parts = partitions,
  176. .nr_parts = ARRAY_SIZE(partitions),
  177. };
  178. /* REVISIT remove this ugly global and its "only one" limitation */
  179. static struct butterfly *butterfly;
  180. static void butterfly_attach(struct parport *p)
  181. {
  182. struct pardevice *pd;
  183. int status;
  184. struct butterfly *pp;
  185. struct spi_master *master;
  186. struct platform_device *pdev;
  187. if (butterfly)
  188. return;
  189. /* REVISIT: this just _assumes_ a butterfly is there ... no probe,
  190. * and no way to be selective about what it binds to.
  191. */
  192. /* FIXME where should master->cdev.dev come from?
  193. * e.g. /sys/bus/pnp0/00:0b, some PCI thing, etc
  194. * setting up a platform device like this is an ugly kluge...
  195. */
  196. pdev = platform_device_register_simple("butterfly", -1, NULL, 0);
  197. master = spi_alloc_master(&pdev->dev, sizeof *pp);
  198. if (!master) {
  199. status = -ENOMEM;
  200. goto done;
  201. }
  202. pp = spi_master_get_devdata(master);
  203. /*
  204. * SPI and bitbang hookup
  205. *
  206. * use default setup(), cleanup(), and transfer() methods; and
  207. * only bother implementing mode 0. Start it later.
  208. */
  209. master->bus_num = 42;
  210. master->num_chipselect = 2;
  211. pp->bitbang.master = spi_master_get(master);
  212. pp->bitbang.chipselect = butterfly_chipselect;
  213. pp->bitbang.txrx_word[SPI_MODE_0] = butterfly_txrx_word_mode0;
  214. /*
  215. * parport hookup
  216. */
  217. pp->port = p;
  218. pd = parport_register_device(p, "spi_butterfly",
  219. NULL, NULL, NULL,
  220. 0 /* FLAGS */, pp);
  221. if (!pd) {
  222. status = -ENOMEM;
  223. goto clean0;
  224. }
  225. pp->pd = pd;
  226. status = parport_claim(pd);
  227. if (status < 0)
  228. goto clean1;
  229. /*
  230. * Butterfly reset, powerup, run firmware
  231. */
  232. pr_debug("%s: powerup/reset Butterfly\n", p->name);
  233. /* nCS for dataflash (this bit is inverted on output) */
  234. parport_frob_control(pp->port, spi_cs_bit, 0);
  235. /* stabilize power with chip in reset (nRESET), and
  236. * both spi_sck_bit and usi_sck_bit clear (CPOL=0)
  237. */
  238. pp->lastbyte |= vcc_bits;
  239. parport_write_data(pp->port, pp->lastbyte);
  240. msleep(5);
  241. /* take it out of reset; assume long reset delay */
  242. pp->lastbyte |= butterfly_nreset;
  243. parport_write_data(pp->port, pp->lastbyte);
  244. msleep(100);
  245. /*
  246. * Start SPI ... for now, hide that we're two physical busses.
  247. */
  248. status = spi_bitbang_start(&pp->bitbang);
  249. if (status < 0)
  250. goto clean2;
  251. /* Bus 1 lets us talk to at45db041b (firmware disables AVR)
  252. * or AVR (firmware resets at45, acts as spi slave)
  253. */
  254. pp->info[0].max_speed_hz = 15 * 1000 * 1000;
  255. strcpy(pp->info[0].modalias, "mtd_dataflash");
  256. pp->info[0].platform_data = &flash;
  257. pp->info[0].chip_select = 1;
  258. pp->info[0].controller_data = pp;
  259. pp->dataflash = spi_new_device(pp->bitbang.master, &pp->info[0]);
  260. if (pp->dataflash)
  261. pr_debug("%s: dataflash at %s\n", p->name,
  262. pp->dataflash->dev.bus_id);
  263. #ifdef HAVE_USI
  264. /* even more custom AVR firmware */
  265. pp->info[1].max_speed_hz = 10 /* ?? */ * 1000 * 1000;
  266. strcpy(pp->info[1].modalias, "butterfly");
  267. // pp->info[1].platform_data = ... TBD ... ;
  268. pp->info[1].chip_select = 2,
  269. pp->info[1].controller_data = pp;
  270. pp->butterfly = spi_new_device(pp->bitbang.master, &pp->info[1]);
  271. if (pp->butterfly)
  272. pr_debug("%s: butterfly at %s\n", p->name,
  273. pp->butterfly->dev.bus_id);
  274. /* FIXME setup ACK for the IRQ line ... */
  275. #endif
  276. // dev_info(_what?_, ...)
  277. pr_info("%s: AVR Butterfly\n", p->name);
  278. butterfly = pp;
  279. return;
  280. clean2:
  281. /* turn off VCC */
  282. parport_write_data(pp->port, 0);
  283. parport_release(pp->pd);
  284. clean1:
  285. parport_unregister_device(pd);
  286. clean0:
  287. (void) spi_master_put(pp->bitbang.master);
  288. done:
  289. platform_device_unregister(pdev);
  290. pr_debug("%s: butterfly probe, fail %d\n", p->name, status);
  291. }
  292. static void butterfly_detach(struct parport *p)
  293. {
  294. struct butterfly *pp;
  295. struct platform_device *pdev;
  296. int status;
  297. /* FIXME this global is ugly ... but, how to quickly get from
  298. * the parport to the "struct butterfly" associated with it?
  299. * "old school" driver-internal device lists?
  300. */
  301. if (!butterfly || butterfly->port != p)
  302. return;
  303. pp = butterfly;
  304. butterfly = NULL;
  305. #ifdef HAVE_USI
  306. spi_unregister_device(pp->butterfly);
  307. pp->butterfly = NULL;
  308. #endif
  309. spi_unregister_device(pp->dataflash);
  310. pp->dataflash = NULL;
  311. status = spi_bitbang_stop(&pp->bitbang);
  312. /* turn off VCC */
  313. parport_write_data(pp->port, 0);
  314. msleep(10);
  315. parport_release(pp->pd);
  316. parport_unregister_device(pp->pd);
  317. pdev = to_platform_device(pp->bitbang.master->cdev.dev);
  318. (void) spi_master_put(pp->bitbang.master);
  319. platform_device_unregister(pdev);
  320. }
  321. static struct parport_driver butterfly_driver = {
  322. .name = "spi_butterfly",
  323. .attach = butterfly_attach,
  324. .detach = butterfly_detach,
  325. };
  326. static int __init butterfly_init(void)
  327. {
  328. return parport_register_driver(&butterfly_driver);
  329. }
  330. device_initcall(butterfly_init);
  331. static void __exit butterfly_exit(void)
  332. {
  333. parport_unregister_driver(&butterfly_driver);
  334. }
  335. module_exit(butterfly_exit);
  336. MODULE_LICENSE("GPL");