cafe.c 21 KB

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