cafe.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. * Driver for One Laptop Per Child ‘CAFÉ’ controller, aka Marvell 88ALP01
  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_ECC_RESULT 0x3C
  30. #define CAFE_NAND_DMA_CTRL 0x40
  31. #define CAFE_NAND_DMA_ADDR0 0x44
  32. #define CAFE_NAND_DMA_ADDR1 0x48
  33. #define CAFE_NAND_ECC_SYN01 0x50
  34. #define CAFE_NAND_ECC_SYN23 0x54
  35. #define CAFE_NAND_ECC_SYN45 0x58
  36. #define CAFE_NAND_ECC_SYN67 0x5c
  37. #define CAFE_NAND_READ_DATA 0x1000
  38. #define CAFE_NAND_WRITE_DATA 0x2000
  39. int cafe_correct_ecc(unsigned char *buf,
  40. unsigned short *chk_syndrome_list);
  41. struct cafe_priv {
  42. struct nand_chip nand;
  43. struct pci_dev *pdev;
  44. void __iomem *mmio;
  45. uint32_t ctl1;
  46. uint32_t ctl2;
  47. int datalen;
  48. int nr_data;
  49. int data_pos;
  50. int page_addr;
  51. dma_addr_t dmaaddr;
  52. unsigned char *dmabuf;
  53. };
  54. static int usedma = 0;
  55. module_param(usedma, int, 0644);
  56. static int skipbbt = 0;
  57. module_param(skipbbt, int, 0644);
  58. static int debug = 0;
  59. module_param(debug, int, 0644);
  60. static int checkecc = 0;
  61. module_param(checkecc, int, 0644);
  62. /* Hrm. Why isn't this already conditional on something in the struct device? */
  63. #define cafe_dev_dbg(dev, args...) do { if (debug) dev_dbg(dev, ##args); } while(0)
  64. static int cafe_device_ready(struct mtd_info *mtd)
  65. {
  66. struct cafe_priv *cafe = mtd->priv;
  67. int result = !!(readl(cafe->mmio + CAFE_NAND_STATUS) | 0x40000000);
  68. uint32_t irqs = readl(cafe->mmio + CAFE_NAND_IRQ);
  69. writel(irqs, cafe->mmio+CAFE_NAND_IRQ);
  70. cafe_dev_dbg(&cafe->pdev->dev, "NAND device is%s ready, IRQ %x (%x) (%x,%x)\n",
  71. result?"":" not", irqs, readl(cafe->mmio + CAFE_NAND_IRQ),
  72. readl(cafe->mmio + 0x3008), readl(cafe->mmio + 0x300c));
  73. return result;
  74. }
  75. static void cafe_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  76. {
  77. struct cafe_priv *cafe = mtd->priv;
  78. if (usedma)
  79. memcpy(cafe->dmabuf + cafe->datalen, buf, len);
  80. else
  81. memcpy_toio(cafe->mmio + CAFE_NAND_WRITE_DATA + cafe->datalen, buf, len);
  82. cafe->datalen += len;
  83. cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes to write buffer. datalen 0x%x\n",
  84. len, cafe->datalen);
  85. }
  86. static void cafe_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  87. {
  88. struct cafe_priv *cafe = mtd->priv;
  89. if (usedma)
  90. memcpy(buf, cafe->dmabuf + cafe->datalen, len);
  91. else
  92. memcpy_fromio(buf, cafe->mmio + CAFE_NAND_READ_DATA + cafe->datalen, len);
  93. cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes from position 0x%x in read buffer.\n",
  94. len, cafe->datalen);
  95. cafe->datalen += len;
  96. }
  97. static uint8_t cafe_read_byte(struct mtd_info *mtd)
  98. {
  99. struct cafe_priv *cafe = mtd->priv;
  100. uint8_t d;
  101. cafe_read_buf(mtd, &d, 1);
  102. cafe_dev_dbg(&cafe->pdev->dev, "Read %02x\n", d);
  103. return d;
  104. }
  105. static void cafe_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
  106. int column, int page_addr)
  107. {
  108. struct cafe_priv *cafe = mtd->priv;
  109. int adrbytes = 0;
  110. uint32_t ctl1;
  111. uint32_t doneint = 0x80000000;
  112. cafe_dev_dbg(&cafe->pdev->dev, "cmdfunc %02x, 0x%x, 0x%x\n",
  113. command, column, page_addr);
  114. if (command == NAND_CMD_ERASE2 || command == NAND_CMD_PAGEPROG) {
  115. /* Second half of a command we already calculated */
  116. writel(cafe->ctl2 | 0x100 | command, cafe->mmio + CAFE_NAND_CTRL2);
  117. ctl1 = cafe->ctl1;
  118. cafe_dev_dbg(&cafe->pdev->dev, "Continue command, ctl1 %08x, #data %d\n",
  119. cafe->ctl1, cafe->nr_data);
  120. goto do_command;
  121. }
  122. /* Reset ECC engine */
  123. writel(0, cafe->mmio + CAFE_NAND_CTRL2);
  124. /* Emulate NAND_CMD_READOOB on large-page chips */
  125. if (mtd->writesize > 512 &&
  126. command == NAND_CMD_READOOB) {
  127. column += mtd->writesize;
  128. command = NAND_CMD_READ0;
  129. }
  130. /* FIXME: Do we need to send read command before sending data
  131. for small-page chips, to position the buffer correctly? */
  132. if (column != -1) {
  133. writel(column, cafe->mmio + CAFE_NAND_ADDR1);
  134. adrbytes = 2;
  135. if (page_addr != -1)
  136. goto write_adr2;
  137. } else if (page_addr != -1) {
  138. writel(page_addr & 0xffff, cafe->mmio + CAFE_NAND_ADDR1);
  139. page_addr >>= 16;
  140. write_adr2:
  141. writel(page_addr, cafe->mmio+0x20);
  142. adrbytes += 2;
  143. if (mtd->size > mtd->writesize << 16)
  144. adrbytes++;
  145. }
  146. cafe->data_pos = cafe->datalen = 0;
  147. /* Set command valid bit */
  148. ctl1 = 0x80000000 | command;
  149. /* Set RD or WR bits as appropriate */
  150. if (command == NAND_CMD_READID || command == NAND_CMD_STATUS) {
  151. ctl1 |= (1<<26); /* rd */
  152. /* Always 5 bytes, for now */
  153. cafe->datalen = 4;
  154. /* And one address cycle -- even for STATUS, since the controller doesn't work without */
  155. adrbytes = 1;
  156. } else if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
  157. command == NAND_CMD_READOOB || command == NAND_CMD_RNDOUT) {
  158. ctl1 |= 1<<26; /* rd */
  159. /* For now, assume just read to end of page */
  160. cafe->datalen = mtd->writesize + mtd->oobsize - column;
  161. } else if (command == NAND_CMD_SEQIN)
  162. ctl1 |= 1<<25; /* wr */
  163. /* Set number of address bytes */
  164. if (adrbytes)
  165. ctl1 |= ((adrbytes-1)|8) << 27;
  166. if (command == NAND_CMD_SEQIN || command == NAND_CMD_ERASE1) {
  167. /* Ignore the first command of a pair; the hardware
  168. deals with them both at once, later */
  169. cafe->ctl1 = ctl1;
  170. cafe->ctl2 = 0;
  171. cafe_dev_dbg(&cafe->pdev->dev, "Setup for delayed command, ctl1 %08x, dlen %x\n",
  172. cafe->ctl1, cafe->datalen);
  173. return;
  174. }
  175. /* RNDOUT and READ0 commands need a following byte */
  176. if (command == NAND_CMD_RNDOUT)
  177. writel(cafe->ctl2 | 0x100 | NAND_CMD_RNDOUTSTART, cafe->mmio + CAFE_NAND_CTRL2);
  178. else if (command == NAND_CMD_READ0 && mtd->writesize > 512)
  179. writel(cafe->ctl2 | 0x100 | NAND_CMD_READSTART, cafe->mmio + CAFE_NAND_CTRL2);
  180. do_command:
  181. #if 0
  182. /* http://dev.laptop.org/ticket/200
  183. ECC on read only works if we read precisely 0x80e bytes */
  184. if (cafe->datalen == 2112)
  185. cafe->datalen = 2062;
  186. #endif
  187. cafe_dev_dbg(&cafe->pdev->dev, "dlen %x, ctl1 %x, ctl2 %x\n",
  188. cafe->datalen, ctl1, readl(cafe->mmio+CAFE_NAND_CTRL2));
  189. /* NB: The datasheet lies -- we really should be subtracting 1 here */
  190. writel(cafe->datalen, cafe->mmio + CAFE_NAND_DATA_LEN);
  191. writel(0x90000000, cafe->mmio + CAFE_NAND_IRQ);
  192. if (usedma && (ctl1 & (3<<25))) {
  193. uint32_t dmactl = 0xc0000000 + cafe->datalen;
  194. /* If WR or RD bits set, set up DMA */
  195. if (ctl1 & (1<<26)) {
  196. /* It's a read */
  197. dmactl |= (1<<29);
  198. /* ... so it's done when the DMA is done, not just
  199. the command. */
  200. doneint = 0x10000000;
  201. }
  202. writel(dmactl, cafe->mmio + CAFE_NAND_DMA_CTRL);
  203. }
  204. cafe->datalen = 0;
  205. #if 0
  206. { int i;
  207. printk("About to write command %08x\n", ctl1);
  208. for (i=0; i< 0x5c; i+=4)
  209. printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
  210. }
  211. #endif
  212. writel(ctl1, cafe->mmio + CAFE_NAND_CTRL1);
  213. /* Apply this short delay always to ensure that we do wait tWB in
  214. * any case on any machine. */
  215. ndelay(100);
  216. if (1) {
  217. int c = 500000;
  218. uint32_t irqs;
  219. while (c--) {
  220. irqs = readl(cafe->mmio + CAFE_NAND_IRQ);
  221. if (irqs & doneint)
  222. break;
  223. udelay(1);
  224. if (!(c % 100000))
  225. cafe_dev_dbg(&cafe->pdev->dev, "Wait for ready, IRQ %x\n", irqs);
  226. cpu_relax();
  227. }
  228. writel(doneint, cafe->mmio + CAFE_NAND_IRQ);
  229. cafe_dev_dbg(&cafe->pdev->dev, "Command %x completed after %d usec, irqs %x (%x)\n", command, 50000-c, irqs, readl(cafe->mmio + CAFE_NAND_IRQ));
  230. }
  231. cafe->ctl2 &= ~(1<<8);
  232. cafe->ctl2 &= ~(1<<30);
  233. switch (command) {
  234. case NAND_CMD_CACHEDPROG:
  235. case NAND_CMD_PAGEPROG:
  236. case NAND_CMD_ERASE1:
  237. case NAND_CMD_ERASE2:
  238. case NAND_CMD_SEQIN:
  239. case NAND_CMD_RNDIN:
  240. case NAND_CMD_STATUS:
  241. case NAND_CMD_DEPLETE1:
  242. case NAND_CMD_RNDOUT:
  243. case NAND_CMD_STATUS_ERROR:
  244. case NAND_CMD_STATUS_ERROR0:
  245. case NAND_CMD_STATUS_ERROR1:
  246. case NAND_CMD_STATUS_ERROR2:
  247. case NAND_CMD_STATUS_ERROR3:
  248. writel(cafe->ctl2, cafe->mmio + CAFE_NAND_CTRL2);
  249. return;
  250. }
  251. nand_wait_ready(mtd);
  252. writel(cafe->ctl2, cafe->mmio + CAFE_NAND_CTRL2);
  253. }
  254. static void cafe_select_chip(struct mtd_info *mtd, int chipnr)
  255. {
  256. //struct cafe_priv *cafe = mtd->priv;
  257. // cafe_dev_dbg(&cafe->pdev->dev, "select_chip %d\n", chipnr);
  258. }
  259. static int cafe_nand_interrupt(int irq, void *id, struct pt_regs *regs)
  260. {
  261. struct mtd_info *mtd = id;
  262. struct cafe_priv *cafe = mtd->priv;
  263. uint32_t irqs = readl(cafe->mmio + CAFE_NAND_IRQ);
  264. writel(irqs & ~0x90000000, cafe->mmio + CAFE_NAND_IRQ);
  265. if (!irqs)
  266. return IRQ_NONE;
  267. cafe_dev_dbg(&cafe->pdev->dev, "irq, bits %x (%x)\n", irqs, readl(cafe->mmio + CAFE_NAND_IRQ));
  268. return IRQ_HANDLED;
  269. }
  270. static void cafe_nand_bug(struct mtd_info *mtd)
  271. {
  272. BUG();
  273. }
  274. static int cafe_nand_write_oob(struct mtd_info *mtd,
  275. struct nand_chip *chip, int page)
  276. {
  277. int status = 0;
  278. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  279. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  280. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  281. status = chip->waitfunc(mtd, chip);
  282. return status & NAND_STATUS_FAIL ? -EIO : 0;
  283. }
  284. /* Don't use -- use nand_read_oob_std for now */
  285. static int cafe_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  286. int page, int sndcmd)
  287. {
  288. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  289. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  290. return 1;
  291. }
  292. /**
  293. * cafe_nand_read_page_syndrome - {REPLACABLE] hardware ecc syndrom based page read
  294. * @mtd: mtd info structure
  295. * @chip: nand chip info structure
  296. * @buf: buffer to store read data
  297. *
  298. * The hw generator calculates the error syndrome automatically. Therefor
  299. * we need a special oob layout and handling.
  300. */
  301. static unsigned short cafe_empty_syndromes[8] = { 4095, 748, 2629, 2920, 875, 1454, 51, 1456 };
  302. static int is_all_ff(unsigned char *buf, int len)
  303. {
  304. unsigned long *lbuf = (void *)buf;
  305. int i;
  306. for (i=0; i < (len/sizeof(long)); i++) {
  307. if (lbuf[i] != ~0UL)
  308. return 0;
  309. }
  310. i *= sizeof(long);
  311. for (; i< len; i++) {
  312. if (buf[i] != 0xff)
  313. return 0;
  314. }
  315. return 1;
  316. }
  317. static int cafe_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  318. uint8_t *buf)
  319. {
  320. struct cafe_priv *cafe = mtd->priv;
  321. cafe_dev_dbg(&cafe->pdev->dev, "ECC result %08x SYN1,2 %08x\n",
  322. readl(cafe->mmio + CAFE_NAND_ECC_RESULT),
  323. readl(cafe->mmio + CAFE_NAND_ECC_SYN01));
  324. chip->read_buf(mtd, buf, mtd->writesize);
  325. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  326. if (checkecc && readl(cafe->mmio + CAFE_NAND_ECC_RESULT) & (1<<18)) {
  327. unsigned short syn[8];
  328. int i;
  329. for (i=0; i<8; i+=2) {
  330. uint32_t tmp = readl(cafe->mmio + CAFE_NAND_ECC_SYN01 + (i*2));
  331. syn[i] = tmp & 0xfff;
  332. syn[i+1] = (tmp >> 16) & 0xfff;
  333. }
  334. /* FIXME: http://dev.laptop.org/ticket/215 */
  335. if (!memcmp(syn, cafe_empty_syndromes, sizeof(syn))
  336. && is_all_ff(chip->oob_poi, 14)
  337. && is_all_ff(buf, mtd->writesize)) {
  338. dev_dbg(&cafe->pdev->dev, "ECC error reported on empty block\n");
  339. /* It was an empty block. Nothing to fix here except the hardware */
  340. } else if ((i = cafe_correct_ecc(buf, syn)) < 0) {
  341. dev_dbg(&cafe->pdev->dev, "Failed to correct ECC\n");
  342. mtd->ecc_stats.failed++;
  343. } else {
  344. dev_dbg(&cafe->pdev->dev, "Corrected %d symbol errors\n", i);
  345. mtd->ecc_stats.corrected += i;
  346. }
  347. }
  348. return 0;
  349. }
  350. static struct nand_ecclayout cafe_oobinfo_2048 = {
  351. .eccbytes = 14,
  352. .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
  353. .oobfree = {{14, 50}}
  354. };
  355. /* Ick. The BBT code really ought to be able to work this bit out
  356. for itself from the above, at least for the 2KiB case */
  357. static uint8_t cafe_bbt_pattern_2048[] = { 'B', 'b', 't', '0' };
  358. static uint8_t cafe_mirror_pattern_2048[] = { '1', 't', 'b', 'B' };
  359. static uint8_t cafe_bbt_pattern_512[] = { 0xBB };
  360. static uint8_t cafe_mirror_pattern_512[] = { 0xBC };
  361. static struct nand_bbt_descr cafe_bbt_main_descr_2048 = {
  362. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  363. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  364. .offs = 14,
  365. .len = 4,
  366. .veroffs = 18,
  367. .maxblocks = 4,
  368. .pattern = cafe_bbt_pattern_2048
  369. };
  370. static struct nand_bbt_descr cafe_bbt_mirror_descr_2048 = {
  371. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  372. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  373. .offs = 14,
  374. .len = 4,
  375. .veroffs = 18,
  376. .maxblocks = 4,
  377. .pattern = cafe_mirror_pattern_2048
  378. };
  379. static struct nand_ecclayout cafe_oobinfo_512 = {
  380. .eccbytes = 14,
  381. .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
  382. .oobfree = {{14, 2}}
  383. };
  384. static struct nand_bbt_descr cafe_bbt_main_descr_512 = {
  385. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  386. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  387. .offs = 14,
  388. .len = 1,
  389. .veroffs = 15,
  390. .maxblocks = 4,
  391. .pattern = cafe_bbt_pattern_512
  392. };
  393. static struct nand_bbt_descr cafe_bbt_mirror_descr_512 = {
  394. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  395. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  396. .offs = 14,
  397. .len = 1,
  398. .veroffs = 15,
  399. .maxblocks = 4,
  400. .pattern = cafe_mirror_pattern_512
  401. };
  402. static void cafe_nand_write_page_lowlevel(struct mtd_info *mtd,
  403. struct nand_chip *chip, const uint8_t *buf)
  404. {
  405. struct cafe_priv *cafe = mtd->priv;
  406. chip->write_buf(mtd, buf, mtd->writesize);
  407. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  408. /* Set up ECC autogeneration */
  409. cafe->ctl2 |= (1<<27) | (1<<30);
  410. if (mtd->writesize == 2048)
  411. cafe->ctl2 |= (1<<29);
  412. }
  413. static int cafe_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  414. const uint8_t *buf, int page, int cached, int raw)
  415. {
  416. int status;
  417. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  418. if (unlikely(raw))
  419. chip->ecc.write_page_raw(mtd, chip, buf);
  420. else
  421. chip->ecc.write_page(mtd, chip, buf);
  422. /*
  423. * Cached progamming disabled for now, Not sure if its worth the
  424. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
  425. */
  426. cached = 0;
  427. if (!cached || !(chip->options & NAND_CACHEPRG)) {
  428. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  429. status = chip->waitfunc(mtd, chip);
  430. /*
  431. * See if operation failed and additional status checks are
  432. * available
  433. */
  434. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  435. status = chip->errstat(mtd, chip, FL_WRITING, status,
  436. page);
  437. if (status & NAND_STATUS_FAIL)
  438. return -EIO;
  439. } else {
  440. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  441. status = chip->waitfunc(mtd, chip);
  442. }
  443. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  444. /* Send command to read back the data */
  445. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  446. if (chip->verify_buf(mtd, buf, mtd->writesize))
  447. return -EIO;
  448. #endif
  449. return 0;
  450. }
  451. static int cafe_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
  452. {
  453. return 0;
  454. }
  455. static int __devinit cafe_nand_probe(struct pci_dev *pdev,
  456. const struct pci_device_id *ent)
  457. {
  458. struct mtd_info *mtd;
  459. struct cafe_priv *cafe;
  460. uint32_t ctrl;
  461. int err = 0;
  462. err = pci_enable_device(pdev);
  463. if (err)
  464. return err;
  465. pci_set_master(pdev);
  466. mtd = kzalloc(sizeof(*mtd) + sizeof(struct cafe_priv), GFP_KERNEL);
  467. if (!mtd) {
  468. dev_warn(&pdev->dev, "failed to alloc mtd_info\n");
  469. return -ENOMEM;
  470. }
  471. cafe = (void *)(&mtd[1]);
  472. mtd->priv = cafe;
  473. mtd->owner = THIS_MODULE;
  474. cafe->pdev = pdev;
  475. cafe->mmio = pci_iomap(pdev, 0, 0);
  476. if (!cafe->mmio) {
  477. dev_warn(&pdev->dev, "failed to iomap\n");
  478. err = -ENOMEM;
  479. goto out_free_mtd;
  480. }
  481. cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev, 2112 + sizeof(struct nand_buffers),
  482. &cafe->dmaaddr, GFP_KERNEL);
  483. if (!cafe->dmabuf) {
  484. err = -ENOMEM;
  485. goto out_ior;
  486. }
  487. cafe->nand.buffers = (void *)cafe->dmabuf + 2112;
  488. cafe->nand.cmdfunc = cafe_nand_cmdfunc;
  489. cafe->nand.dev_ready = cafe_device_ready;
  490. cafe->nand.read_byte = cafe_read_byte;
  491. cafe->nand.read_buf = cafe_read_buf;
  492. cafe->nand.write_buf = cafe_write_buf;
  493. cafe->nand.select_chip = cafe_select_chip;
  494. cafe->nand.chip_delay = 0;
  495. /* Enable the following for a flash based bad block table */
  496. cafe->nand.options = NAND_USE_FLASH_BBT | NAND_NO_AUTOINCR | NAND_OWN_BUFFERS;
  497. if (skipbbt) {
  498. cafe->nand.options |= NAND_SKIP_BBTSCAN;
  499. cafe->nand.block_bad = cafe_nand_block_bad;
  500. }
  501. /* Start off by resetting the NAND controller completely */
  502. writel(1, cafe->mmio + 0x3034);
  503. writel(0, cafe->mmio + 0x3034);
  504. /* Timings from Marvell's test code (not verified or calculated by us) */
  505. writel(0xffffffff, cafe->mmio + CAFE_NAND_IRQ_MASK);
  506. #if 1
  507. writel(0x01010a0a, cafe->mmio + CAFE_NAND_TIMING1);
  508. writel(0x24121212, cafe->mmio + CAFE_NAND_TIMING2);
  509. writel(0x11000000, cafe->mmio + CAFE_NAND_TIMING3);
  510. #else
  511. writel(0xffffffff, cafe->mmio + CAFE_NAND_TIMING1);
  512. writel(0xffffffff, cafe->mmio + CAFE_NAND_TIMING2);
  513. writel(0xffffffff, cafe->mmio + CAFE_NAND_TIMING3);
  514. #endif
  515. writel(0xffffffff, cafe->mmio + CAFE_NAND_IRQ_MASK);
  516. err = request_irq(pdev->irq, &cafe_nand_interrupt, SA_SHIRQ, "CAFE NAND", mtd);
  517. if (err) {
  518. dev_warn(&pdev->dev, "Could not register IRQ %d\n", pdev->irq);
  519. goto out_free_dma;
  520. }
  521. #if 1
  522. /* Disable master reset, enable NAND clock */
  523. ctrl = readl(cafe->mmio + 0x3004);
  524. ctrl &= 0xffffeff0;
  525. ctrl |= 0x00007000;
  526. writel(ctrl | 0x05, cafe->mmio + 0x3004);
  527. writel(ctrl | 0x0a, cafe->mmio + 0x3004);
  528. writel(0, cafe->mmio + CAFE_NAND_DMA_CTRL);
  529. writel(0x7006, cafe->mmio + 0x3004);
  530. writel(0x700a, cafe->mmio + 0x3004);
  531. /* Set up DMA address */
  532. writel(cafe->dmaaddr & 0xffffffff, cafe->mmio + CAFE_NAND_DMA_ADDR0);
  533. if (sizeof(cafe->dmaaddr) > 4)
  534. /* Shift in two parts to shut the compiler up */
  535. writel((cafe->dmaaddr >> 16) >> 16, cafe->mmio + CAFE_NAND_DMA_ADDR1);
  536. else
  537. writel(0, cafe->mmio + CAFE_NAND_DMA_ADDR1);
  538. cafe_dev_dbg(&cafe->pdev->dev, "Set DMA address to %x (virt %p)\n",
  539. readl(cafe->mmio + CAFE_NAND_DMA_ADDR0), cafe->dmabuf);
  540. /* Enable NAND IRQ in global IRQ mask register */
  541. writel(0x80000007, cafe->mmio + 0x300c);
  542. cafe_dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n",
  543. readl(cafe->mmio + 0x3004), readl(cafe->mmio + 0x300c));
  544. #endif
  545. #if 1
  546. mtd->writesize=2048;
  547. mtd->oobsize = 0x40;
  548. memset(cafe->dmabuf, 0x5a, 2112);
  549. cafe->nand.cmdfunc(mtd, NAND_CMD_READID, 0, -1);
  550. cafe->nand.read_byte(mtd);
  551. cafe->nand.read_byte(mtd);
  552. cafe->nand.read_byte(mtd);
  553. cafe->nand.read_byte(mtd);
  554. cafe->nand.read_byte(mtd);
  555. #endif
  556. #if 0
  557. cafe->nand.cmdfunc(mtd, NAND_CMD_READ0, 0, 0);
  558. // nand_wait_ready(mtd);
  559. cafe->nand.read_byte(mtd);
  560. cafe->nand.read_byte(mtd);
  561. cafe->nand.read_byte(mtd);
  562. cafe->nand.read_byte(mtd);
  563. #endif
  564. #if 0
  565. writel(0x84600070, cafe->mmio);
  566. udelay(10);
  567. cafe_dev_dbg(&cafe->pdev->dev, "Status %x\n", readl(cafe->mmio + 0x30));
  568. #endif
  569. /* Scan to find existance of the device */
  570. if (nand_scan_ident(mtd, 1)) {
  571. err = -ENXIO;
  572. goto out_irq;
  573. }
  574. cafe->ctl2 = 1<<27; /* Reed-Solomon ECC */
  575. if (mtd->writesize == 2048)
  576. cafe->ctl2 |= 1<<29; /* 2KiB page size */
  577. /* Set up ECC according to the type of chip we found */
  578. if (mtd->writesize == 2048) {
  579. cafe->nand.ecc.layout = &cafe_oobinfo_2048;
  580. cafe->nand.bbt_td = &cafe_bbt_main_descr_2048;
  581. cafe->nand.bbt_md = &cafe_bbt_mirror_descr_2048;
  582. } else if (mtd->writesize == 512) {
  583. cafe->nand.ecc.layout = &cafe_oobinfo_512;
  584. cafe->nand.bbt_td = &cafe_bbt_main_descr_512;
  585. cafe->nand.bbt_md = &cafe_bbt_mirror_descr_512;
  586. } else {
  587. printk(KERN_WARNING "Unexpected NAND flash writesize %d. Aborting\n",
  588. mtd->writesize);
  589. goto out_irq;
  590. }
  591. cafe->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
  592. cafe->nand.ecc.size = mtd->writesize;
  593. cafe->nand.ecc.bytes = 14;
  594. cafe->nand.ecc.hwctl = (void *)cafe_nand_bug;
  595. cafe->nand.ecc.calculate = (void *)cafe_nand_bug;
  596. cafe->nand.ecc.correct = (void *)cafe_nand_bug;
  597. cafe->nand.write_page = cafe_nand_write_page;
  598. cafe->nand.ecc.write_page = cafe_nand_write_page_lowlevel;
  599. cafe->nand.ecc.write_oob = cafe_nand_write_oob;
  600. cafe->nand.ecc.read_page = cafe_nand_read_page;
  601. cafe->nand.ecc.read_oob = cafe_nand_read_oob;
  602. err = nand_scan_tail(mtd);
  603. if (err)
  604. goto out_irq;
  605. pci_set_drvdata(pdev, mtd);
  606. add_mtd_device(mtd);
  607. goto out;
  608. out_irq:
  609. /* Disable NAND IRQ in global IRQ mask register */
  610. writel(~1 & readl(cafe->mmio + 0x300c), cafe->mmio + 0x300c);
  611. free_irq(pdev->irq, mtd);
  612. out_free_dma:
  613. dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
  614. out_ior:
  615. pci_iounmap(pdev, cafe->mmio);
  616. out_free_mtd:
  617. kfree(mtd);
  618. out:
  619. return err;
  620. }
  621. static void __devexit cafe_nand_remove(struct pci_dev *pdev)
  622. {
  623. struct mtd_info *mtd = pci_get_drvdata(pdev);
  624. struct cafe_priv *cafe = mtd->priv;
  625. del_mtd_device(mtd);
  626. /* Disable NAND IRQ in global IRQ mask register */
  627. writel(~1 & readl(cafe->mmio + 0x300c), cafe->mmio + 0x300c);
  628. free_irq(pdev->irq, mtd);
  629. nand_release(mtd);
  630. pci_iounmap(pdev, cafe->mmio);
  631. dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
  632. kfree(mtd);
  633. }
  634. static struct pci_device_id cafe_nand_tbl[] = {
  635. { 0x11ab, 0x4100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MEMORY_FLASH << 8, 0xFFFF0 }
  636. };
  637. MODULE_DEVICE_TABLE(pci, cafe_nand_tbl);
  638. static struct pci_driver cafe_nand_pci_driver = {
  639. .name = "CAFÉ NAND",
  640. .id_table = cafe_nand_tbl,
  641. .probe = cafe_nand_probe,
  642. .remove = __devexit_p(cafe_nand_remove),
  643. #ifdef CONFIG_PMx
  644. .suspend = cafe_nand_suspend,
  645. .resume = cafe_nand_resume,
  646. #endif
  647. };
  648. static int cafe_nand_init(void)
  649. {
  650. return pci_register_driver(&cafe_nand_pci_driver);
  651. }
  652. static void cafe_nand_exit(void)
  653. {
  654. pci_unregister_driver(&cafe_nand_pci_driver);
  655. }
  656. module_init(cafe_nand_init);
  657. module_exit(cafe_nand_exit);
  658. MODULE_LICENSE("GPL");
  659. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  660. MODULE_DESCRIPTION("NAND flash driver for OLPC CAFE chip");
  661. /* Correct ECC for 2048 bytes of 0xff:
  662. 41 a0 71 65 54 27 f3 93 ec a9 be ed 0b a1 */
  663. /* dwmw2's B-test board, in case of completely screwing it:
  664. Bad eraseblock 2394 at 0x12b40000
  665. Bad eraseblock 2627 at 0x14860000
  666. Bad eraseblock 3349 at 0x1a2a0000
  667. */