mchp23k256.c 5.8 KB

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