spi-butterfly.c 7.9 KB

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