spi_butterfly.c 8.5 KB

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