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/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 != BITBANG_CS_INACTIVE)
  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, so we must
  137. * morph it to value == "bit value to write in control register"
  138. */
  139. if (spi_cs_bit == PARPORT_CONTROL_INIT)
  140. value = !value;
  141. parport_frob_control(pp->port, spi_cs_bit, value ? spi_cs_bit : 0);
  142. }
  143. /* we only needed to implement one mode here, and choose SPI_MODE_0 */
  144. #define spidelay(X) do{}while(0)
  145. //#define spidelay ndelay
  146. #define EXPAND_BITBANG_TXRX
  147. #include <linux/spi/spi_bitbang.h>
  148. static u32
  149. butterfly_txrx_word_mode0(struct spi_device *spi,
  150. unsigned nsecs,
  151. u32 word, u8 bits)
  152. {
  153. return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
  154. }
  155. /*----------------------------------------------------------------------*/
  156. /* override default partitioning with cmdlinepart */
  157. static struct mtd_partition partitions[] = { {
  158. /* JFFS2 wants partitions of 4*N blocks for this device,
  159. * so sectors 0 and 1 can't be partitions by themselves.
  160. */
  161. /* sector 0 = 8 pages * 264 bytes/page (1 block)
  162. * sector 1 = 248 pages * 264 bytes/page
  163. */
  164. .name = "bookkeeping", // 66 KB
  165. .offset = 0,
  166. .size = (8 + 248) * 264,
  167. // .mask_flags = MTD_WRITEABLE,
  168. }, {
  169. /* sector 2 = 256 pages * 264 bytes/page
  170. * sectors 3-5 = 512 pages * 264 bytes/page
  171. */
  172. .name = "filesystem", // 462 KB
  173. .offset = MTDPART_OFS_APPEND,
  174. .size = MTDPART_SIZ_FULL,
  175. } };
  176. static struct flash_platform_data flash = {
  177. .name = "butterflash",
  178. .parts = partitions,
  179. .nr_parts = ARRAY_SIZE(partitions),
  180. };
  181. /* REVISIT remove this ugly global and its "only one" limitation */
  182. static struct butterfly *butterfly;
  183. static void butterfly_attach(struct parport *p)
  184. {
  185. struct pardevice *pd;
  186. int status;
  187. struct butterfly *pp;
  188. struct spi_master *master;
  189. struct platform_device *pdev;
  190. if (butterfly)
  191. return;
  192. /* REVISIT: this just _assumes_ a butterfly is there ... no probe,
  193. * and no way to be selective about what it binds to.
  194. */
  195. /* FIXME where should master->cdev.dev come from?
  196. * e.g. /sys/bus/pnp0/00:0b, some PCI thing, etc
  197. * setting up a platform device like this is an ugly kluge...
  198. */
  199. pdev = platform_device_register_simple("butterfly", -1, NULL, 0);
  200. master = spi_alloc_master(&pdev->dev, sizeof *pp);
  201. if (!master) {
  202. status = -ENOMEM;
  203. goto done;
  204. }
  205. pp = spi_master_get_devdata(master);
  206. /*
  207. * SPI and bitbang hookup
  208. *
  209. * use default setup(), cleanup(), and transfer() methods; and
  210. * only bother implementing mode 0. Start it later.
  211. */
  212. master->bus_num = 42;
  213. master->num_chipselect = 2;
  214. pp->bitbang.master = spi_master_get(master);
  215. pp->bitbang.chipselect = butterfly_chipselect;
  216. pp->bitbang.txrx_word[SPI_MODE_0] = butterfly_txrx_word_mode0;
  217. /*
  218. * parport hookup
  219. */
  220. pp->port = p;
  221. pd = parport_register_device(p, "spi_butterfly",
  222. NULL, NULL, NULL,
  223. 0 /* FLAGS */, pp);
  224. if (!pd) {
  225. status = -ENOMEM;
  226. goto clean0;
  227. }
  228. pp->pd = pd;
  229. status = parport_claim(pd);
  230. if (status < 0)
  231. goto clean1;
  232. /*
  233. * Butterfly reset, powerup, run firmware
  234. */
  235. pr_debug("%s: powerup/reset Butterfly\n", p->name);
  236. /* nCS for dataflash (this bit is inverted on output) */
  237. parport_frob_control(pp->port, spi_cs_bit, 0);
  238. /* stabilize power with chip in reset (nRESET), and
  239. * both spi_sck_bit and usi_sck_bit clear (CPOL=0)
  240. */
  241. pp->lastbyte |= vcc_bits;
  242. parport_write_data(pp->port, pp->lastbyte);
  243. msleep(5);
  244. /* take it out of reset; assume long reset delay */
  245. pp->lastbyte |= butterfly_nreset;
  246. parport_write_data(pp->port, pp->lastbyte);
  247. msleep(100);
  248. /*
  249. * Start SPI ... for now, hide that we're two physical busses.
  250. */
  251. status = spi_bitbang_start(&pp->bitbang);
  252. if (status < 0)
  253. goto clean2;
  254. /* Bus 1 lets us talk to at45db041b (firmware disables AVR SPI), AVR
  255. * (firmware resets at45, acts as spi slave) or neither (we ignore
  256. * both, AVR uses AT45). Here we expect firmware for the first option.
  257. */
  258. pp->info[0].max_speed_hz = 15 * 1000 * 1000;
  259. strcpy(pp->info[0].modalias, "mtd_dataflash");
  260. pp->info[0].platform_data = &flash;
  261. pp->info[0].chip_select = 1;
  262. pp->info[0].controller_data = pp;
  263. pp->dataflash = spi_new_device(pp->bitbang.master, &pp->info[0]);
  264. if (pp->dataflash)
  265. pr_debug("%s: dataflash at %s\n", p->name,
  266. pp->dataflash->dev.bus_id);
  267. #ifdef HAVE_USI
  268. /* Bus 2 is only for talking to the AVR, and it can work no
  269. * matter who masters bus 1; needs appropriate AVR firmware.
  270. */
  271. pp->info[1].max_speed_hz = 10 /* ?? */ * 1000 * 1000;
  272. strcpy(pp->info[1].modalias, "butterfly");
  273. // pp->info[1].platform_data = ... TBD ... ;
  274. pp->info[1].chip_select = 2,
  275. pp->info[1].controller_data = pp;
  276. pp->butterfly = spi_new_device(pp->bitbang.master, &pp->info[1]);
  277. if (pp->butterfly)
  278. pr_debug("%s: butterfly at %s\n", p->name,
  279. pp->butterfly->dev.bus_id);
  280. /* FIXME setup ACK for the IRQ line ... */
  281. #endif
  282. // dev_info(_what?_, ...)
  283. pr_info("%s: AVR Butterfly\n", p->name);
  284. butterfly = pp;
  285. return;
  286. clean2:
  287. /* turn off VCC */
  288. parport_write_data(pp->port, 0);
  289. parport_release(pp->pd);
  290. clean1:
  291. parport_unregister_device(pd);
  292. clean0:
  293. (void) spi_master_put(pp->bitbang.master);
  294. done:
  295. platform_device_unregister(pdev);
  296. pr_debug("%s: butterfly probe, fail %d\n", p->name, status);
  297. }
  298. static void butterfly_detach(struct parport *p)
  299. {
  300. struct butterfly *pp;
  301. struct platform_device *pdev;
  302. int status;
  303. /* FIXME this global is ugly ... but, how to quickly get from
  304. * the parport to the "struct butterfly" associated with it?
  305. * "old school" driver-internal device lists?
  306. */
  307. if (!butterfly || butterfly->port != p)
  308. return;
  309. pp = butterfly;
  310. butterfly = NULL;
  311. /* stop() unregisters child devices too */
  312. pdev = to_platform_device(pp->bitbang.master->cdev.dev);
  313. status = spi_bitbang_stop(&pp->bitbang);
  314. /* turn off VCC */
  315. parport_write_data(pp->port, 0);
  316. msleep(10);
  317. parport_release(pp->pd);
  318. parport_unregister_device(pp->pd);
  319. (void) spi_master_put(pp->bitbang.master);
  320. platform_device_unregister(pdev);
  321. }
  322. static struct parport_driver butterfly_driver = {
  323. .name = "spi_butterfly",
  324. .attach = butterfly_attach,
  325. .detach = butterfly_detach,
  326. };
  327. static int __init butterfly_init(void)
  328. {
  329. return parport_register_driver(&butterfly_driver);
  330. }
  331. device_initcall(butterfly_init);
  332. static void __exit butterfly_exit(void)
  333. {
  334. parport_unregister_driver(&butterfly_driver);
  335. }
  336. module_exit(butterfly_exit);
  337. MODULE_DESCRIPTION("Parport Adapter driver for AVR Butterfly");
  338. MODULE_LICENSE("GPL");