mchp23k256.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * mchp23k256.c
  3. *
  4. * Driver for Microchip 23k256 SPI RAM chips
  5. *
  6. * Copyright © 2016 Andrew Lunn <andrew@lunn.ch>
  7. *
  8. * This code is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/device.h>
  14. #include <linux/module.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/mtd/partitions.h>
  17. #include <linux/mutex.h>
  18. #include <linux/sched.h>
  19. #include <linux/sizes.h>
  20. #include <linux/spi/flash.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/of_device.h>
  23. #define MAX_CMD_SIZE 4
  24. struct mchp23_caps {
  25. u8 addr_width;
  26. unsigned int size;
  27. };
  28. struct mchp23k256_flash {
  29. struct spi_device *spi;
  30. struct mutex lock;
  31. struct mtd_info mtd;
  32. const struct mchp23_caps *caps;
  33. };
  34. #define MCHP23K256_CMD_WRITE_STATUS 0x01
  35. #define MCHP23K256_CMD_WRITE 0x02
  36. #define MCHP23K256_CMD_READ 0x03
  37. #define MCHP23K256_MODE_SEQ BIT(6)
  38. #define to_mchp23k256_flash(x) container_of(x, struct mchp23k256_flash, mtd)
  39. static void mchp23k256_addr2cmd(struct mchp23k256_flash *flash,
  40. unsigned int addr, u8 *cmd)
  41. {
  42. int i;
  43. /*
  44. * Address is sent in big endian (MSB first) and we skip
  45. * the first entry of the cmd array which contains the cmd
  46. * opcode.
  47. */
  48. for (i = flash->caps->addr_width; i > 0; i--, addr >>= 8)
  49. cmd[i] = addr;
  50. }
  51. static int mchp23k256_cmdsz(struct mchp23k256_flash *flash)
  52. {
  53. return 1 + flash->caps->addr_width;
  54. }
  55. static int mchp23k256_write(struct mtd_info *mtd, loff_t to, size_t len,
  56. size_t *retlen, const unsigned char *buf)
  57. {
  58. struct mchp23k256_flash *flash = to_mchp23k256_flash(mtd);
  59. struct spi_transfer transfer[2] = {};
  60. struct spi_message message;
  61. unsigned char command[MAX_CMD_SIZE];
  62. int ret;
  63. spi_message_init(&message);
  64. command[0] = MCHP23K256_CMD_WRITE;
  65. mchp23k256_addr2cmd(flash, to, command);
  66. transfer[0].tx_buf = command;
  67. transfer[0].len = mchp23k256_cmdsz(flash);
  68. spi_message_add_tail(&transfer[0], &message);
  69. transfer[1].tx_buf = buf;
  70. transfer[1].len = len;
  71. spi_message_add_tail(&transfer[1], &message);
  72. mutex_lock(&flash->lock);
  73. ret = spi_sync(flash->spi, &message);
  74. mutex_unlock(&flash->lock);
  75. if (ret)
  76. return ret;
  77. if (retlen && message.actual_length > sizeof(command))
  78. *retlen += message.actual_length - sizeof(command);
  79. return 0;
  80. }
  81. static int mchp23k256_read(struct mtd_info *mtd, loff_t from, size_t len,
  82. size_t *retlen, unsigned char *buf)
  83. {
  84. struct mchp23k256_flash *flash = to_mchp23k256_flash(mtd);
  85. struct spi_transfer transfer[2] = {};
  86. struct spi_message message;
  87. unsigned char command[MAX_CMD_SIZE];
  88. int ret;
  89. spi_message_init(&message);
  90. memset(&transfer, 0, sizeof(transfer));
  91. command[0] = MCHP23K256_CMD_READ;
  92. mchp23k256_addr2cmd(flash, from, command);
  93. transfer[0].tx_buf = command;
  94. transfer[0].len = mchp23k256_cmdsz(flash);
  95. spi_message_add_tail(&transfer[0], &message);
  96. transfer[1].rx_buf = buf;
  97. transfer[1].len = len;
  98. spi_message_add_tail(&transfer[1], &message);
  99. mutex_lock(&flash->lock);
  100. ret = spi_sync(flash->spi, &message);
  101. mutex_unlock(&flash->lock);
  102. if (ret)
  103. return ret;
  104. if (retlen && message.actual_length > sizeof(command))
  105. *retlen += message.actual_length - sizeof(command);
  106. return 0;
  107. }
  108. /*
  109. * Set the device into sequential mode. This allows read/writes to the
  110. * entire SRAM in a single operation
  111. */
  112. static int mchp23k256_set_mode(struct spi_device *spi)
  113. {
  114. struct spi_transfer transfer = {};
  115. struct spi_message message;
  116. unsigned char command[2];
  117. spi_message_init(&message);
  118. command[0] = MCHP23K256_CMD_WRITE_STATUS;
  119. command[1] = MCHP23K256_MODE_SEQ;
  120. transfer.tx_buf = command;
  121. transfer.len = sizeof(command);
  122. spi_message_add_tail(&transfer, &message);
  123. return spi_sync(spi, &message);
  124. }
  125. static const struct mchp23_caps mchp23k256_caps = {
  126. .size = SZ_32K,
  127. .addr_width = 2,
  128. };
  129. static const struct mchp23_caps mchp23lcv1024_caps = {
  130. .size = SZ_128K,
  131. .addr_width = 3,
  132. };
  133. static int mchp23k256_probe(struct spi_device *spi)
  134. {
  135. struct mchp23k256_flash *flash;
  136. struct flash_platform_data *data;
  137. int err;
  138. flash = devm_kzalloc(&spi->dev, sizeof(*flash), GFP_KERNEL);
  139. if (!flash)
  140. return -ENOMEM;
  141. flash->spi = spi;
  142. mutex_init(&flash->lock);
  143. spi_set_drvdata(spi, flash);
  144. err = mchp23k256_set_mode(spi);
  145. if (err)
  146. return err;
  147. data = dev_get_platdata(&spi->dev);
  148. flash->caps = of_device_get_match_data(&spi->dev);
  149. if (!flash->caps)
  150. flash->caps = &mchp23k256_caps;
  151. mtd_set_of_node(&flash->mtd, spi->dev.of_node);
  152. flash->mtd.dev.parent = &spi->dev;
  153. flash->mtd.type = MTD_RAM;
  154. flash->mtd.flags = MTD_CAP_RAM;
  155. flash->mtd.writesize = 1;
  156. flash->mtd.size = flash->caps->size;
  157. flash->mtd._read = mchp23k256_read;
  158. flash->mtd._write = mchp23k256_write;
  159. err = mtd_device_register(&flash->mtd, data ? data->parts : NULL,
  160. data ? data->nr_parts : 0);
  161. if (err)
  162. return err;
  163. return 0;
  164. }
  165. static int mchp23k256_remove(struct spi_device *spi)
  166. {
  167. struct mchp23k256_flash *flash = spi_get_drvdata(spi);
  168. return mtd_device_unregister(&flash->mtd);
  169. }
  170. static const struct of_device_id mchp23k256_of_table[] = {
  171. {
  172. .compatible = "microchip,mchp23k256",
  173. .data = &mchp23k256_caps,
  174. },
  175. {
  176. .compatible = "microchip,mchp23lcv1024",
  177. .data = &mchp23lcv1024_caps,
  178. },
  179. {}
  180. };
  181. MODULE_DEVICE_TABLE(of, mchp23k256_of_table);
  182. static struct spi_driver mchp23k256_driver = {
  183. .driver = {
  184. .name = "mchp23k256",
  185. .of_match_table = of_match_ptr(mchp23k256_of_table),
  186. },
  187. .probe = mchp23k256_probe,
  188. .remove = mchp23k256_remove,
  189. };
  190. module_spi_driver(mchp23k256_driver);
  191. MODULE_DESCRIPTION("MTD SPI driver for MCHP23K256 RAM chips");
  192. MODULE_AUTHOR("Andrew Lunn <andre@lunn.ch>");
  193. MODULE_LICENSE("GPL v2");
  194. MODULE_ALIAS("spi:mchp23k256");