bcm47xxsflash.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/slab.h>
  4. #include <linux/delay.h>
  5. #include <linux/ioport.h>
  6. #include <linux/mtd/mtd.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/bcma/bcma.h>
  9. #include "bcm47xxsflash.h"
  10. MODULE_LICENSE("GPL");
  11. MODULE_DESCRIPTION("Serial flash driver for BCMA bus");
  12. static const char * const probes[] = { "bcm47xxpart", NULL };
  13. /**************************************************
  14. * Various helpers
  15. **************************************************/
  16. static void bcm47xxsflash_cmd(struct bcm47xxsflash *b47s, u32 opcode)
  17. {
  18. int i;
  19. b47s->cc_write(b47s, BCMA_CC_FLASHCTL, BCMA_CC_FLASHCTL_START | opcode);
  20. for (i = 0; i < 1000; i++) {
  21. if (!(b47s->cc_read(b47s, BCMA_CC_FLASHCTL) &
  22. BCMA_CC_FLASHCTL_BUSY))
  23. return;
  24. cpu_relax();
  25. }
  26. pr_err("Control command failed (timeout)!\n");
  27. }
  28. static int bcm47xxsflash_poll(struct bcm47xxsflash *b47s, int timeout)
  29. {
  30. unsigned long deadline = jiffies + timeout;
  31. do {
  32. switch (b47s->type) {
  33. case BCM47XXSFLASH_TYPE_ST:
  34. bcm47xxsflash_cmd(b47s, OPCODE_ST_RDSR);
  35. if (!(b47s->cc_read(b47s, BCMA_CC_FLASHDATA) &
  36. SR_ST_WIP))
  37. return 0;
  38. break;
  39. case BCM47XXSFLASH_TYPE_ATMEL:
  40. bcm47xxsflash_cmd(b47s, OPCODE_AT_STATUS);
  41. if (b47s->cc_read(b47s, BCMA_CC_FLASHDATA) &
  42. SR_AT_READY)
  43. return 0;
  44. break;
  45. }
  46. cpu_relax();
  47. udelay(1);
  48. } while (!time_after_eq(jiffies, deadline));
  49. pr_err("Timeout waiting for flash to be ready!\n");
  50. return -EBUSY;
  51. }
  52. /**************************************************
  53. * MTD ops
  54. **************************************************/
  55. static int bcm47xxsflash_erase(struct mtd_info *mtd, struct erase_info *erase)
  56. {
  57. struct bcm47xxsflash *b47s = mtd->priv;
  58. int err;
  59. switch (b47s->type) {
  60. case BCM47XXSFLASH_TYPE_ST:
  61. bcm47xxsflash_cmd(b47s, OPCODE_ST_WREN);
  62. b47s->cc_write(b47s, BCMA_CC_FLASHADDR, erase->addr);
  63. /* Newer flashes have "sub-sectors" which can be erased
  64. * independently with a new command: ST_SSE. The ST_SE command
  65. * erases 64KB just as before.
  66. */
  67. if (b47s->blocksize < (64 * 1024))
  68. bcm47xxsflash_cmd(b47s, OPCODE_ST_SSE);
  69. else
  70. bcm47xxsflash_cmd(b47s, OPCODE_ST_SE);
  71. break;
  72. case BCM47XXSFLASH_TYPE_ATMEL:
  73. b47s->cc_write(b47s, BCMA_CC_FLASHADDR, erase->addr << 1);
  74. bcm47xxsflash_cmd(b47s, OPCODE_AT_PAGE_ERASE);
  75. break;
  76. }
  77. err = bcm47xxsflash_poll(b47s, HZ);
  78. if (err)
  79. erase->state = MTD_ERASE_FAILED;
  80. else
  81. erase->state = MTD_ERASE_DONE;
  82. if (erase->callback)
  83. erase->callback(erase);
  84. return err;
  85. }
  86. static int bcm47xxsflash_read(struct mtd_info *mtd, loff_t from, size_t len,
  87. size_t *retlen, u_char *buf)
  88. {
  89. struct bcm47xxsflash *b47s = mtd->priv;
  90. /* Check address range */
  91. if ((from + len) > mtd->size)
  92. return -EINVAL;
  93. memcpy_fromio(buf, b47s->window + from, len);
  94. *retlen = len;
  95. return len;
  96. }
  97. static int bcm47xxsflash_write_st(struct mtd_info *mtd, u32 offset, size_t len,
  98. const u_char *buf)
  99. {
  100. struct bcm47xxsflash *b47s = mtd->priv;
  101. int written = 0;
  102. /* Enable writes */
  103. bcm47xxsflash_cmd(b47s, OPCODE_ST_WREN);
  104. /* Write first byte */
  105. b47s->cc_write(b47s, BCMA_CC_FLASHADDR, offset);
  106. b47s->cc_write(b47s, BCMA_CC_FLASHDATA, *buf++);
  107. /* Program page */
  108. if (b47s->bcma_cc->core->id.rev < 20) {
  109. bcm47xxsflash_cmd(b47s, OPCODE_ST_PP);
  110. return 1; /* 1B written */
  111. }
  112. /* Program page and set CSA (on newer chips we can continue writing) */
  113. bcm47xxsflash_cmd(b47s, OPCODE_ST_CSA | OPCODE_ST_PP);
  114. offset++;
  115. len--;
  116. written++;
  117. while (len > 0) {
  118. /* Page boundary, another function call is needed */
  119. if ((offset & 0xFF) == 0)
  120. break;
  121. bcm47xxsflash_cmd(b47s, OPCODE_ST_CSA | *buf++);
  122. offset++;
  123. len--;
  124. written++;
  125. }
  126. /* All done, drop CSA & poll */
  127. b47s->cc_write(b47s, BCMA_CC_FLASHCTL, 0);
  128. udelay(1);
  129. if (bcm47xxsflash_poll(b47s, HZ / 10))
  130. pr_err("Flash rejected dropping CSA\n");
  131. return written;
  132. }
  133. static int bcm47xxsflash_write_at(struct mtd_info *mtd, u32 offset, size_t len,
  134. const u_char *buf)
  135. {
  136. struct bcm47xxsflash *b47s = mtd->priv;
  137. u32 mask = b47s->blocksize - 1;
  138. u32 page = (offset & ~mask) << 1;
  139. u32 byte = offset & mask;
  140. int written = 0;
  141. /* If we don't overwrite whole page, read it to the buffer first */
  142. if (byte || (len < b47s->blocksize)) {
  143. int err;
  144. b47s->cc_write(b47s, BCMA_CC_FLASHADDR, page);
  145. bcm47xxsflash_cmd(b47s, OPCODE_AT_BUF1_LOAD);
  146. /* 250 us for AT45DB321B */
  147. err = bcm47xxsflash_poll(b47s, HZ / 1000);
  148. if (err) {
  149. pr_err("Timeout reading page 0x%X info buffer\n", page);
  150. return err;
  151. }
  152. }
  153. /* Change buffer content with our data */
  154. while (len > 0) {
  155. /* Page boundary, another function call is needed */
  156. if (byte == b47s->blocksize)
  157. break;
  158. b47s->cc_write(b47s, BCMA_CC_FLASHADDR, byte++);
  159. b47s->cc_write(b47s, BCMA_CC_FLASHDATA, *buf++);
  160. bcm47xxsflash_cmd(b47s, OPCODE_AT_BUF1_WRITE);
  161. len--;
  162. written++;
  163. }
  164. /* Program page with the buffer content */
  165. b47s->cc_write(b47s, BCMA_CC_FLASHADDR, page);
  166. bcm47xxsflash_cmd(b47s, OPCODE_AT_BUF1_PROGRAM);
  167. return written;
  168. }
  169. static int bcm47xxsflash_write(struct mtd_info *mtd, loff_t to, size_t len,
  170. size_t *retlen, const u_char *buf)
  171. {
  172. struct bcm47xxsflash *b47s = mtd->priv;
  173. int written;
  174. /* Writing functions can return without writing all passed data, for
  175. * example when the hardware is too old or when we git page boundary.
  176. */
  177. while (len > 0) {
  178. switch (b47s->type) {
  179. case BCM47XXSFLASH_TYPE_ST:
  180. written = bcm47xxsflash_write_st(mtd, to, len, buf);
  181. break;
  182. case BCM47XXSFLASH_TYPE_ATMEL:
  183. written = bcm47xxsflash_write_at(mtd, to, len, buf);
  184. break;
  185. default:
  186. BUG_ON(1);
  187. }
  188. if (written < 0) {
  189. pr_err("Error writing at offset 0x%llX\n", to);
  190. return written;
  191. }
  192. to += (loff_t)written;
  193. len -= written;
  194. *retlen += written;
  195. buf += written;
  196. }
  197. return 0;
  198. }
  199. static void bcm47xxsflash_fill_mtd(struct bcm47xxsflash *b47s,
  200. struct device *dev)
  201. {
  202. struct mtd_info *mtd = &b47s->mtd;
  203. mtd->priv = b47s;
  204. mtd->dev.parent = dev;
  205. mtd->name = "bcm47xxsflash";
  206. mtd->type = MTD_NORFLASH;
  207. mtd->flags = MTD_CAP_NORFLASH;
  208. mtd->size = b47s->size;
  209. mtd->erasesize = b47s->blocksize;
  210. mtd->writesize = 1;
  211. mtd->writebufsize = 1;
  212. mtd->_erase = bcm47xxsflash_erase;
  213. mtd->_read = bcm47xxsflash_read;
  214. mtd->_write = bcm47xxsflash_write;
  215. }
  216. /**************************************************
  217. * BCMA
  218. **************************************************/
  219. static int bcm47xxsflash_bcma_cc_read(struct bcm47xxsflash *b47s, u16 offset)
  220. {
  221. return bcma_cc_read32(b47s->bcma_cc, offset);
  222. }
  223. static void bcm47xxsflash_bcma_cc_write(struct bcm47xxsflash *b47s, u16 offset,
  224. u32 value)
  225. {
  226. bcma_cc_write32(b47s->bcma_cc, offset, value);
  227. }
  228. static int bcm47xxsflash_bcma_probe(struct platform_device *pdev)
  229. {
  230. struct device *dev = &pdev->dev;
  231. struct bcma_sflash *sflash = dev_get_platdata(dev);
  232. struct bcm47xxsflash *b47s;
  233. struct resource *res;
  234. int err;
  235. b47s = devm_kzalloc(dev, sizeof(*b47s), GFP_KERNEL);
  236. if (!b47s)
  237. return -ENOMEM;
  238. sflash->priv = b47s;
  239. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  240. if (!res) {
  241. dev_err(dev, "invalid resource\n");
  242. return -EINVAL;
  243. }
  244. if (!devm_request_mem_region(dev, res->start, resource_size(res),
  245. res->name)) {
  246. dev_err(dev, "can't request region for resource %pR\n", res);
  247. return -EBUSY;
  248. }
  249. b47s->window = ioremap_cache(res->start, resource_size(res));
  250. if (!b47s->window) {
  251. dev_err(dev, "ioremap failed for resource %pR\n", res);
  252. return -ENOMEM;
  253. }
  254. b47s->bcma_cc = container_of(sflash, struct bcma_drv_cc, sflash);
  255. b47s->cc_read = bcm47xxsflash_bcma_cc_read;
  256. b47s->cc_write = bcm47xxsflash_bcma_cc_write;
  257. switch (b47s->bcma_cc->capabilities & BCMA_CC_CAP_FLASHT) {
  258. case BCMA_CC_FLASHT_STSER:
  259. b47s->type = BCM47XXSFLASH_TYPE_ST;
  260. break;
  261. case BCMA_CC_FLASHT_ATSER:
  262. b47s->type = BCM47XXSFLASH_TYPE_ATMEL;
  263. break;
  264. }
  265. b47s->blocksize = sflash->blocksize;
  266. b47s->numblocks = sflash->numblocks;
  267. b47s->size = sflash->size;
  268. bcm47xxsflash_fill_mtd(b47s, &pdev->dev);
  269. err = mtd_device_parse_register(&b47s->mtd, probes, NULL, NULL, 0);
  270. if (err) {
  271. pr_err("Failed to register MTD device: %d\n", err);
  272. iounmap(b47s->window);
  273. return err;
  274. }
  275. if (bcm47xxsflash_poll(b47s, HZ / 10))
  276. pr_warn("Serial flash busy\n");
  277. return 0;
  278. }
  279. static int bcm47xxsflash_bcma_remove(struct platform_device *pdev)
  280. {
  281. struct bcma_sflash *sflash = dev_get_platdata(&pdev->dev);
  282. struct bcm47xxsflash *b47s = sflash->priv;
  283. mtd_device_unregister(&b47s->mtd);
  284. iounmap(b47s->window);
  285. return 0;
  286. }
  287. static struct platform_driver bcma_sflash_driver = {
  288. .probe = bcm47xxsflash_bcma_probe,
  289. .remove = bcm47xxsflash_bcma_remove,
  290. .driver = {
  291. .name = "bcma_sflash",
  292. },
  293. };
  294. /**************************************************
  295. * Init
  296. **************************************************/
  297. module_platform_driver(bcma_sflash_driver);