spi_butterfly.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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/sched.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. if (IS_ERR(pdev))
  201. return;
  202. master = spi_alloc_master(&pdev->dev, sizeof *pp);
  203. if (!master) {
  204. status = -ENOMEM;
  205. goto done;
  206. }
  207. pp = spi_master_get_devdata(master);
  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. pp->bitbang.master = spi_master_get(master);
  217. pp->bitbang.chipselect = butterfly_chipselect;
  218. pp->bitbang.txrx_word[SPI_MODE_0] = butterfly_txrx_word_mode0;
  219. /*
  220. * parport hookup
  221. */
  222. pp->port = p;
  223. pd = parport_register_device(p, "spi_butterfly",
  224. NULL, NULL, NULL,
  225. 0 /* FLAGS */, pp);
  226. if (!pd) {
  227. status = -ENOMEM;
  228. goto clean0;
  229. }
  230. pp->pd = pd;
  231. status = parport_claim(pd);
  232. if (status < 0)
  233. goto clean1;
  234. /*
  235. * Butterfly reset, powerup, run firmware
  236. */
  237. pr_debug("%s: powerup/reset Butterfly\n", p->name);
  238. /* nCS for dataflash (this bit is inverted on output) */
  239. parport_frob_control(pp->port, spi_cs_bit, 0);
  240. /* stabilize power with chip in reset (nRESET), and
  241. * both spi_sck_bit and usi_sck_bit clear (CPOL=0)
  242. */
  243. pp->lastbyte |= vcc_bits;
  244. parport_write_data(pp->port, pp->lastbyte);
  245. msleep(5);
  246. /* take it out of reset; assume long reset delay */
  247. pp->lastbyte |= butterfly_nreset;
  248. parport_write_data(pp->port, pp->lastbyte);
  249. msleep(100);
  250. /*
  251. * Start SPI ... for now, hide that we're two physical busses.
  252. */
  253. status = spi_bitbang_start(&pp->bitbang);
  254. if (status < 0)
  255. goto clean2;
  256. /* Bus 1 lets us talk to at45db041b (firmware disables AVR SPI), AVR
  257. * (firmware resets at45, acts as spi slave) or neither (we ignore
  258. * both, AVR uses AT45). Here we expect firmware for the first option.
  259. */
  260. pp->info[0].max_speed_hz = 15 * 1000 * 1000;
  261. strcpy(pp->info[0].modalias, "mtd_dataflash");
  262. pp->info[0].platform_data = &flash;
  263. pp->info[0].chip_select = 1;
  264. pp->info[0].controller_data = pp;
  265. pp->dataflash = spi_new_device(pp->bitbang.master, &pp->info[0]);
  266. if (pp->dataflash)
  267. pr_debug("%s: dataflash at %s\n", p->name,
  268. pp->dataflash->dev.bus_id);
  269. #ifdef HAVE_USI
  270. /* Bus 2 is only for talking to the AVR, and it can work no
  271. * matter who masters bus 1; needs appropriate AVR firmware.
  272. */
  273. pp->info[1].max_speed_hz = 10 /* ?? */ * 1000 * 1000;
  274. strcpy(pp->info[1].modalias, "butterfly");
  275. // pp->info[1].platform_data = ... TBD ... ;
  276. pp->info[1].chip_select = 2,
  277. pp->info[1].controller_data = pp;
  278. pp->butterfly = spi_new_device(pp->bitbang.master, &pp->info[1]);
  279. if (pp->butterfly)
  280. pr_debug("%s: butterfly at %s\n", p->name,
  281. pp->butterfly->dev.bus_id);
  282. /* FIXME setup ACK for the IRQ line ... */
  283. #endif
  284. // dev_info(_what?_, ...)
  285. pr_info("%s: AVR Butterfly\n", p->name);
  286. butterfly = pp;
  287. return;
  288. clean2:
  289. /* turn off VCC */
  290. parport_write_data(pp->port, 0);
  291. parport_release(pp->pd);
  292. clean1:
  293. parport_unregister_device(pd);
  294. clean0:
  295. (void) spi_master_put(pp->bitbang.master);
  296. done:
  297. platform_device_unregister(pdev);
  298. pr_debug("%s: butterfly probe, fail %d\n", p->name, status);
  299. }
  300. static void butterfly_detach(struct parport *p)
  301. {
  302. struct butterfly *pp;
  303. struct platform_device *pdev;
  304. int status;
  305. /* FIXME this global is ugly ... but, how to quickly get from
  306. * the parport to the "struct butterfly" associated with it?
  307. * "old school" driver-internal device lists?
  308. */
  309. if (!butterfly || butterfly->port != p)
  310. return;
  311. pp = butterfly;
  312. butterfly = NULL;
  313. /* stop() unregisters child devices too */
  314. pdev = to_platform_device(pp->bitbang.master->cdev.dev);
  315. status = spi_bitbang_stop(&pp->bitbang);
  316. /* turn off VCC */
  317. parport_write_data(pp->port, 0);
  318. msleep(10);
  319. parport_release(pp->pd);
  320. parport_unregister_device(pp->pd);
  321. (void) spi_master_put(pp->bitbang.master);
  322. platform_device_unregister(pdev);
  323. }
  324. static struct parport_driver butterfly_driver = {
  325. .name = "spi_butterfly",
  326. .attach = butterfly_attach,
  327. .detach = butterfly_detach,
  328. };
  329. static int __init butterfly_init(void)
  330. {
  331. return parport_register_driver(&butterfly_driver);
  332. }
  333. device_initcall(butterfly_init);
  334. static void __exit butterfly_exit(void)
  335. {
  336. parport_unregister_driver(&butterfly_driver);
  337. }
  338. module_exit(butterfly_exit);
  339. MODULE_DESCRIPTION("Parport Adapter driver for AVR Butterfly");
  340. MODULE_LICENSE("GPL");