cafe.c 21 KB

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