mchp23k256.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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, cmd_len;
  63. spi_message_init(&message);
  64. cmd_len = mchp23k256_cmdsz(flash);
  65. command[0] = MCHP23K256_CMD_WRITE;
  66. mchp23k256_addr2cmd(flash, to, command);
  67. transfer[0].tx_buf = command;
  68. transfer[0].len = cmd_len;
  69. spi_message_add_tail(&transfer[0], &message);
  70. transfer[1].tx_buf = buf;
  71. transfer[1].len = len;
  72. spi_message_add_tail(&transfer[1], &message);
  73. mutex_lock(&flash->lock);
  74. ret = spi_sync(flash->spi, &message);
  75. mutex_unlock(&flash->lock);
  76. if (ret)
  77. return ret;
  78. if (retlen && message.actual_length > cmd_len)
  79. *retlen += message.actual_length - cmd_len;
  80. return 0;
  81. }
  82. static int mchp23k256_read(struct mtd_info *mtd, loff_t from, size_t len,
  83. size_t *retlen, unsigned char *buf)
  84. {
  85. struct mchp23k256_flash *flash = to_mchp23k256_flash(mtd);
  86. struct spi_transfer transfer[2] = {};
  87. struct spi_message message;
  88. unsigned char command[MAX_CMD_SIZE];
  89. int ret, cmd_len;
  90. spi_message_init(&message);
  91. cmd_len = mchp23k256_cmdsz(flash);
  92. memset(&transfer, 0, sizeof(transfer));
  93. command[0] = MCHP23K256_CMD_READ;
  94. mchp23k256_addr2cmd(flash, from, command);
  95. transfer[0].tx_buf = command;
  96. transfer[0].len = cmd_len;
  97. spi_message_add_tail(&transfer[0], &message);
  98. transfer[1].rx_buf = buf;
  99. transfer[1].len = len;
  100. spi_message_add_tail(&transfer[1], &message);
  101. mutex_lock(&flash->lock);
  102. ret = spi_sync(flash->spi, &message);
  103. mutex_unlock(&flash->lock);
  104. if (ret)
  105. return ret;
  106. if (retlen && message.actual_length > cmd_len)
  107. *retlen += message.actual_length - cmd_len;
  108. return 0;
  109. }
  110. /*
  111. * Set the device into sequential mode. This allows read/writes to the
  112. * entire SRAM in a single operation
  113. */
  114. static int mchp23k256_set_mode(struct spi_device *spi)
  115. {
  116. struct spi_transfer transfer = {};
  117. struct spi_message message;
  118. unsigned char command[2];
  119. spi_message_init(&message);
  120. command[0] = MCHP23K256_CMD_WRITE_STATUS;
  121. command[1] = MCHP23K256_MODE_SEQ;
  122. transfer.tx_buf = command;
  123. transfer.len = sizeof(command);
  124. spi_message_add_tail(&transfer, &message);
  125. return spi_sync(spi, &message);
  126. }
  127. static const struct mchp23_caps mchp23k256_caps = {
  128. .size = SZ_32K,
  129. .addr_width = 2,
  130. };
  131. static const struct mchp23_caps mchp23lcv1024_caps = {
  132. .size = SZ_128K,
  133. .addr_width = 3,
  134. };
  135. static int mchp23k256_probe(struct spi_device *spi)
  136. {
  137. struct mchp23k256_flash *flash;
  138. struct flash_platform_data *data;
  139. int err;
  140. flash = devm_kzalloc(&spi->dev, sizeof(*flash), GFP_KERNEL);
  141. if (!flash)
  142. return -ENOMEM;
  143. flash->spi = spi;
  144. mutex_init(&flash->lock);
  145. spi_set_drvdata(spi, flash);
  146. err = mchp23k256_set_mode(spi);
  147. if (err)
  148. return err;
  149. data = dev_get_platdata(&spi->dev);
  150. flash->caps = of_device_get_match_data(&spi->dev);
  151. if (!flash->caps)
  152. flash->caps = &mchp23k256_caps;
  153. mtd_set_of_node(&flash->mtd, spi->dev.of_node);
  154. flash->mtd.dev.parent = &spi->dev;
  155. flash->mtd.type = MTD_RAM;
  156. flash->mtd.flags = MTD_CAP_RAM;
  157. flash->mtd.writesize = 1;
  158. flash->mtd.size = flash->caps->size;
  159. flash->mtd._read = mchp23k256_read;
  160. flash->mtd._write = mchp23k256_write;
  161. err = mtd_device_register(&flash->mtd, data ? data->parts : NULL,
  162. data ? data->nr_parts : 0);
  163. if (err)
  164. return err;
  165. return 0;
  166. }
  167. static int mchp23k256_remove(struct spi_device *spi)
  168. {
  169. struct mchp23k256_flash *flash = spi_get_drvdata(spi);
  170. return mtd_device_unregister(&flash->mtd);
  171. }
  172. static const struct of_device_id mchp23k256_of_table[] = {
  173. {
  174. .compatible = "microchip,mchp23k256",
  175. .data = &mchp23k256_caps,
  176. },
  177. {
  178. .compatible = "microchip,mchp23lcv1024",
  179. .data = &mchp23lcv1024_caps,
  180. },
  181. {}
  182. };
  183. MODULE_DEVICE_TABLE(of, mchp23k256_of_table);
  184. static struct spi_driver mchp23k256_driver = {
  185. .driver = {
  186. .name = "mchp23k256",
  187. .of_match_table = of_match_ptr(mchp23k256_of_table),
  188. },
  189. .probe = mchp23k256_probe,
  190. .remove = mchp23k256_remove,
  191. };
  192. module_spi_driver(mchp23k256_driver);
  193. MODULE_DESCRIPTION("MTD SPI driver for MCHP23K256 RAM chips");
  194. MODULE_AUTHOR("Andrew Lunn <andre@lunn.ch>");
  195. MODULE_LICENSE("GPL v2");
  196. MODULE_ALIAS("spi:mchp23k256");