spi_butterfly.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/parport.h>
  25. #include <linux/spi/spi.h>
  26. #include <linux/spi/spi_bitbang.h>
  27. #include <linux/spi/flash.h>
  28. #include <linux/mtd/partitions.h>
  29. /*
  30. * This uses SPI to talk with an "AVR Butterfly", which is a $US20 card
  31. * with a battery powered AVR microcontroller and lots of goodies. You
  32. * can use GCC to develop firmware for this.
  33. *
  34. * See Documentation/spi/butterfly for information about how to build
  35. * and use this custom parallel port cable.
  36. */
  37. #undef HAVE_USI /* nyet */
  38. /* DATA output bits (pins 2..9 == D0..D7) */
  39. #define butterfly_nreset (1 << 1) /* pin 3 */
  40. #define spi_sck_bit (1 << 0) /* pin 2 */
  41. #define spi_mosi_bit (1 << 7) /* pin 9 */
  42. #define usi_sck_bit (1 << 3) /* pin 5 */
  43. #define usi_mosi_bit (1 << 4) /* pin 6 */
  44. #define vcc_bits ((1 << 6) | (1 << 5)) /* pins 7, 8 */
  45. /* STATUS input bits */
  46. #define spi_miso_bit PARPORT_STATUS_BUSY /* pin 11 */
  47. #define usi_miso_bit PARPORT_STATUS_PAPEROUT /* pin 12 */
  48. /* CONTROL output bits */
  49. #define spi_cs_bit PARPORT_CONTROL_SELECT /* pin 17 */
  50. /* USI uses no chipselect */
  51. static inline struct butterfly *spidev_to_pp(struct spi_device *spi)
  52. {
  53. return spi->controller_data;
  54. }
  55. static inline int is_usidev(struct spi_device *spi)
  56. {
  57. #ifdef HAVE_USI
  58. return spi->chip_select != 1;
  59. #else
  60. return 0;
  61. #endif
  62. }
  63. struct butterfly {
  64. /* REVISIT ... for now, this must be first */
  65. struct spi_bitbang bitbang;
  66. struct parport *port;
  67. struct pardevice *pd;
  68. u8 lastbyte;
  69. struct spi_device *dataflash;
  70. struct spi_device *butterfly;
  71. struct spi_board_info info[2];
  72. };
  73. /*----------------------------------------------------------------------*/
  74. /*
  75. * these routines may be slower than necessary because they're hiding
  76. * the fact that there are two different SPI busses on this cable: one
  77. * to the DataFlash chip (or AVR SPI controller), the other to the
  78. * AVR USI controller.
  79. */
  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. if (is_usidev(spi))
  86. bit = usi_sck_bit;
  87. else
  88. bit = spi_sck_bit;
  89. if (is_on)
  90. byte |= bit;
  91. else
  92. byte &= ~bit;
  93. parport_write_data(pp->port, byte);
  94. pp->lastbyte = byte;
  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. if (is_usidev(spi))
  102. bit = usi_mosi_bit;
  103. else
  104. bit = spi_mosi_bit;
  105. if (is_on)
  106. byte |= bit;
  107. else
  108. byte &= ~bit;
  109. parport_write_data(pp->port, byte);
  110. pp->lastbyte = byte;
  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. if (is_usidev(spi))
  118. bit = usi_miso_bit;
  119. else
  120. bit = spi_miso_bit;
  121. /* only STATUS_BUSY is NOT negated */
  122. value = !(parport_read_status(pp->port) & bit);
  123. return (bit == PARPORT_STATUS_BUSY) ? value : !value;
  124. }
  125. static void butterfly_chipselect(struct spi_device *spi, int value)
  126. {
  127. struct butterfly *pp = spidev_to_pp(spi);
  128. /* set default clock polarity */
  129. if (value != BITBANG_CS_INACTIVE)
  130. setsck(spi, spi->mode & SPI_CPOL);
  131. /* no chipselect on this USI link config */
  132. if (is_usidev(spi))
  133. return;
  134. /* here, value == "activate or not";
  135. * most PARPORT_CONTROL_* bits are negated, so we must
  136. * morph it to value == "bit value to write in control register"
  137. */
  138. if (spi_cs_bit == PARPORT_CONTROL_INIT)
  139. value = !value;
  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. * so sectors 0 and 1 can't be partitions by themselves.
  159. */
  160. /* sector 0 = 8 pages * 264 bytes/page (1 block)
  161. * sector 1 = 248 pages * 264 bytes/page
  162. */
  163. .name = "bookkeeping", // 66 KB
  164. .offset = 0,
  165. .size = (8 + 248) * 264,
  166. // .mask_flags = MTD_WRITEABLE,
  167. }, {
  168. /* sector 2 = 256 pages * 264 bytes/page
  169. * sectors 3-5 = 512 pages * 264 bytes/page
  170. */
  171. .name = "filesystem", // 462 KB
  172. .offset = MTDPART_OFS_APPEND,
  173. .size = MTDPART_SIZ_FULL,
  174. } };
  175. static struct flash_platform_data flash = {
  176. .name = "butterflash",
  177. .parts = partitions,
  178. .nr_parts = ARRAY_SIZE(partitions),
  179. };
  180. /* REVISIT remove this ugly global and its "only one" limitation */
  181. static struct butterfly *butterfly;
  182. static void butterfly_attach(struct parport *p)
  183. {
  184. struct pardevice *pd;
  185. int status;
  186. struct butterfly *pp;
  187. struct spi_master *master;
  188. struct platform_device *pdev;
  189. if (butterfly)
  190. return;
  191. /* REVISIT: this just _assumes_ a butterfly is there ... no probe,
  192. * and no way to be selective about what it binds to.
  193. */
  194. /* FIXME where should master->cdev.dev come from?
  195. * e.g. /sys/bus/pnp0/00:0b, some PCI thing, etc
  196. * setting up a platform device like this is an ugly kluge...
  197. */
  198. pdev = platform_device_register_simple("butterfly", -1, NULL, 0);
  199. master = spi_alloc_master(&pdev->dev, sizeof *pp);
  200. if (!master) {
  201. status = -ENOMEM;
  202. goto done;
  203. }
  204. pp = spi_master_get_devdata(master);
  205. /*
  206. * SPI and bitbang hookup
  207. *
  208. * use default setup(), cleanup(), and transfer() methods; and
  209. * only bother implementing mode 0. Start it later.
  210. */
  211. master->bus_num = 42;
  212. master->num_chipselect = 2;
  213. pp->bitbang.master = spi_master_get(master);
  214. pp->bitbang.chipselect = butterfly_chipselect;
  215. pp->bitbang.txrx_word[SPI_MODE_0] = butterfly_txrx_word_mode0;
  216. /*
  217. * parport hookup
  218. */
  219. pp->port = p;
  220. pd = parport_register_device(p, "spi_butterfly",
  221. NULL, NULL, NULL,
  222. 0 /* FLAGS */, pp);
  223. if (!pd) {
  224. status = -ENOMEM;
  225. goto clean0;
  226. }
  227. pp->pd = pd;
  228. status = parport_claim(pd);
  229. if (status < 0)
  230. goto clean1;
  231. /*
  232. * Butterfly reset, powerup, run firmware
  233. */
  234. pr_debug("%s: powerup/reset Butterfly\n", p->name);
  235. /* nCS for dataflash (this bit is inverted on output) */
  236. parport_frob_control(pp->port, spi_cs_bit, 0);
  237. /* stabilize power with chip in reset (nRESET), and
  238. * both spi_sck_bit and usi_sck_bit clear (CPOL=0)
  239. */
  240. pp->lastbyte |= vcc_bits;
  241. parport_write_data(pp->port, pp->lastbyte);
  242. msleep(5);
  243. /* take it out of reset; assume long reset delay */
  244. pp->lastbyte |= butterfly_nreset;
  245. parport_write_data(pp->port, pp->lastbyte);
  246. msleep(100);
  247. /*
  248. * Start SPI ... for now, hide that we're two physical busses.
  249. */
  250. status = spi_bitbang_start(&pp->bitbang);
  251. if (status < 0)
  252. goto clean2;
  253. /* Bus 1 lets us talk to at45db041b (firmware disables AVR SPI), AVR
  254. * (firmware resets at45, acts as spi slave) or neither (we ignore
  255. * both, AVR uses AT45). Here we expect firmware for the first option.
  256. */
  257. pp->info[0].max_speed_hz = 15 * 1000 * 1000;
  258. strcpy(pp->info[0].modalias, "mtd_dataflash");
  259. pp->info[0].platform_data = &flash;
  260. pp->info[0].chip_select = 1;
  261. pp->info[0].controller_data = pp;
  262. pp->dataflash = spi_new_device(pp->bitbang.master, &pp->info[0]);
  263. if (pp->dataflash)
  264. pr_debug("%s: dataflash at %s\n", p->name,
  265. pp->dataflash->dev.bus_id);
  266. #ifdef HAVE_USI
  267. /* Bus 2 is only for talking to the AVR, and it can work no
  268. * matter who masters bus 1; needs appropriate AVR firmware.
  269. */
  270. pp->info[1].max_speed_hz = 10 /* ?? */ * 1000 * 1000;
  271. strcpy(pp->info[1].modalias, "butterfly");
  272. // pp->info[1].platform_data = ... TBD ... ;
  273. pp->info[1].chip_select = 2,
  274. pp->info[1].controller_data = pp;
  275. pp->butterfly = spi_new_device(pp->bitbang.master, &pp->info[1]);
  276. if (pp->butterfly)
  277. pr_debug("%s: butterfly at %s\n", p->name,
  278. pp->butterfly->dev.bus_id);
  279. /* FIXME setup ACK for the IRQ line ... */
  280. #endif
  281. // dev_info(_what?_, ...)
  282. pr_info("%s: AVR Butterfly\n", p->name);
  283. butterfly = pp;
  284. return;
  285. clean2:
  286. /* turn off VCC */
  287. parport_write_data(pp->port, 0);
  288. parport_release(pp->pd);
  289. clean1:
  290. parport_unregister_device(pd);
  291. clean0:
  292. (void) spi_master_put(pp->bitbang.master);
  293. done:
  294. platform_device_unregister(pdev);
  295. pr_debug("%s: butterfly probe, fail %d\n", p->name, status);
  296. }
  297. static void butterfly_detach(struct parport *p)
  298. {
  299. struct butterfly *pp;
  300. struct platform_device *pdev;
  301. int status;
  302. /* FIXME this global is ugly ... but, how to quickly get from
  303. * the parport to the "struct butterfly" associated with it?
  304. * "old school" driver-internal device lists?
  305. */
  306. if (!butterfly || butterfly->port != p)
  307. return;
  308. pp = butterfly;
  309. butterfly = NULL;
  310. /* stop() unregisters child devices too */
  311. pdev = to_platform_device(pp->bitbang.master->cdev.dev);
  312. status = spi_bitbang_stop(&pp->bitbang);
  313. /* turn off VCC */
  314. parport_write_data(pp->port, 0);
  315. msleep(10);
  316. parport_release(pp->pd);
  317. parport_unregister_device(pp->pd);
  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_DESCRIPTION("Parport Adapter driver for AVR Butterfly");
  337. MODULE_LICENSE("GPL");