bcm47xxsflash.c 9.5 KB

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