bcm53xxspiflash.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include <linux/module.h>
  2. #include <linux/delay.h>
  3. #include <linux/spi/spi.h>
  4. #include <linux/mtd/spi-nor.h>
  5. #include <linux/mtd/mtd.h>
  6. #include <linux/mtd/cfi.h>
  7. #include <linux/mtd/partitions.h>
  8. static const char * const probes[] = { "ofpart", "bcm47xxpart", NULL };
  9. struct bcm53xxsf {
  10. struct spi_device *spi;
  11. struct spi_nor nor;
  12. };
  13. /**************************************************
  14. * spi-nor API
  15. **************************************************/
  16. static int bcm53xxspiflash_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf,
  17. int len)
  18. {
  19. struct bcm53xxsf *b53sf = nor->priv;
  20. return spi_write_then_read(b53sf->spi, &opcode, 1, buf, len);
  21. }
  22. static int bcm53xxspiflash_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf,
  23. int len)
  24. {
  25. struct bcm53xxsf *b53sf = nor->priv;
  26. u8 *cmd = kzalloc(len + 1, GFP_KERNEL);
  27. int err;
  28. if (!cmd)
  29. return -ENOMEM;
  30. cmd[0] = opcode;
  31. memcpy(&cmd[1], buf, len);
  32. err = spi_write(b53sf->spi, cmd, len + 1);
  33. kfree(cmd);
  34. return err;
  35. }
  36. static int bcm53xxspiflash_read(struct spi_nor *nor, loff_t from, size_t len,
  37. size_t *retlen, u_char *buf)
  38. {
  39. struct bcm53xxsf *b53sf = nor->priv;
  40. struct spi_message m;
  41. struct spi_transfer t[2] = { { 0 }, { 0 } };
  42. unsigned char cmd[5];
  43. int cmd_len = 0;
  44. int err;
  45. spi_message_init(&m);
  46. cmd[cmd_len++] = SPINOR_OP_READ;
  47. if (nor->mtd.size > 0x1000000)
  48. cmd[cmd_len++] = (from & 0xFF000000) >> 24;
  49. cmd[cmd_len++] = (from & 0x00FF0000) >> 16;
  50. cmd[cmd_len++] = (from & 0x0000FF00) >> 8;
  51. cmd[cmd_len++] = (from & 0x000000FF) >> 0;
  52. t[0].tx_buf = cmd;
  53. t[0].len = cmd_len;
  54. spi_message_add_tail(&t[0], &m);
  55. t[1].rx_buf = buf;
  56. t[1].len = len;
  57. spi_message_add_tail(&t[1], &m);
  58. err = spi_sync(b53sf->spi, &m);
  59. if (err)
  60. return err;
  61. if (retlen && m.actual_length > cmd_len)
  62. *retlen = m.actual_length - cmd_len;
  63. return 0;
  64. }
  65. static void bcm53xxspiflash_write(struct spi_nor *nor, loff_t to, size_t len,
  66. size_t *retlen, const u_char *buf)
  67. {
  68. struct bcm53xxsf *b53sf = nor->priv;
  69. struct spi_message m;
  70. struct spi_transfer t = { 0 };
  71. u8 *cmd = kzalloc(len + 5, GFP_KERNEL);
  72. int cmd_len = 0;
  73. int err;
  74. if (!cmd)
  75. return;
  76. spi_message_init(&m);
  77. cmd[cmd_len++] = nor->program_opcode;
  78. if (nor->mtd.size > 0x1000000)
  79. cmd[cmd_len++] = (to & 0xFF000000) >> 24;
  80. cmd[cmd_len++] = (to & 0x00FF0000) >> 16;
  81. cmd[cmd_len++] = (to & 0x0000FF00) >> 8;
  82. cmd[cmd_len++] = (to & 0x000000FF) >> 0;
  83. memcpy(&cmd[cmd_len], buf, len);
  84. t.tx_buf = cmd;
  85. t.len = cmd_len + len;
  86. spi_message_add_tail(&t, &m);
  87. err = spi_sync(b53sf->spi, &m);
  88. if (err)
  89. goto out;
  90. if (retlen && m.actual_length > cmd_len)
  91. *retlen += m.actual_length - cmd_len;
  92. out:
  93. kfree(cmd);
  94. }
  95. static int bcm53xxspiflash_erase(struct spi_nor *nor, loff_t offs)
  96. {
  97. struct bcm53xxsf *b53sf = nor->priv;
  98. unsigned char cmd[5];
  99. int i;
  100. i = 0;
  101. cmd[i++] = nor->erase_opcode;
  102. if (nor->mtd.size > 0x1000000)
  103. cmd[i++] = (offs & 0xFF000000) >> 24;
  104. cmd[i++] = ((offs & 0x00FF0000) >> 16);
  105. cmd[i++] = ((offs & 0x0000FF00) >> 8);
  106. cmd[i++] = ((offs & 0x000000FF) >> 0);
  107. return spi_write(b53sf->spi, cmd, i);
  108. }
  109. static const char *bcm53xxspiflash_chip_name(struct spi_nor *nor)
  110. {
  111. struct bcm53xxsf *b53sf = nor->priv;
  112. struct device *dev = &b53sf->spi->dev;
  113. unsigned char cmd[4];
  114. unsigned char resp[2];
  115. int err;
  116. /* SST and Winbond/NexFlash specific command */
  117. cmd[0] = 0x90; /* Read Manufacturer / Device ID */
  118. cmd[1] = 0;
  119. cmd[2] = 0;
  120. cmd[3] = 0;
  121. err = spi_write_then_read(b53sf->spi, cmd, 4, resp, 2);
  122. if (err < 0) {
  123. dev_err(dev, "error reading SPI flash id\n");
  124. return ERR_PTR(-EBUSY);
  125. }
  126. switch (resp[0]) {
  127. case 0xef: /* Winbond/NexFlash */
  128. switch (resp[1]) {
  129. case 0x17:
  130. return "w25q128";
  131. }
  132. dev_err(dev, "Unknown Winbond/NexFlash flash: %02X %02X\n",
  133. resp[0], resp[1]);
  134. return NULL;
  135. }
  136. /* TODO: Try more ID commands */
  137. return NULL;
  138. }
  139. /**************************************************
  140. * SPI driver
  141. **************************************************/
  142. static int bcm53xxspiflash_probe(struct spi_device *spi)
  143. {
  144. struct mtd_part_parser_data parser_data = {};
  145. struct bcm53xxsf *b53sf;
  146. struct spi_nor *nor;
  147. int err;
  148. b53sf = devm_kzalloc(&spi->dev, sizeof(*b53sf), GFP_KERNEL);
  149. if (!b53sf)
  150. return -ENOMEM;
  151. spi_set_drvdata(spi, b53sf);
  152. nor = &b53sf->nor;
  153. b53sf->spi = spi;
  154. nor->dev = &spi->dev;
  155. nor->read_reg = bcm53xxspiflash_read_reg;
  156. nor->write_reg = bcm53xxspiflash_write_reg;
  157. nor->read = bcm53xxspiflash_read;
  158. nor->write = bcm53xxspiflash_write;
  159. nor->erase = bcm53xxspiflash_erase;
  160. nor->priv = b53sf;
  161. err = spi_nor_scan(&b53sf->nor, bcm53xxspiflash_chip_name(nor),
  162. SPI_NOR_NORMAL);
  163. if (err)
  164. return err;
  165. parser_data.of_node = spi->master->dev.parent->of_node;
  166. err = mtd_device_parse_register(&nor->mtd, probes, &parser_data,
  167. NULL, 0);
  168. if (err)
  169. return err;
  170. return 0;
  171. }
  172. static int bcm53xxspiflash_remove(struct spi_device *spi)
  173. {
  174. return 0;
  175. }
  176. static struct spi_driver bcm53xxspiflash_driver = {
  177. .driver = {
  178. .name = "bcm53xxspiflash",
  179. .owner = THIS_MODULE,
  180. },
  181. .probe = bcm53xxspiflash_probe,
  182. .remove = bcm53xxspiflash_remove,
  183. };
  184. module_spi_driver(bcm53xxspiflash_driver);