bcm47xxsflash.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/slab.h>
  4. #include <linux/delay.h>
  5. #include <linux/mtd/mtd.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/bcma/bcma.h>
  8. #include "bcm47xxsflash.h"
  9. MODULE_LICENSE("GPL");
  10. MODULE_DESCRIPTION("Serial flash driver for BCMA bus");
  11. static const char * const probes[] = { "bcm47xxpart", NULL };
  12. /**************************************************
  13. * Various helpers
  14. **************************************************/
  15. static void bcm47xxsflash_cmd(struct bcm47xxsflash *b47s, u32 opcode)
  16. {
  17. int i;
  18. b47s->cc_write(b47s, BCMA_CC_FLASHCTL, BCMA_CC_FLASHCTL_START | opcode);
  19. for (i = 0; i < 1000; i++) {
  20. if (!(b47s->cc_read(b47s, BCMA_CC_FLASHCTL) &
  21. BCMA_CC_FLASHCTL_BUSY))
  22. return;
  23. cpu_relax();
  24. }
  25. pr_err("Control command failed (timeout)!\n");
  26. }
  27. static int bcm47xxsflash_poll(struct bcm47xxsflash *b47s, int timeout)
  28. {
  29. unsigned long deadline = jiffies + timeout;
  30. do {
  31. switch (b47s->type) {
  32. case BCM47XXSFLASH_TYPE_ST:
  33. bcm47xxsflash_cmd(b47s, OPCODE_ST_RDSR);
  34. if (!(b47s->cc_read(b47s, BCMA_CC_FLASHDATA) &
  35. SR_ST_WIP))
  36. return 0;
  37. break;
  38. case BCM47XXSFLASH_TYPE_ATMEL:
  39. bcm47xxsflash_cmd(b47s, OPCODE_AT_STATUS);
  40. if (b47s->cc_read(b47s, BCMA_CC_FLASHDATA) &
  41. SR_AT_READY)
  42. return 0;
  43. break;
  44. }
  45. cpu_relax();
  46. udelay(1);
  47. } while (!time_after_eq(jiffies, deadline));
  48. pr_err("Timeout waiting for flash to be ready!\n");
  49. return -EBUSY;
  50. }
  51. /**************************************************
  52. * MTD ops
  53. **************************************************/
  54. static int bcm47xxsflash_read(struct mtd_info *mtd, loff_t from, size_t len,
  55. size_t *retlen, u_char *buf)
  56. {
  57. struct bcm47xxsflash *b47s = mtd->priv;
  58. /* Check address range */
  59. if ((from + len) > mtd->size)
  60. return -EINVAL;
  61. memcpy_fromio(buf, (void __iomem *)KSEG0ADDR(b47s->window + from),
  62. len);
  63. *retlen = len;
  64. return len;
  65. }
  66. static void bcm47xxsflash_fill_mtd(struct bcm47xxsflash *b47s)
  67. {
  68. struct mtd_info *mtd = &b47s->mtd;
  69. mtd->priv = b47s;
  70. mtd->name = "bcm47xxsflash";
  71. mtd->owner = THIS_MODULE;
  72. mtd->type = MTD_ROM;
  73. mtd->size = b47s->size;
  74. mtd->_read = bcm47xxsflash_read;
  75. /* TODO: implement writing support and verify/change following code */
  76. mtd->flags = MTD_CAP_ROM;
  77. mtd->writebufsize = mtd->writesize = 1;
  78. }
  79. /**************************************************
  80. * BCMA
  81. **************************************************/
  82. static int bcm47xxsflash_bcma_cc_read(struct bcm47xxsflash *b47s, u16 offset)
  83. {
  84. return bcma_cc_read32(b47s->bcma_cc, offset);
  85. }
  86. static void bcm47xxsflash_bcma_cc_write(struct bcm47xxsflash *b47s, u16 offset,
  87. u32 value)
  88. {
  89. bcma_cc_write32(b47s->bcma_cc, offset, value);
  90. }
  91. static int bcm47xxsflash_bcma_probe(struct platform_device *pdev)
  92. {
  93. struct bcma_sflash *sflash = dev_get_platdata(&pdev->dev);
  94. struct bcm47xxsflash *b47s;
  95. int err;
  96. b47s = devm_kzalloc(&pdev->dev, sizeof(*b47s), GFP_KERNEL);
  97. if (!b47s)
  98. return -ENOMEM;
  99. sflash->priv = b47s;
  100. b47s->bcma_cc = container_of(sflash, struct bcma_drv_cc, sflash);
  101. b47s->cc_read = bcm47xxsflash_bcma_cc_read;
  102. b47s->cc_write = bcm47xxsflash_bcma_cc_write;
  103. switch (b47s->bcma_cc->capabilities & BCMA_CC_CAP_FLASHT) {
  104. case BCMA_CC_FLASHT_STSER:
  105. b47s->type = BCM47XXSFLASH_TYPE_ST;
  106. break;
  107. case BCMA_CC_FLASHT_ATSER:
  108. b47s->type = BCM47XXSFLASH_TYPE_ATMEL;
  109. break;
  110. }
  111. b47s->window = sflash->window;
  112. b47s->blocksize = sflash->blocksize;
  113. b47s->numblocks = sflash->numblocks;
  114. b47s->size = sflash->size;
  115. bcm47xxsflash_fill_mtd(b47s);
  116. err = mtd_device_parse_register(&b47s->mtd, probes, NULL, NULL, 0);
  117. if (err) {
  118. pr_err("Failed to register MTD device: %d\n", err);
  119. return err;
  120. }
  121. if (bcm47xxsflash_poll(b47s, HZ / 10))
  122. pr_warn("Serial flash busy\n");
  123. return 0;
  124. }
  125. static int bcm47xxsflash_bcma_remove(struct platform_device *pdev)
  126. {
  127. struct bcma_sflash *sflash = dev_get_platdata(&pdev->dev);
  128. struct bcm47xxsflash *b47s = sflash->priv;
  129. mtd_device_unregister(&b47s->mtd);
  130. return 0;
  131. }
  132. static struct platform_driver bcma_sflash_driver = {
  133. .probe = bcm47xxsflash_bcma_probe,
  134. .remove = bcm47xxsflash_bcma_remove,
  135. .driver = {
  136. .name = "bcma_sflash",
  137. .owner = THIS_MODULE,
  138. },
  139. };
  140. /**************************************************
  141. * Init
  142. **************************************************/
  143. static int __init bcm47xxsflash_init(void)
  144. {
  145. int err;
  146. err = platform_driver_register(&bcma_sflash_driver);
  147. if (err)
  148. pr_err("Failed to register BCMA serial flash driver: %d\n",
  149. err);
  150. return err;
  151. }
  152. static void __exit bcm47xxsflash_exit(void)
  153. {
  154. platform_driver_unregister(&bcma_sflash_driver);
  155. }
  156. module_init(bcm47xxsflash_init);
  157. module_exit(bcm47xxsflash_exit);