bcm47xxsflash.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. size_t orig_len = len;
  91. /* Check address range */
  92. if ((from + len) > mtd->size)
  93. return -EINVAL;
  94. /* Read as much as possible using fast MMIO window */
  95. if (from < BCM47XXSFLASH_WINDOW_SZ) {
  96. size_t memcpy_len;
  97. memcpy_len = min(len, (size_t)(BCM47XXSFLASH_WINDOW_SZ - from));
  98. memcpy_fromio(buf, b47s->window + from, memcpy_len);
  99. from += memcpy_len;
  100. len -= memcpy_len;
  101. buf += memcpy_len;
  102. }
  103. /* Use indirect access for content out of the window */
  104. for (; len; len--) {
  105. b47s->cc_write(b47s, BCMA_CC_FLASHADDR, from++);
  106. bcm47xxsflash_cmd(b47s, OPCODE_ST_READ4B);
  107. *buf++ = b47s->cc_read(b47s, BCMA_CC_FLASHDATA);
  108. }
  109. *retlen = orig_len;
  110. return orig_len;
  111. }
  112. static int bcm47xxsflash_write_st(struct mtd_info *mtd, u32 offset, size_t len,
  113. const u_char *buf)
  114. {
  115. struct bcm47xxsflash *b47s = mtd->priv;
  116. int written = 0;
  117. /* Enable writes */
  118. bcm47xxsflash_cmd(b47s, OPCODE_ST_WREN);
  119. /* Write first byte */
  120. b47s->cc_write(b47s, BCMA_CC_FLASHADDR, offset);
  121. b47s->cc_write(b47s, BCMA_CC_FLASHDATA, *buf++);
  122. /* Program page */
  123. if (b47s->bcma_cc->core->id.rev < 20) {
  124. bcm47xxsflash_cmd(b47s, OPCODE_ST_PP);
  125. return 1; /* 1B written */
  126. }
  127. /* Program page and set CSA (on newer chips we can continue writing) */
  128. bcm47xxsflash_cmd(b47s, OPCODE_ST_CSA | OPCODE_ST_PP);
  129. offset++;
  130. len--;
  131. written++;
  132. while (len > 0) {
  133. /* Page boundary, another function call is needed */
  134. if ((offset & 0xFF) == 0)
  135. break;
  136. bcm47xxsflash_cmd(b47s, OPCODE_ST_CSA | *buf++);
  137. offset++;
  138. len--;
  139. written++;
  140. }
  141. /* All done, drop CSA & poll */
  142. b47s->cc_write(b47s, BCMA_CC_FLASHCTL, 0);
  143. udelay(1);
  144. if (bcm47xxsflash_poll(b47s, HZ / 10))
  145. pr_err("Flash rejected dropping CSA\n");
  146. return written;
  147. }
  148. static int bcm47xxsflash_write_at(struct mtd_info *mtd, u32 offset, size_t len,
  149. const u_char *buf)
  150. {
  151. struct bcm47xxsflash *b47s = mtd->priv;
  152. u32 mask = b47s->blocksize - 1;
  153. u32 page = (offset & ~mask) << 1;
  154. u32 byte = offset & mask;
  155. int written = 0;
  156. /* If we don't overwrite whole page, read it to the buffer first */
  157. if (byte || (len < b47s->blocksize)) {
  158. int err;
  159. b47s->cc_write(b47s, BCMA_CC_FLASHADDR, page);
  160. bcm47xxsflash_cmd(b47s, OPCODE_AT_BUF1_LOAD);
  161. /* 250 us for AT45DB321B */
  162. err = bcm47xxsflash_poll(b47s, HZ / 1000);
  163. if (err) {
  164. pr_err("Timeout reading page 0x%X info buffer\n", page);
  165. return err;
  166. }
  167. }
  168. /* Change buffer content with our data */
  169. while (len > 0) {
  170. /* Page boundary, another function call is needed */
  171. if (byte == b47s->blocksize)
  172. break;
  173. b47s->cc_write(b47s, BCMA_CC_FLASHADDR, byte++);
  174. b47s->cc_write(b47s, BCMA_CC_FLASHDATA, *buf++);
  175. bcm47xxsflash_cmd(b47s, OPCODE_AT_BUF1_WRITE);
  176. len--;
  177. written++;
  178. }
  179. /* Program page with the buffer content */
  180. b47s->cc_write(b47s, BCMA_CC_FLASHADDR, page);
  181. bcm47xxsflash_cmd(b47s, OPCODE_AT_BUF1_PROGRAM);
  182. return written;
  183. }
  184. static int bcm47xxsflash_write(struct mtd_info *mtd, loff_t to, size_t len,
  185. size_t *retlen, const u_char *buf)
  186. {
  187. struct bcm47xxsflash *b47s = mtd->priv;
  188. int written;
  189. /* Writing functions can return without writing all passed data, for
  190. * example when the hardware is too old or when we git page boundary.
  191. */
  192. while (len > 0) {
  193. switch (b47s->type) {
  194. case BCM47XXSFLASH_TYPE_ST:
  195. written = bcm47xxsflash_write_st(mtd, to, len, buf);
  196. break;
  197. case BCM47XXSFLASH_TYPE_ATMEL:
  198. written = bcm47xxsflash_write_at(mtd, to, len, buf);
  199. break;
  200. default:
  201. BUG_ON(1);
  202. }
  203. if (written < 0) {
  204. pr_err("Error writing at offset 0x%llX\n", to);
  205. return written;
  206. }
  207. to += (loff_t)written;
  208. len -= written;
  209. *retlen += written;
  210. buf += written;
  211. }
  212. return 0;
  213. }
  214. static void bcm47xxsflash_fill_mtd(struct bcm47xxsflash *b47s,
  215. struct device *dev)
  216. {
  217. struct mtd_info *mtd = &b47s->mtd;
  218. mtd->priv = b47s;
  219. mtd->dev.parent = dev;
  220. mtd->name = "bcm47xxsflash";
  221. mtd->type = MTD_NORFLASH;
  222. mtd->flags = MTD_CAP_NORFLASH;
  223. mtd->size = b47s->size;
  224. mtd->erasesize = b47s->blocksize;
  225. mtd->writesize = 1;
  226. mtd->writebufsize = 1;
  227. mtd->_erase = bcm47xxsflash_erase;
  228. mtd->_read = bcm47xxsflash_read;
  229. mtd->_write = bcm47xxsflash_write;
  230. }
  231. /**************************************************
  232. * BCMA
  233. **************************************************/
  234. static int bcm47xxsflash_bcma_cc_read(struct bcm47xxsflash *b47s, u16 offset)
  235. {
  236. return bcma_cc_read32(b47s->bcma_cc, offset);
  237. }
  238. static void bcm47xxsflash_bcma_cc_write(struct bcm47xxsflash *b47s, u16 offset,
  239. u32 value)
  240. {
  241. bcma_cc_write32(b47s->bcma_cc, offset, value);
  242. }
  243. static int bcm47xxsflash_bcma_probe(struct platform_device *pdev)
  244. {
  245. struct device *dev = &pdev->dev;
  246. struct bcma_sflash *sflash = dev_get_platdata(dev);
  247. struct bcm47xxsflash *b47s;
  248. struct resource *res;
  249. int err;
  250. b47s = devm_kzalloc(dev, sizeof(*b47s), GFP_KERNEL);
  251. if (!b47s)
  252. return -ENOMEM;
  253. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  254. if (!res) {
  255. dev_err(dev, "invalid resource\n");
  256. return -EINVAL;
  257. }
  258. if (!devm_request_mem_region(dev, res->start, resource_size(res),
  259. res->name)) {
  260. dev_err(dev, "can't request region for resource %pR\n", res);
  261. return -EBUSY;
  262. }
  263. b47s->bcma_cc = container_of(sflash, struct bcma_drv_cc, sflash);
  264. b47s->cc_read = bcm47xxsflash_bcma_cc_read;
  265. b47s->cc_write = bcm47xxsflash_bcma_cc_write;
  266. /*
  267. * On old MIPS devices cache was magically invalidated when needed,
  268. * allowing us to use cached access and gain some performance. Trying
  269. * the same on ARM based BCM53573 results in flash corruptions, we need
  270. * to use uncached access for it.
  271. *
  272. * It may be arch specific, but right now there is only 1 ARM SoC using
  273. * this driver, so let's follow Broadcom's reference code and check
  274. * ChipCommon revision.
  275. */
  276. if (b47s->bcma_cc->core->id.rev == 54)
  277. b47s->window = ioremap_nocache(res->start, resource_size(res));
  278. else
  279. b47s->window = ioremap_cache(res->start, resource_size(res));
  280. if (!b47s->window) {
  281. dev_err(dev, "ioremap failed for resource %pR\n", res);
  282. return -ENOMEM;
  283. }
  284. switch (b47s->bcma_cc->capabilities & BCMA_CC_CAP_FLASHT) {
  285. case BCMA_CC_FLASHT_STSER:
  286. b47s->type = BCM47XXSFLASH_TYPE_ST;
  287. break;
  288. case BCMA_CC_FLASHT_ATSER:
  289. b47s->type = BCM47XXSFLASH_TYPE_ATMEL;
  290. break;
  291. }
  292. b47s->blocksize = sflash->blocksize;
  293. b47s->numblocks = sflash->numblocks;
  294. b47s->size = sflash->size;
  295. bcm47xxsflash_fill_mtd(b47s, &pdev->dev);
  296. platform_set_drvdata(pdev, b47s);
  297. err = mtd_device_parse_register(&b47s->mtd, probes, NULL, NULL, 0);
  298. if (err) {
  299. pr_err("Failed to register MTD device: %d\n", err);
  300. iounmap(b47s->window);
  301. return err;
  302. }
  303. if (bcm47xxsflash_poll(b47s, HZ / 10))
  304. pr_warn("Serial flash busy\n");
  305. return 0;
  306. }
  307. static int bcm47xxsflash_bcma_remove(struct platform_device *pdev)
  308. {
  309. struct bcm47xxsflash *b47s = platform_get_drvdata(pdev);
  310. mtd_device_unregister(&b47s->mtd);
  311. iounmap(b47s->window);
  312. return 0;
  313. }
  314. static struct platform_driver bcma_sflash_driver = {
  315. .probe = bcm47xxsflash_bcma_probe,
  316. .remove = bcm47xxsflash_bcma_remove,
  317. .driver = {
  318. .name = "bcma_sflash",
  319. },
  320. };
  321. /**************************************************
  322. * Init
  323. **************************************************/
  324. module_platform_driver(bcma_sflash_driver);