mchp23k256.c 5.5 KB

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