cafe_nand.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * cafe_nand.c
  3. *
  4. * Copyright © 2006 Red Hat, Inc.
  5. * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
  6. */
  7. //#define DEBUG
  8. #include <linux/device.h>
  9. #undef DEBUG
  10. #include <linux/mtd/mtd.h>
  11. #include <linux/mtd/nand.h>
  12. #include <linux/pci.h>
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <asm/io.h>
  16. #define CAFE_NAND_CTRL1 0x00
  17. #define CAFE_NAND_CTRL2 0x04
  18. #define CAFE_NAND_CTRL3 0x08
  19. #define CAFE_NAND_STATUS 0x0c
  20. #define CAFE_NAND_IRQ 0x10
  21. #define CAFE_NAND_IRQ_MASK 0x14
  22. #define CAFE_NAND_DATA_LEN 0x18
  23. #define CAFE_NAND_ADDR1 0x1c
  24. #define CAFE_NAND_ADDR2 0x20
  25. #define CAFE_NAND_TIMING1 0x24
  26. #define CAFE_NAND_TIMING2 0x28
  27. #define CAFE_NAND_TIMING3 0x2c
  28. #define CAFE_NAND_NONMEM 0x30
  29. #define CAFE_NAND_DMA_CTRL 0x40
  30. #define CAFE_NAND_DMA_ADDR0 0x44
  31. #define CAFE_NAND_DMA_ADDR1 0x48
  32. #define CAFE_NAND_READ_DATA 0x1000
  33. #define CAFE_NAND_WRITE_DATA 0x2000
  34. struct cafe_priv {
  35. struct nand_chip nand;
  36. struct pci_dev *pdev;
  37. void __iomem *mmio;
  38. uint32_t ctl1;
  39. uint32_t ctl2;
  40. int datalen;
  41. int nr_data;
  42. int data_pos;
  43. int page_addr;
  44. dma_addr_t dmaaddr;
  45. unsigned char *dmabuf;
  46. };
  47. static int usedma = 1;
  48. module_param(usedma, int, 0644);
  49. static int cafe_device_ready(struct mtd_info *mtd)
  50. {
  51. struct cafe_priv *cafe = mtd->priv;
  52. int result = !!(readl(cafe->mmio + CAFE_NAND_STATUS) | 0x40000000);
  53. uint32_t irqs = readl(cafe->mmio + 0x10);
  54. writel(irqs, cafe->mmio+0x10);
  55. dev_dbg(&cafe->pdev->dev, "NAND device is%s ready, IRQ %x (%x) (%x,%x)\n",
  56. result?"":" not", irqs, readl(cafe->mmio + 0x10),
  57. readl(cafe->mmio + 0x3008), readl(cafe->mmio + 0x300c));
  58. return result;
  59. }
  60. static void cafe_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  61. {
  62. struct cafe_priv *cafe = mtd->priv;
  63. if (usedma)
  64. memcpy(cafe->dmabuf + cafe->datalen, buf, len);
  65. else
  66. memcpy_toio(cafe->mmio + CAFE_NAND_WRITE_DATA + cafe->datalen, buf, len);
  67. cafe->datalen += len;
  68. dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes to write buffer. datalen 0x%x\n",
  69. len, cafe->datalen);
  70. }
  71. static void cafe_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  72. {
  73. struct cafe_priv *cafe = mtd->priv;
  74. if (usedma)
  75. memcpy(buf, cafe->dmabuf + cafe->datalen, len);
  76. else
  77. memcpy_fromio(buf, cafe->mmio + CAFE_NAND_READ_DATA + cafe->datalen, len);
  78. dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes from position 0x%x in read buffer.\n",
  79. len, cafe->datalen);
  80. cafe->datalen += len;
  81. }
  82. static uint8_t cafe_read_byte(struct mtd_info *mtd)
  83. {
  84. struct cafe_priv *cafe = mtd->priv;
  85. uint8_t d;
  86. cafe_read_buf(mtd, &d, 1);
  87. dev_dbg(&cafe->pdev->dev, "Read %02x\n", d);
  88. return d;
  89. }
  90. static void cafe_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
  91. int column, int page_addr)
  92. {
  93. struct cafe_priv *cafe = mtd->priv;
  94. int adrbytes = 0;
  95. uint32_t ctl1;
  96. uint32_t doneint = 0x80000000;
  97. int i;
  98. dev_dbg(&cafe->pdev->dev, "cmdfunc %02x, 0x%x, 0x%x\n",
  99. command, column, page_addr);
  100. if (command == NAND_CMD_ERASE2 || command == NAND_CMD_PAGEPROG) {
  101. /* Second half of a command we already calculated */
  102. writel(cafe->ctl2 | 0x100 | command, cafe->mmio + 0x04);
  103. ctl1 = cafe->ctl1;
  104. dev_dbg(&cafe->pdev->dev, "Continue command, ctl1 %08x, #data %d\n",
  105. cafe->ctl1, cafe->nr_data);
  106. goto do_command;
  107. }
  108. /* Reset ECC engine */
  109. writel(0, cafe->mmio + CAFE_NAND_CTRL2);
  110. /* Emulate NAND_CMD_READOOB on large-page chips */
  111. if (mtd->writesize > 512 &&
  112. command == NAND_CMD_READOOB) {
  113. column += mtd->writesize;
  114. command = NAND_CMD_READ0;
  115. }
  116. /* FIXME: Do we need to send read command before sending data
  117. for small-page chips, to position the buffer correctly? */
  118. if (column != -1) {
  119. writel(column, cafe->mmio + 0x1c);
  120. adrbytes = 2;
  121. if (page_addr != -1)
  122. goto write_adr2;
  123. } else if (page_addr != -1) {
  124. writel(page_addr & 0xffff, cafe->mmio + 0x1c);
  125. page_addr >>= 16;
  126. write_adr2:
  127. writel(page_addr, cafe->mmio+0x20);
  128. adrbytes += 2;
  129. if (mtd->size > mtd->writesize << 16)
  130. adrbytes++;
  131. }
  132. cafe->data_pos = cafe->datalen = 0;
  133. /* Set command valid bit */
  134. ctl1 = 0x80000000 | command;
  135. /* Set RD or WR bits as appropriate */
  136. if (command == NAND_CMD_READID || command == NAND_CMD_STATUS) {
  137. ctl1 |= (1<<26); /* rd */
  138. /* Always 5 bytes, for now */
  139. cafe->datalen = 5;
  140. /* And one address cycle -- even for STATUS, since the controller doesn't work without */
  141. adrbytes = 1;
  142. } else if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
  143. command == NAND_CMD_READOOB || command == NAND_CMD_RNDOUT) {
  144. ctl1 |= 1<<26; /* rd */
  145. /* For now, assume just read to end of page */
  146. cafe->datalen = mtd->writesize + mtd->oobsize - column;
  147. } else if (command == NAND_CMD_SEQIN)
  148. ctl1 |= 1<<25; /* wr */
  149. /* Set number of address bytes */
  150. if (adrbytes)
  151. ctl1 |= ((adrbytes-1)|8) << 27;
  152. if (command == NAND_CMD_SEQIN || command == NAND_CMD_ERASE1) {
  153. /* Ignore the first command of a pair; the hardware
  154. deals with them both at once, later */
  155. cafe->ctl1 = ctl1;
  156. cafe->ctl2 = 0;
  157. dev_dbg(&cafe->pdev->dev, "Setup for delayed command, ctl1 %08x, dlen %x\n",
  158. cafe->ctl1, cafe->datalen);
  159. return;
  160. }
  161. /* RNDOUT and READ0 commands need a following byte */
  162. if (command == NAND_CMD_RNDOUT)
  163. writel(cafe->ctl2 | 0x100 | NAND_CMD_RNDOUTSTART, cafe->mmio + CAFE_NAND_CTRL2);
  164. else if (command == NAND_CMD_READ0 && mtd->writesize > 512)
  165. writel(cafe->ctl2 | 0x100 | NAND_CMD_READSTART, cafe->mmio + CAFE_NAND_CTRL2);
  166. do_command:
  167. if (cafe->datalen == 2112)
  168. cafe->datalen = 2062;
  169. dev_dbg(&cafe->pdev->dev, "dlen %x, ctl1 %x, ctl2 %x\n",
  170. cafe->datalen, ctl1, readl(cafe->mmio+CAFE_NAND_CTRL2));
  171. /* NB: The datasheet lies -- we really should be subtracting 1 here */
  172. writel(cafe->datalen, cafe->mmio + CAFE_NAND_DATA_LEN);
  173. writel(0x90000000, cafe->mmio + 0x10);
  174. if (usedma && (ctl1 & (3<<25))) {
  175. uint32_t dmactl = 0xc0000000 + cafe->datalen;
  176. /* If WR or RD bits set, set up DMA */
  177. if (ctl1 & (1<<26)) {
  178. /* It's a read */
  179. dmactl |= (1<<29);
  180. /* ... so it's done when the DMA is done, not just
  181. the command. */
  182. doneint = 0x10000000;
  183. }
  184. writel(dmactl, cafe->mmio + 0x40);
  185. }
  186. #if 0
  187. printk("DMA setup is %x, status %x, ctl1 %x\n", readl(cafe->mmio + 0x40), readl(cafe->mmio + 0x0c), readl(cafe->mmio));
  188. printk("DMA setup is %x, status %x, ctl1 %x\n", readl(cafe->mmio + 0x40), readl(cafe->mmio + 0x0c), readl(cafe->mmio));
  189. #endif
  190. cafe->datalen = 0;
  191. #if 0
  192. printk("About to write command %08x\n", ctl1);
  193. for (i=0; i< 0x5c; i+=4)
  194. printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
  195. #endif
  196. writel(ctl1, cafe->mmio + CAFE_NAND_CTRL1);
  197. /* Apply this short delay always to ensure that we do wait tWB in
  198. * any case on any machine. */
  199. ndelay(100);
  200. if (1) {
  201. int c = 50000;
  202. uint32_t irqs;
  203. while (c--) {
  204. irqs = readl(cafe->mmio + 0x10);
  205. if (irqs & doneint)
  206. break;
  207. udelay(1);
  208. if (!(c & 1000))
  209. dev_dbg(&cafe->pdev->dev, "Wait for ready, IRQ %x\n", irqs);
  210. cpu_relax();
  211. }
  212. writel(doneint, cafe->mmio + 0x10);
  213. dev_dbg(&cafe->pdev->dev, "Command %x completed after %d usec, irqs %x (%x)\n", command, 50000-c, irqs, readl(cafe->mmio + 0x10));
  214. }
  215. cafe->ctl2 &= ~(1<<8);
  216. cafe->ctl2 &= ~(1<<30);
  217. switch (command) {
  218. case NAND_CMD_CACHEDPROG:
  219. case NAND_CMD_PAGEPROG:
  220. case NAND_CMD_ERASE1:
  221. case NAND_CMD_ERASE2:
  222. case NAND_CMD_SEQIN:
  223. case NAND_CMD_RNDIN:
  224. case NAND_CMD_STATUS:
  225. case NAND_CMD_DEPLETE1:
  226. case NAND_CMD_RNDOUT:
  227. case NAND_CMD_STATUS_ERROR:
  228. case NAND_CMD_STATUS_ERROR0:
  229. case NAND_CMD_STATUS_ERROR1:
  230. case NAND_CMD_STATUS_ERROR2:
  231. case NAND_CMD_STATUS_ERROR3:
  232. writel(cafe->ctl2, cafe->mmio + CAFE_NAND_CTRL2);
  233. return;
  234. }
  235. nand_wait_ready(mtd);
  236. writel(cafe->ctl2, cafe->mmio + CAFE_NAND_CTRL2);
  237. }
  238. static void cafe_select_chip(struct mtd_info *mtd, int chipnr)
  239. {
  240. //struct cafe_priv *cafe = mtd->priv;
  241. // dev_dbg(&cafe->pdev->dev, "select_chip %d\n", chipnr);
  242. }
  243. static int cafe_nand_interrupt(int irq, void *id, struct pt_regs *regs)
  244. {
  245. struct mtd_info *mtd = id;
  246. struct cafe_priv *cafe = mtd->priv;
  247. uint32_t irqs = readl(cafe->mmio + 0x10);
  248. writel(irqs & ~0x90000000, cafe->mmio + 0x10);
  249. if (!irqs)
  250. return IRQ_NONE;
  251. dev_dbg(&cafe->pdev->dev, "irq, bits %x (%x)\n", irqs, readl(cafe->mmio + 0x10));
  252. return IRQ_HANDLED;
  253. }
  254. static void cafe_nand_bug(struct mtd_info *mtd)
  255. {
  256. BUG();
  257. }
  258. static int cafe_nand_write_oob(struct mtd_info *mtd,
  259. struct nand_chip *chip, int page)
  260. {
  261. int status = 0;
  262. WARN_ON(chip->oob_poi != chip->buffers->oobwbuf);
  263. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  264. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  265. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  266. status = chip->waitfunc(mtd, chip);
  267. return status & NAND_STATUS_FAIL ? -EIO : 0;
  268. }
  269. /* Don't use -- use nand_read_oob_std for now */
  270. static int cafe_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  271. int page, int sndcmd)
  272. {
  273. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  274. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  275. return 1;
  276. }
  277. /**
  278. * cafe_nand_read_page_syndrome - {REPLACABLE] hardware ecc syndrom based page read
  279. * @mtd: mtd info structure
  280. * @chip: nand chip info structure
  281. * @buf: buffer to store read data
  282. *
  283. * The hw generator calculates the error syndrome automatically. Therefor
  284. * we need a special oob layout and handling.
  285. */
  286. static int cafe_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  287. uint8_t *buf)
  288. {
  289. struct cafe_priv *cafe = mtd->priv;
  290. WARN_ON(chip->oob_poi != chip->buffers->oobrbuf);
  291. dev_dbg(&cafe->pdev->dev, "ECC result %08x SYN1,2 %08x\n", readl(cafe->mmio + 0x3c), readl(cafe->mmio + 0x50));
  292. chip->read_buf(mtd, buf, mtd->writesize);
  293. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  294. return 0;
  295. }
  296. static char foo[14];
  297. static void cafe_nand_write_page_lowlevel(struct mtd_info *mtd,
  298. struct nand_chip *chip, const uint8_t *buf)
  299. {
  300. struct cafe_priv *cafe = mtd->priv;
  301. WARN_ON(chip->oob_poi != chip->buffers->oobwbuf);
  302. chip->write_buf(mtd, buf, mtd->writesize);
  303. chip->write_buf(mtd, foo, 14);
  304. // chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  305. /* Set up ECC autogeneration */
  306. cafe->ctl2 |= (1<<27) | (1<<30);
  307. if (mtd->writesize == 2048)
  308. cafe->ctl2 |= (1<<29);
  309. }
  310. static int cafe_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  311. const uint8_t *buf, int page, int cached, int raw)
  312. {
  313. int status;
  314. WARN_ON(chip->oob_poi != chip->buffers->oobwbuf);
  315. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  316. if (unlikely(raw))
  317. chip->ecc.write_page_raw(mtd, chip, buf);
  318. else
  319. chip->ecc.write_page(mtd, chip, buf);
  320. /*
  321. * Cached progamming disabled for now, Not sure if its worth the
  322. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
  323. */
  324. cached = 0;
  325. if (!cached || !(chip->options & NAND_CACHEPRG)) {
  326. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  327. status = chip->waitfunc(mtd, chip);
  328. /*
  329. * See if operation failed and additional status checks are
  330. * available
  331. */
  332. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  333. status = chip->errstat(mtd, chip, FL_WRITING, status,
  334. page);
  335. if (status & NAND_STATUS_FAIL)
  336. return -EIO;
  337. } else {
  338. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  339. status = chip->waitfunc(mtd, chip);
  340. }
  341. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  342. /* Send command to read back the data */
  343. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  344. if (chip->verify_buf(mtd, buf, mtd->writesize))
  345. return -EIO;
  346. #endif
  347. return 0;
  348. }
  349. static int __devinit cafe_nand_probe(struct pci_dev *pdev,
  350. const struct pci_device_id *ent)
  351. {
  352. struct mtd_info *mtd;
  353. struct cafe_priv *cafe;
  354. uint32_t ctrl;
  355. int err = 0;
  356. err = pci_enable_device(pdev);
  357. if (err)
  358. return err;
  359. pci_set_master(pdev);
  360. mtd = kzalloc(sizeof(*mtd) + sizeof(struct cafe_priv), GFP_KERNEL);
  361. if (!mtd) {
  362. dev_warn(&pdev->dev, "failed to alloc mtd_info\n");
  363. return -ENOMEM;
  364. }
  365. cafe = (void *)(&mtd[1]);
  366. mtd->priv = cafe;
  367. mtd->owner = THIS_MODULE;
  368. cafe->pdev = pdev;
  369. cafe->mmio = pci_iomap(pdev, 0, 0);
  370. if (!cafe->mmio) {
  371. dev_warn(&pdev->dev, "failed to iomap\n");
  372. err = -ENOMEM;
  373. goto out_free_mtd;
  374. }
  375. cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev, 2112 + sizeof(struct nand_buffers),
  376. &cafe->dmaaddr, GFP_KERNEL);
  377. if (!cafe->dmabuf) {
  378. err = -ENOMEM;
  379. goto out_ior;
  380. }
  381. cafe->nand.buffers = (void *)cafe->dmabuf + 2112;
  382. cafe->nand.cmdfunc = cafe_nand_cmdfunc;
  383. cafe->nand.dev_ready = cafe_device_ready;
  384. cafe->nand.read_byte = cafe_read_byte;
  385. cafe->nand.read_buf = cafe_read_buf;
  386. cafe->nand.write_buf = cafe_write_buf;
  387. cafe->nand.select_chip = cafe_select_chip;
  388. cafe->nand.chip_delay = 0;
  389. /* Enable the following for a flash based bad block table */
  390. cafe->nand.options = NAND_USE_FLASH_BBT | NAND_NO_AUTOINCR | NAND_OWN_BUFFERS;
  391. /* Timings from Marvell's test code (not verified or calculated by us) */
  392. writel(0xffffffff, cafe->mmio + CAFE_NAND_IRQ_MASK);
  393. #if 1
  394. writel(0x01010a0a, cafe->mmio + CAFE_NAND_TIMING1);
  395. writel(0x24121212, cafe->mmio + CAFE_NAND_TIMING2);
  396. writel(0x11000000, cafe->mmio + CAFE_NAND_TIMING3);
  397. #else
  398. writel(0xffffffff, cafe->mmio + CAFE_NAND_TIMING1);
  399. writel(0xffffffff, cafe->mmio + CAFE_NAND_TIMING2);
  400. writel(0xffffffff, cafe->mmio + CAFE_NAND_TIMING3);
  401. #endif
  402. writel(0xdfffffff, cafe->mmio + 0x14);
  403. err = request_irq(pdev->irq, &cafe_nand_interrupt, SA_SHIRQ, "CAFE NAND", mtd);
  404. if (err) {
  405. dev_warn(&pdev->dev, "Could not register IRQ %d\n", pdev->irq);
  406. goto out_free_dma;
  407. }
  408. #if 1
  409. /* Disable master reset, enable NAND clock */
  410. ctrl = readl(cafe->mmio + 0x3004);
  411. ctrl &= 0xffffeff0;
  412. ctrl |= 0x00007000;
  413. writel(ctrl | 0x05, cafe->mmio + 0x3004);
  414. writel(ctrl | 0x0a, cafe->mmio + 0x3004);
  415. writel(0, cafe->mmio + 0x40);
  416. writel(0x7006, cafe->mmio + 0x3004);
  417. writel(0x700a, cafe->mmio + 0x3004);
  418. /* Set up DMA address */
  419. writel(cafe->dmaaddr & 0xffffffff, cafe->mmio + 0x44);
  420. if (sizeof(cafe->dmaaddr) > 4)
  421. writel((cafe->dmaaddr >> 16) >> 16, cafe->mmio + 0x48);
  422. else
  423. writel(0, cafe->mmio + 0x48);
  424. dev_dbg(&cafe->pdev->dev, "Set DMA address to %x (virt %p)\n",
  425. readl(cafe->mmio+0x44), cafe->dmabuf);
  426. /* Enable NAND IRQ in global IRQ mask register */
  427. writel(0x80000007, cafe->mmio + 0x300c);
  428. dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n",
  429. readl(cafe->mmio + 0x3004), readl(cafe->mmio + 0x300c));
  430. #endif
  431. #if 1
  432. mtd->writesize=2048;
  433. mtd->oobsize = 0x40;
  434. memset(cafe->dmabuf, 0xa5, 2112);
  435. cafe->nand.cmdfunc(mtd, NAND_CMD_READID, 0, -1);
  436. cafe->nand.read_byte(mtd);
  437. cafe->nand.read_byte(mtd);
  438. cafe->nand.read_byte(mtd);
  439. cafe->nand.read_byte(mtd);
  440. cafe->nand.read_byte(mtd);
  441. #endif
  442. #if 0
  443. cafe->nand.cmdfunc(mtd, NAND_CMD_READ0, 0, 0);
  444. // nand_wait_ready(mtd);
  445. cafe->nand.read_byte(mtd);
  446. cafe->nand.read_byte(mtd);
  447. cafe->nand.read_byte(mtd);
  448. cafe->nand.read_byte(mtd);
  449. #endif
  450. #if 0
  451. writel(0x84600070, cafe->mmio);
  452. udelay(10);
  453. dev_dbg(&cafe->pdev->dev, "Status %x\n", readl(cafe->mmio + 0x30));
  454. #endif
  455. /* Scan to find existance of the device */
  456. if (nand_scan_ident(mtd, 1)) {
  457. err = -ENXIO;
  458. goto out_irq;
  459. }
  460. cafe->ctl2 = 1<<27; /* Reed-Solomon ECC */
  461. if (mtd->writesize == 2048)
  462. cafe->ctl2 |= 1<<29; /* 2KiB page size */
  463. /* Set up ECC according to the type of chip we found */
  464. if (mtd->writesize == 512 || mtd->writesize == 2048) {
  465. cafe->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
  466. cafe->nand.ecc.size = mtd->writesize;
  467. cafe->nand.ecc.bytes = 14;
  468. cafe->nand.ecc.hwctl = (void *)cafe_nand_bug;
  469. cafe->nand.ecc.calculate = (void *)cafe_nand_bug;
  470. cafe->nand.ecc.correct = (void *)cafe_nand_bug;
  471. cafe->nand.write_page = cafe_nand_write_page;
  472. cafe->nand.ecc.write_page = cafe_nand_write_page_lowlevel;
  473. cafe->nand.ecc.write_oob = cafe_nand_write_oob;
  474. cafe->nand.ecc.read_page = cafe_nand_read_page;
  475. cafe->nand.ecc.read_oob = cafe_nand_read_oob;
  476. } else {
  477. printk(KERN_WARNING "Unexpected NAND flash writesize %d. Using software ECC\n",
  478. mtd->writesize);
  479. cafe->nand.ecc.mode = NAND_ECC_NONE;
  480. }
  481. err = nand_scan_tail(mtd);
  482. if (err)
  483. goto out_irq;
  484. pci_set_drvdata(pdev, mtd);
  485. add_mtd_device(mtd);
  486. goto out;
  487. out_irq:
  488. /* Disable NAND IRQ in global IRQ mask register */
  489. writel(~1 & readl(cafe->mmio + 0x300c), cafe->mmio + 0x300c);
  490. free_irq(pdev->irq, mtd);
  491. out_free_dma:
  492. dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
  493. out_ior:
  494. pci_iounmap(pdev, cafe->mmio);
  495. out_free_mtd:
  496. kfree(mtd);
  497. out:
  498. return err;
  499. }
  500. static void __devexit cafe_nand_remove(struct pci_dev *pdev)
  501. {
  502. struct mtd_info *mtd = pci_get_drvdata(pdev);
  503. struct cafe_priv *cafe = mtd->priv;
  504. del_mtd_device(mtd);
  505. /* Disable NAND IRQ in global IRQ mask register */
  506. writel(~1 & readl(cafe->mmio + 0x300c), cafe->mmio + 0x300c);
  507. free_irq(pdev->irq, mtd);
  508. nand_release(mtd);
  509. pci_iounmap(pdev, cafe->mmio);
  510. dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
  511. kfree(mtd);
  512. }
  513. static struct pci_device_id cafe_nand_tbl[] = {
  514. { 0x11ab, 0x4100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MEMORY_FLASH << 8, 0xFFFF0 }
  515. };
  516. MODULE_DEVICE_TABLE(pci, cafe_nand_tbl);
  517. static struct pci_driver cafe_nand_pci_driver = {
  518. .name = "CAFÉ NAND",
  519. .id_table = cafe_nand_tbl,
  520. .probe = cafe_nand_probe,
  521. .remove = __devexit_p(cafe_nand_remove),
  522. #ifdef CONFIG_PMx
  523. .suspend = cafe_nand_suspend,
  524. .resume = cafe_nand_resume,
  525. #endif
  526. };
  527. static int cafe_nand_init(void)
  528. {
  529. return pci_register_driver(&cafe_nand_pci_driver);
  530. }
  531. static void cafe_nand_exit(void)
  532. {
  533. pci_unregister_driver(&cafe_nand_pci_driver);
  534. }
  535. module_init(cafe_nand_init);
  536. module_exit(cafe_nand_exit);
  537. MODULE_LICENSE("GPL");
  538. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  539. MODULE_DESCRIPTION("NAND flash driver for OLPC CAFE chip");
  540. /* Correct ECC for 2048 bytes of 0xff:
  541. 41 a0 71 65 54 27 f3 93 ec a9 be ed 0b a1 */